Transition of alpha via Action Script

this isnt a direct problem but i saw a post where suprabreena mentioned using scripts for alpha commands and movement. this is something im not very aware of but would like to know more about, so heres my question, when using a script such as “this” do you only need to put the image in one frame of your layer? say frame 1, and then add the action to that frame alone? also if you could give some examples of what actions you would use to do different things such as motion tweens and alpha fades and so forth and how they work in summary it would help alot. file size is perhaps the one thing i battle more than anything else\rthanks.

There are a couple of ways of doing this. Usually one is trying to move an object in a fluid motion… you’ll have to set up a loop to do this.\r\rI’d suggest this to play with first.\r\rCreate a movieclip and call it controler. Put nothing inside it.\rDrag it from the library to the main stage. Click on it once, if it is not currently selected.\rOpen up your Action panel.\r\rtype in\r\ronClipEvent (load) {\r trip = 0;\r counter = 0;\r}\ronClipEvent (enterFrame) {\r if (trip==0) {\r counter=counter+5;\r }\r if (trip==1) {\r counter=counter-5;\r }\r setProperty ("_root.circle", _alpha, counter);\r if (counter==100) {\r trip = 1;\r }\r if (counter==0) {\r trip = 0;\r }\r}\r\rNow place a circle on the main stage, and make it into a movieClip. Open the instance panel and name it “circle”. <~very important!!!\r\rTry out your movie the circle should fade in and out.\r\rOther things can be done by changing the Property, from Alpha to something else. Try playing around with it.\r\rThis example runs 448 bits… that’s less than 1k

one of the lovely things about the flash 5 dot syntax, is that you can access properties of objects in that way, thus doing away with clunky functions like setProperty();\r\ranother marvel is “this”, which will refer to the relative root from wherever the code is.\r\rperhaps not so clear - here’s another version of the fading code rewritten using both these techniques:\r\ronClipEvent(enterFrame){\rif (this._alpha == 100) {\rdiff = -5;\r} else if (this._alpha == 0) {\rdiff = 5;\r}\rthis._alpha += diff;\r}\r\rbecause this code is in “circle”, “this” refers to “circle”. now we needn’t name our movie (unless we refer to it from elsewhere), and setting the _alpha property is nice and neat like this too, n’est pas?\r\rthis will cause the circle to be initially visible and fade out then in then out … if you want it to fade in first, make this._alpha=0 in and onClipEvent(load); event.\r\redited to add whitespace, i hate that this message board removes it.\r\rarrgh. adding a character at the beginning of the line didn’t help! >: (\r\rany solutions? it will make code so much more legible.

There have been some lovely alternatives on the forum. Here it goes :\rPatola :

  onClipEvent (enterFrame)\r\r{\r\r        if (this._alpha >= 100)\r\r        {\r\r                diff = -5;\r\r        }\r\r        if (this._alpha <= 0)\r\r        {\r\r                diff = +5;\r\r        }\r\r        this._alpha += diff;\r\r}

Thoriphes :

  onClipEvent(load) {\r\rdiff = 5;\r\r}\r\ronClipEvent(enterFrame) {\r\rif (_alpha >= 100 || _alpha <= 0) {\r\rdiff = -diff;\r\r}\r\r_alpha += diff;\r\r}

Supra :

   onClipEvent(enterFrame){\r\r        _alpha = Math.abs(-100+(a+=5)%200);\r\r}

Eyez also explained that

% is the modulo operator, results in the remainder of any two numbers.\rExample: 10%2 returns 0,\r10%3 returns 1…\r\rVery usefull to determine if a number is even or odd: number%2 is 0 when it’s even.\r\rJust use trace (number%another number) and check the result.
pom 0] \r

Thanks for Cliping this in here POM… I was just about to do that… :slight_smile: (He beat me to it… he must be bucking for a promotion… I’ll have to sabotage him with the boss. Muhahaha)

Hey, Upu, you’re not supposed to post here !! Did you see that, Boss ??\r\rpom 0]

Hi all,
is it possible to use the above code from upuaut8 but have it so once it fades up and back down it stops so that another image can fade in in place of it, kind of like a slide show.
:bandit:

Here ya go… with an MX update…

[AS]MovieClip.prototype.fade = function(minAlpha, maxAlpha, speed) {
this.onEnterFrame = function() {
if (!this.trigger) {
this._alpha -= speed;
if (this._alpha<=minAlpha) {
this._alpha = minAlpha;
this.trigger = true;
}
} else if (this.trigger) {
this._alpha += speed;
if (this._alpha>=maxAlpha) {
this._alpha = maxAlpha;
this.trigger = false;
}
}
};
};
clipToFade.fade(0, 100, 5);[/AS]

Thankx lostinbeta,
I tried this using MX but nothing is happening at all now. I can’t understand if I need to name the instance (I can’t see any root name).

I have MX but I stiil favour Flash 5 as I’m still finding my way around the MX.:bandit:

clipToFade is the instance name you will need to target.

I never tried Flash 5, but from what I hear, MX beats it out hands down.

Strewth that was a fast reply,
Okay i’m probably being really thick this morning but this is what i’ve done, I made a shape into a movie clip, typed clipToFade into the instance name and copied and pasted your code but still nothing, I know i’m probably missing something really simple here but I can’t understand what.:bandit:

It works for me. It just fades a clip in and out, I know you said like a slideshow, but I figured that would be a point in the right direction.

Heres an example file…

Hey lostinbeta,
I tried this and it still didn’t work so I shut down my computer and restarted it and it works fine, thanks but as it fades in and fades out where in the code would I place the stop action to stop it repeating.
I know i’m being a pain but I appreciate the help

to stop the fading you can use [AS]delete this.onEnterFrame[/AS] in the prototype. What exactly are you trying to do? Are you trying to make a slide show? If so have you seen this?

http://www.kirupa.com/developer/mx/photogallery.htm

Hi lostinbeta,
that was perfect for me and it worked fine, thanks a lot for that youve been a great help.:bandit:

I am glad I could help :slight_smile:

Hi, lostinbeta:

can you modify that photo gallery (http://www.kirupa.com/developer/mx/photogallery.htm) so instead of click arrows for next photos, it just automatically externally loads in JPGs every 5 seconds (and each one fade in and out once)? That would be a true slideshow, and I believe you can make it.

Thanks

Sure, add this to the very end of the script…

[AS]function nextUp() {
changePhoto(1);
}
setInterval(nextUp, 5000);[/AS]

And remove your buttons and this script [AS]this.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
this.changePhoto(-1);
} else if (Key.getCode() == Key.RIGHT) {
this.changePhoto(1);
}
};
Key.addListener(this);[/AS]if you just want it to be a slide show, otherwise people will still be able to go back and forth on their own via the arrow keys or the buttons on your stage.

Hi, lostinbeta:

This work like charm! You must be a very good scripter because I saw other had to use many line of code to get the same effect while your simple 3 lines would do the trick.

Just adding a little spice: Is it possible to randomly load in JPGs without messing with those code??

I tried adding in following code below “this.pIndex=0;”:

function randomPhoto() {
randomNumber = random (this.pathToPics+this.pArray.length);
loadMovie(this.pathToPics+this.pArray[randomNumber], _root.photo);
}

randomPhoto ();

Well, it does not randomly load. always the same photo (pea****) first.

Thanks a again.

The easiest way to do this would be to randomize the array itself and not randomize it’s selection of images.

There is no default method to randomize an array in Flash (although there should be!), so we will have to create one.

Add this at the beginning of all the code…[AS]Array.prototype.randomize = function() {
var arrayLength = this.length;
var randNum, tempArray;
for (i=0; i<arrayLength; i++) {
randNum = Math.floor(Math.random()arrayLength);
tempArray = this
;
this* = this[randNum];
this[randNum] = tempArray;
}
};[/AS]

And then right after the declaration of the “this.pArray” array (contains the list of images to load) add this line… [AS]this.pArray.randomize();[/AS]

Tah dah, each time this loads the images will be random.