stillwaiting,
[quoted text, click to view] > How would I make the alpha transition smooth, taking
> about 1/2 to 1 second to go from 35% to 100% and back?
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."