ActionScript 3 Particle System Snowfall in Flash

ActionScript 3 Particle System Snowfall in Flash
Method Two

The second method will allow us to use only a few snowflakes instead of continuously creating new ones. But to do this, we will need to find a way to recycle the snowflakes that we do create. The first step is to change the way we call the snowflake function which creates the snowflakes. We no longer want to create a new snowflake with each new frame, so we will need to remove the Event Listener at the top of the code.

stage.addEventListener(Event.ENTER_FRAME, snowflake);

Now we will add the code that will create a small number of snowflakes. We will use a for loop statement within the snowflake function to control the number of snowflakes that will be created. We will need about 70 snowflakes. If we increase this number much higher, the animation will again become tasking. Place your cursor inside the snowflake function code, just after the opening curly bracket and add the line of code which will tell Flash to repeat the snowflake function 70 times. Next, add the opening curly bracket for the loop. You might recognize this for loop code from the second tutorial in this series.

function snowflake(event:Event):void
{
for (var i:Number = 0; i < 70; i++)
{


The rest of the snowflake function code should remain the same. Now, we need to close the for loop with a closing curly bracket. Place this bracket just after last line of the function. Here is the code. I have moved the body of the function to the right to make it more readable. However, this is not necessary for the code to work.

Finally, we need to remove the snowfall function from within the snowflake function. This is easy to do. Just move the curly bracket that closes the snowflake function from the bottom of the program to just under the end of the for loop code. Now we have two functions that are completely separate from each other.

Because we are no longer calling the snowflake function with the Event Listener that we removed from the top of the code, we will need to call it with a simple function call instead. Since we are using a for loop statement within the function to repeat the function 70 times, we only need to call the function itself once. At the end of the entire program code, place this function call.

snowflake();

Finally, we need to remove the removeChild code that we added to our code for Method One. Remember that we want to recycle these snowflakes instead of removing them from the display list. Remove the following code.

removeChild(_Snowflake);

If you test your movie, you will get error messages. Let's take care of the last error in the list. We got this error message because we have one argument inside the parentheses of the snowflake function code. But we are no longer using this "event:Event" argument since we removed the Event Listener. We need to remove the "event:Event" from inside the parentheses.

function snowflake():void

← Back | Next →

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.





RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map








Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.