This defines the function called fadeOut (it does not
execute it), with the variables startboxnr and speed. This function will
call the function fadeMC and passes startboxnr and speed along to fadeMC. So
this function receives the values startboxnr and speed, and calls the
function fadeMC and passes the same variables it received along to fadeMC.
function fadeMC : This defines the function fadeMC
with the variables mcnr and speed.
this.onEnterFrame = function(){
declares an anonymous function that executes onEnterFrame of the
movieclip "box"+mcnr (mcnr is the variable the function receives when
called, remember ?). So say that we call the function fadeMC and pass the
variables mcnr and speed along with the values of 3 and 5 for example, it
will define the onEnterFrame for the movieclip box3. If we'd pass along 4
and 5 for example, it will define the onEnterFrame for box4. Keep that in
mind.
this._alpha -= speed
decreases the alpha value of this (which is box+mcnr) with the value speed.
Since this is inside the function onEnterFrame, this line will keep
executing until the onEnterFrame is deleted.
if(this._alpha<=smoothness){
checks if the alpha value of this (box+mcnr) is less than or equal to the
value of the variable smoothness (which we defined before). If so, it
executes anything inside the brackets { }. The onEnterFrame is still open,
so this line will also be executed until the onEnterFrame has been deleted.
this.onEnterFrame = null
sets the onEnterFrame of the movieclip of the movieclip targetted by this to
null. This is executed when the if statement is true. It sets the
onEnterFrame of the movieclip "box"+mcnr to null, so the if statement which
is inside the onEnterFrame will no longer be executed. Flash will execute
everything left inside the if statement after the onEnterFrame has been
deleted.
continueFade(this,speed)
executes the function continueFade and passes the variables this ("box"+mcnr)
and speed along. These will be used in the function continueFade. We will
get back to the function continueFade later.
mcnr += 1 adds
the value 1 to the value of variable mcnr. So say mcnr was 5, after this
line it will become 6.
fadeOut(mcnr, speed)
executes fadeOut and passes the variables mcnr (which has just increased by
one) and speed along. So this will redo everything you've read in this
explanation, but with an increased mcnr.
The }'s end
the functions and the if statement.
See how it
works ? This is a short example: mcnr is 0 (the inital value), fadeOut
executes fadeMC and passes mcnr along, fadeMC uses mcnr to define
this["box"+mcnr].onEnterFrame (so box0.onEnterFrame), the onEnterFrame
decreases the alpha until less or equal to smoothness (which has the value
90, we defined it in the beginning of the code) and if so, it deletes the
onEnterFrame, increases mcnr with 1 and does everything over by executing
fadeOut and passing mcnr and speed along.
But how come
the boxes fade out entirely when the onEnterFrame is deleted and everything
is redone at alpha 90 or less ? This is were continueFade comes in. Remember
that continueFade was also inside the if statement that is executed when the
alpha is equal to or less than 90. So continueFade will also be executed
when the onEnterFrame is deleted. Let's have a look:
function
continueFade(mc,speed) { - This defines the function continueFade
with the arguments mc and speed. But how can you execute it when it hasn't
been defined yet ? We executed it in the previous function, right ? Well,
we'll see about it later. This is what this it does:
mc.onEnterFrame = function(){ - sets an anonymous function as the
onEnterFrame handler of the movieclip mc. It received mc when executing (continueFade(mc,
speed)), so it will define the onEnterFrame of the value that was passed
along. Now have a look back at the previous function. We called continueFade
there. What is the first value we passed along ? Right ! this["box"+mcnr] !
It's onEnterFrame has been deleted in the previous function, so what we're
doing now is redifining it. That's right. We first delete it, and then
redefine it. So the value this["box"+mcnr] is stored (mcnr has not yet been
increased with 1 since we call continueFade before we increase mcnr with 1)
in the value of the variable mc.
this._alpha -=
speed - decreases the alpha value of this with the value of the variable
speed, so we're actually setting the alpha of this["box"+mcnr]. This line is
inside the onEnterFrame, so it will be executed until the onEnterFrame has
been deleted or set to null.
if
(this._alpha<= 0){ - checks if the alpha is less than or equal to 0. If so,
it will execute anything inside the { } brackets. This line is also inside
the onEnterFrame, so it will be executed until the onEnterFrame has been
deleted or set to 0.
delete
this.onEnterFrame - deletes the onEnterFrame of the movieclip the
onEnterFrame is assigned to. Since the alpha has reached zero now, it is no
use to keep the onEnterFrame. In fact, if you'd keep the onEnterFrame, it
can slow down your entire movie.
The }'s end
the if statement and the functions.
Okay, now we
know how and why all the functions work. But no function has been executed
yet ! Function1 executes function2, but to be able to do that function1 has
to be executed first. fadeOut is the function that sets everything to work.
If it's been called once, everything goes automatically. So actually, the
function fadeOut is our start trigger. Execute it, and everything will get
to work. That's why the last action executes fadeOut. Because it's on the
first frame, it will be executed when the first frame of the movie has been
entered, which is good if you want the effect to autostart. If not, you can
always execute it through a button, for example
on(release){
fadeOut(0, 5)
}
on a button
will start the whole process. That's what I used on the button of the sample
movie.
The two values
0 and 5 are the values that are passed along when executed. 0 is the number
of the box to start fading at and 5 is the speed at which the alpha
decreases. It will decrease 5 every onEnterFrame. So the higher this value,
the faster it will fade. The variable smoothness which we defined in the
beginning is the alpha value which one box must reach before the following
will start fading.
On the next
page, you will learn how to modify the effect: |