Optimizing Flash CS3 Particle System Animation

Optimizing Flash CS3 Particle System Animation
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.

Method One

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.

if ( _Snowflake.y> 370 || _Snowflake.x> 550)
stage.removeEventListener(Event.ENTER_FRAME, snowfall);
{ _Snowflake.y += 5; _Snowflake.x += Math.random() * 10; }

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.

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.

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

Content copyright © 2022 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.