|Now that we have the FlashR CS3 particle system snowfall animation working exactly like we want, it's time to optimize the snow scene program code. If you tested the animation that we have written in the previous tutorials, you probably noticed that after a time, the animation tends to task your computer's resources. In this tutorial, we will look at a few ways to make the animation more efficient. |
As the code stands now, we are using one Event Listener to listen for each new frame of the animation. With each new frame, the snowflake function (event handler) creates a new snowflake and adds that snowflake to the Flash display list. Next, we use Math.random() to randomize the position and alpha properties of this snowflake. We use a second Event Listener to call the snowfall function, again with each new frame. The snowfall function checks to see if the snowflake has moved beyond the right or bottom edge of the stage. If so, we remove the second Event Listener that repeats the snowfall function. This causes that one snowflake to stop falling, while the other snowflakes continue to fall. The repetition of the snowflake function creates the snowfall animation. What we need to do now is to change the code so that it is more efficient.
The first method requires the addition of only one line of code to our program. We already have a conditional statement inside the snowfall function that checks to see if the snowflake has gone beyond the stage and, if so, removes the Event Listener and stops the snowflake from falling.
The snowflake stops moving but it is still visible at the bottom of the stage. Because it is still visible and still on the display list, Flash still spends resources on this snowflake. If we remove the snowflake from the display list, it will free up the resources and the animation will be more efficient. To remove the snowflakes that we no longer need on the display list, we can add a removeChild code to the conditional statement.
When you test your movie, the snowflakes disappear as they reach the right or bottom edge of the stage and the animation is more efficient.