by Sam Kellett aka Sammo |
23 December 2005
In the previous page,
you created the snow effect. In this page, you will learn why your code works
the way it does.
The Init Function
Let's start with the init function whose main responsibility is to setup
all of the snowflakes. Let's take it a few lines at a time:
- width =
- // pixels
- height =
- // pixels
The width and height variables specify the area your snow effect will be
displayed in. Normally, your width and height values will be the same as the
width and height of your movie's stage.
- max_snowsize =
- // pixels
- snowflakes =
- // quantity
The max_snowsize variable specifies the scale value used in
determining the width and height of your snowflake. The larger the value, the
larger your snowflake.
The snowflakes variable determines the number of snowflakes that
appear on your screen at any given time. If you increase this value, more
snowflakes will appear on your screen, but system performance may also decrease.
- for (i=0;
Our snowflake properties are specified inside this for loop. The for
loop's range is from 0 to the number specified by the snowflakes variable.
- t =
The variable t references the movie clip returned by attachMovie. The
attachMovie function takes in three arguments: movie clip identifier from the
library, new movie clip name, and the new movie clip's depth.
The argument "snow" lets you retrieve the snow movie clip you
created earlier. The value for snow is the not the name of the object from your
Library, but instead it is the name you entered in the Linkage Properties dialog
The new movie clip name is going to be "snow"+i. The value of i cycles
from 0 to the number stored by the snowflakes variable. Your new movies will,
therefore, have names such as snow0, snow1, snow2, etc.
The above four lines set the alpha, x, y, xscale, and yscale properties of
the t movie clip. The statements are fairly self-explanatory so I will not go
into great detail explaining them. Just be sure to note how the variables we
declared are used. I use Math.random() to try to ensure that the snowflakes look
different. After all, no two snowflakes are created equal :P
These two lines specify more properties of our snow movie clip. I did not
include them in the above group of four lines because they do not call built in
movie clip object properties. Instead, they declare and instantiate the
user-created variables k and wind.
The value k references the speed at which our snowflakes hit the ground, and
the value for wind stores the speed at which the snowflakes oscillate
horizontally. Remember, in Flash, you do not have to explicitly declare
variables prior to using them.
In the final line of our init function, I set the t movie clip's
onEnterFrame handler to the value of the mover function.
What good is a function if it isn't called? At the very bottom of all of your
code, I make a call to our init function.
To summarize, the init function is responsible for displaying the snowflakes
on the screen and assigning properties to each of them such as their initial x
and y positions, alpha, scale, vertical speed, and horizontal speed.
The mover function is responsible for moving the snowflakes. It is quite
straightforward, and Kirupa summarized most of how it works in his earlier
iteration of this tutorial:
The main thing about the mover function you need to remember is that is
called by the onEnterFrame event handler of each snow movie clip. If your frame
rate is 25, the mover function is called 25 times each second. The mover
function uses the this variable to reference the properties of that
If you have any questions, feel free to post them on the