Groups | Blog | Home
all groups > flash actionscript > october 2005 >

flash actionscript : making alpha change progressive


stillwaiting
10/27/2005 8:36:13 PM
I have the following code attached to a mc

on(rollOver){
this._alpha = 100;
}
on(rollOut) {
this._alpha = 35;
}

How would I make the alpha transition smooth, taking about 1/2 to 1 second to go from 35% to 100% and back?

Andrew Fitzgerald
10/27/2005 8:43:15 PM
Some people would give you an onEnterFrame code.

I'll give you full AS tweening.

http://laco.wz.cz/tween/

Download that, and you can simply do:

this.alphaTo(35, 0.5);
davepla
10/28/2005 12:00:00 AM
Do the alpha transition in a function and call it through a timer:

T = setInterval(alphaFunction, mseg);

remember to use clearInterval(T); to kill the timer once your MC have arrived
to the proper alpha

HTH,

David P.
Equan
10/28/2005 12:00:00 AM
first set the alpha to 35 so it starts there on load
On the timeline put this code;
this._alpha = 35

then put this code on button

on(rollOver){
onEnterFrame = function(){
this._alpha+=(100-this._alpha)/10;
}
}

on(rollOut){
onEnterFrame = function(){
this._alpha+=(35-this._alpha)/10;
}
}
David Stiller
10/28/2005 12:09:55 AM
stillwaiting,

[quoted text, click to view]

This kind of transition, as *any* kind of animated transition, occurs
when you make small increments over time. Your sample code is, as you
mentioned, instant. To make the alpha change over half a second, you would
look at your fps (frames per second) -- default is 12 -- and decide how many
frames it would take to transpire over half a second. Well, at 12 fps, half
a second would be 6.

So, the difference between 100% and 35% is 65. 65 divided by 6 is
(approximately) 11. So you'll need to adjust the _alpha property by 11 once
every frame for 6 frames. Fudge the numbers a bit, as you like, but that's
the general idea.

As it turns out, every movie clip features an event called onEnterFrame.
(In fact, every movie clip features *all kinds* of events and other useful
features. To see them all, look up the "MovieClip class" entry of the
ActionScript Language Reference.) The onEnterFrame event occurs every time
a movie clip enters a frame -- even if the movie clip has been stopped with
a stop() action. (Just imagine the onEnterFrame event as a car engine
that's idling: even when the car isn't moving, the engine is turning over.)

Give your movie clip an instance name. Step away from that old
fashioned on() handler business. :-p It's much more convenient to give
your clips instance names ... this lets you write all your code in the same
place (in a keyframe of a scripts layer), rather than having to touch each
movie clip and lob code all over the place.

Create a new layer, then, and name it scripts (or actions, or whatever
strikes your fancy). In a keyframe that occurs in the same frame as your
movie clip, type the following -- substituting my sample instance name with
the one you give your clip ...

yourMovieClipInstanceName.onRollOver = function() {
}

.... which means you're about to assign a function to its onRollOver event.
The function you're going to assign will, as it turns out, assign a second
function to this clip's onEnterFrame event.

yourMovieClipInstanceName.onRollOver = function() {
this.onEnterFrame = function() {
}
}

.... with me so far? Now the second function will adjust this clip's _alpha
property and decrease it by 11 every time, as discussed above.

yourMovieClipInstanceName.onRollOver = function() {
this.onEnterFrame = function() {
this._alpha = this._alpha - 11;
}
}

A short way to say this._alpha = this._alpha - 11; is to simply put
this._alpha -= 11, so I'll use that moving ahead.

We'll add an if() statement to stop the onEnterFrame loop when _alpha
reaches 35 (this is something you can't do, by the way, with the on()
handler .... loops just continue forever).

yourMovieClipInstanceName.onRollOver = function() {
this.onEnterFrame = function() {
if (this._alpha > 35) {
this._alpha = this._alpha - 11;
} else {
delete this.onEnterFrame;
}
}
}

To be really sure about it, let's make sure to set _alpha to exactly 35,
for good measure, at the right time.

yourMovieClipInstanceName.onRollOver = function() {
this.onEnterFrame = function() {
if (this._alpha > 35) {
this._alpha = this._alpha - 11;
} else {
this._alpha = 35;
delete this.onEnterFrame;
}
}
}

Badda bing. This is very similar to how it would be done with an on()
handler, so if you really prefer to go that route, toy with the essential
mechanism of what I've shown here. Using the above sample, you should try
to reverse the situation for the onRollOut even of the same clip. Just
write *that* event handler underneath the first (or over it; makes no
difference).


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

stillwaiting
10/28/2005 9:18:13 AM
Wow, you guys(and gals if there are any) go far beyond the call of courtesy.
Dave, your explanation enlightened me exponentially and everybody's methods
where helpful. You guys rock. http://www.sporthoj.com/forum/images/smilies/rockbanana.gif

stillwaiting
10/28/2005 10:10:59 AM

Works (almost) great, at least now that I have the starting alpha matching the alpha specs in the AS(kicking self for such a lame senior moment).
The one problem is this, when the onRollOver event is triggered some clock begins and the time that is spent
hovering over the mc is somehow added on to the front of the onRollOut function. In other words if I spend
5 seconds hovering over the mc, after I trigger the onRollOut it takes 5 seconds before the onRollOut alpha
change begins. Here is my code:

seth_swing.onRollOver = function() {
this.onEnterFrame = function() {
if (this._alpha > 30) {
this._alpha = this._alpha + 7;
} else {
this._alpha = 100;
delete this.onEnterFrame;
}
}
}
seth_swing.onRollOut = function() {
this.onEnterFrame = function() {
if (this._alpha > 30) {
this._alpha = this._alpha - 7;
} else {
this._alpha = 35;
delete this.onEnterFrame;
}
}
}

stillwaiting


[quoted text, click to view]
Rothrock
10/28/2005 11:37:15 AM
Darn! No matter what I do, David always types more than I do. The link below is
my answer to somebody who wants to move a movie over time, so in that case we
are changing the _y property instead of the alpha, but the ideas are the same.


http://www.macromedia.com/cfusion/webforums/forum/messageview.cfm?catid=194&thre
adid=1072477

Here is another thread about fading:


http://www.macromedia.com/cfusion/webforums/forum/messageview.cfm?catid=288&thre
adid=1029037&highlight_key=y&keyword1=fade

For a bit of info about the alpha property. You will find that it isn't always
what you set it to. So never count on it hitting an exact value like 35. The
values you can count on are 0, 25, 50, 100 ? the rest will be off by just a bit.

That is because internally flash turns you number into a value between 0 and
256 ? similar to the hex values for the RGB colors it is just another 8-bit
channel.

So for example:

this._alpha=35;
trace(this._alpha) //traces 34.765625

So you should always use the less than or greater than operators to compare
alphas.
David Stiller
10/28/2005 2:11:00 PM
stillwaiting,

[quoted text, click to view]

Look carefully, and you'll see a logic error in your onRollOver event
handler. My guess on why it seems to pause before the fading takes place is
that your logic allows the _alpha to increment higher than 100%. Depending
on how long you hover, it might be around 180 (or who knows how high?) by
the time you roll away -- at which point the onRollOut beings to decrement
the _alpha property. Naturally, the clip will be fully opaque until the
_alpha dips below 100.

Here is my code:
[quoted text, click to view]

Your if() condition in this case should only increment by 7 if
this._alpha < 100.


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

stillwaiting
10/28/2005 5:42:45 PM
Um, your code is exactly the same as mine. I have made some timing changes so here is the updated code. I added the first line to accomplish the
starting opacity without using the property inspector thinking that might make a difference (didn't). I can't believe it worked I just thought, you
know this might be how you would do this and sure enough it is. :)(patting self on head)

seth_swing._alpha = 25
seth_swing.onRollOver = function() {
this.onEnterFrame = function() {
if (this._alpha > 25) {
this._alpha = this._alpha + 5;
} else {
this._alpha = 100;
delete this.onEnterFrame;
}
}
}
seth_swing.onRollOut = function() {
this.onEnterFrame = function() {
if (this._alpha >= 25) {
this._alpha = this._alpha - 5;
} else {
this._alpha = 25;
delete this.onEnterFrame;
}
}
}

With the above code the alpha change only works onRollOut and has no pause. onRollOver = 100% instantly. One mod I tried was
to set the onRollOut to "if (this._alpha > 24)" This as well as (this._alpha >= 25) produces the proper alpha shift each RollOver/Out but
creates the pause.
Other things I have tried usually do one of two things. Either onRollOver goes immediately to 100% opacity, or it fades
the first time but not after. In the cases where it fades the first time only, if I hover on the first
rollover the opacity continues to go up as you described. After that though, if I rollOver again, even though
it goes immediately to 100%, it doesn't go beyond and onRollOut has an immediate effect. I checked and rechecked
my code against yours and it seems to be exactly the same.

Am I correct in assuming that delete this.onEnterFrame; is supposed to stop the progression when the _alpha reaches 100%? If the progression is 5% per
frame, starting at 25, how could it not be hitting 100%?

Confused,
stillwaiting

[quoted text, click to view]
stillwaiting
10/28/2005 6:21:30 PM
BAM! That did it. Makes perfect sense too.

stillwaiting

stillwaiting
10/28/2005 6:23:02 PM
Nevermind.

seth_swing._alpha = 25
seth_swing.onRollOver = function() {
this.onEnterFrame = function() {
if (this._alpha < 100) { <----------This did it
this._alpha = this._alpha + 5;
} else {
this._alpha = 100;
delete this.onEnterFrame;
}
}
}
seth_swing.onRollOut = function() {
this.onEnterFrame = function() {
if (this._alpha > 25) {
this._alpha = this._alpha - 5;
} else {
this._alpha = 25;
delete this.onEnterFrame;
}
}
stillwaiting
10/28/2005 7:52:41 PM
Yes I do. The if condition is met by both > 25 and < 100 but with > 25 it will continue triggering infinitely because the mc will always have an alpha
value of more than 25 once the onRollOver is triggered. My logical mis-step was that I thought the else statement said (translated to plain English)
"when it gets to 100 then stop." I think it roots from a misunderstanding of what exactly "delete this.onEnterFrame" does. I was under the impression
that the else statement defined the stopping point. Now I see that all of that is accomplished by the if statement and the else is only stopping the
engine (as you described earlier).
I also see that you told me the if value needed to be < 100 in your last post but I missed it and got confused between what was a quote and what you
where saying.

stillwaiting




[quoted text, click to view]
David Stiller
10/28/2005 9:40:16 PM
Yes! That's right. :) I was about to answer and point that out again.

Do you see *why* that does it?


David
stiller (at) quip (dot) net
"Luck is the residue of good design."



[quoted text, click to view]

Gionex
10/29/2005 12:00:00 AM
i wish i got replys to my posts as much as this :s
here is my post HELP ME OUT :D blurrrrrrrriing
http://www.macromedia.com/cfusion/webforums/forum/messageview.cfm?catid=288&thre
adid=1074330&enterthread=y
jesusdoulos
10/29/2005 12:00:00 AM
-- From /*current alpha*/ to 100 if /*end*/ is 0
-- From /*current alpha*/ to 0 if /*end*/ is 0

var speed = 0;
var init = this._alpha;
var end = 100;
speed = (speed + (end - init) / faster) / grown;
this._alpha += speed;

-- /*faster*/ and /*grown*/ are variables you should set to 1.07 and 1.5
-- but I suggest you to try different and see the effect you'll get.

-- Simple Matematical Function -- (always solve your Flash prob with Math;)
midi510
10/29/2005 1:10:43 AM
David Stiller
10/29/2005 1:16:58 AM
stillwaiting,

[quoted text, click to view]

You're right on track! Really, you figured it out, every bit of it. :)
Seeing this kind of "Aha!" moment is why I spend time here. I'm glad you
nailed it.

Keep up the good work!


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

AddThis Social Bookmark Button