Creating a Falling Snow Effect in Flash 8 (continued)1 2
|
code |
explanation |
movieWidth = 468;
shape = Random(6) + 1;
drift = (sizeSnow * 4) - 2;
step = (sizeSnow * sizeSnow * 2) + 1;
gotoAndPlay(3); |
movieWidth is a variable that stores the width of the movie - change this to the actual width of your movie if you are creating a movie of a different size. movieHeight is a variable that stores the distance the snowflake will fall (ie the height of the movie plus the height of the symbol) If we just put the actual movie height here the flake will disapear slightly before it hits the bottom. Again, if your movie is a different size be sure to reflect that here. this line of code sets a random horizontal starting position. The Math.random() function generates a random number between 0 and 1 and that is multiplied by the width of the movie to set the. _x (horizontal position) of the snowFlake instance. This line of code sets a random vertical starting positon. The Math.random() function generates a random number between 0 and 1 and that is multiplied by the height of the movie to set the. _y (vertical position) of the snowFlake instance. These 2 lines of code set which frame of the flake symbol to display. The shape = Random(6) + 1;, generates a random number between 1 and 6 and stores that number in a variable called shape. The next line uses the number stored in the shape variable to set the snowFlake instance of the flake symbol to go to that frame number and stop which allows us to display one of the six snowflakes we created. sizeSnow = Math.random(); creates a random number between 0 and 1 and stores that in a variable called sizeSnow. We will then use this number for to generate other numbers used to set multiple aspects of the snowflake such as size, speed, rotation and opacity. This will give each snowflake a cohesive look - a smaller random number will generate snowflakes that are smaller, slower, and less opaque, larger numbers will generate snowflakes that are larger in size, faster and are less transparent. This will give the illusion of depth to our snowfall. drift = (sizeSnow * 4) -2; This line of code takes the random number stored in sizeSnow and uses it to generate another number between -2 and 2 (this will allow our snowflake to drift both left and right. spin = (Math.random() * 6) - 3;This line of code takes the random number stored in sizeSnow and uses it to generate another number between -3 and 3 (this will allow our snowflake to rotate in up to 3 degree increments in both directions These 2 lines of code set the size of the snowflake by using the random number stored in sizeSnow to create a snowflake that is between 25% and 100% of the original size of the flake symbol. This line of code again the random number we have stored in sizeSnow, this time to set the opacity of our snowflake between 25 percent and 100 percent. This line of code generates a number based on the random number stored in sizeSnow (creating a number between 1 and 3) that will be used to increment the position of the snowflake vertically (ie causing it to fall - each snowflake will fall at a slightly different rate); This line of code tells the symbols playhead to skip frame 2 and go directly to frame 3; |
Place your cursor in frame 2 of the fallingSnow symbol and in script window of the actions panel and enter the following code. Frame 2 is used for resetting the snowflake to the top of the movie and assigning it a new set of attributes so that our snowfall appears random.

code |
explanation |
snowFlake._y = 0;
|
this line of code resets the snowflake to the top of the movie
These lines of code function much as the code in frame one. basically regenerating a new random number for the sizeSnow variable so that each time the snowflake returns to the top of the page it gets a new set of attributes for speed, drift, spin, size and opacity. By resetting these attributes each time our movie will display more variety. |
Place your cursor in frame 4 of the actions layer in the fallingSnow symbol and enter the following code:
code |
explanation |
whereSnowV = snowFlake._y;
if ((whereSnowV <= movieHeight) && (whereSnowH >= - 20) && (whereSnowH <= movieWidth + 20)) {
|
This line of code captures the vertical position of the snowflake and stores it in a variable named whereSnowV. This line of code captures the horizontal position of the snow by adding its horizontal position plus the amount of drift and then stores it in a variable named whereSnowH. If the snowflake is still on screen these lines of code will increment the vertical postion, drift and rotation and then returns the playhead to frame 3 where it will loop back to frame 4 and start the process all over again. If the snowflake has moved beyond the edges of the movie then the symbol playhead will be sent to frame 2 where the snowflake will be reset to the top of the screen with new attribute values. |
Test your movie by clicking Ctrl + Enter (win) Cmd + Return (mac) or Control > Test Movie.
Now that we have our actionscript working as it should we need to add more snow. Return to scene 1 and select the fallingSnow symbol and copy it Ctrl + C (win) or Cmd + C (mac) or choose Edit Copy. Then choose Edit > Paste in Place or Ctrl + Shift + V (win) or Cmd + Shift + V (mac). Repeat to create as many snowflakes as you want for your movie.
1 2
Illustrator tutorials | Photoshop tutorials | InDesign Tutorials | Acrobat Tutorials | Dreamweaver Tutorials | Flash Tutorials | Easter Eggs
Home | About | Training | Calendar | Register | Tutorials | Freelance | Contact | F. A. Q | Become an ACE