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.
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.
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.
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.
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.
← Back | Next →