Flash CS3 Variables & ActionScript 3 Code

Flash CS3 Variables & ActionScript 3 Code
Control the Number of Particles

We will use our next variable to control the maximum number of particles for our particle system. We have hard coded this to 70 in the for loop statement. Let's create a new variable and name it "maxParticles" and set it to 70. Then replace the 70 in the for loop with the variable name.

var maxParticles:Number = 70;

. . .
for (var i:Number = 0; i < maxParticles; i++)

Control the Maximum Size for the Particles

When we recycle our particles back up to the top, we place then in a random position between 0 and 40 along the Y axis.

event.target.y = Math.random() * 40;

This is because the maximum size of our snowflakes are 40 pixels. However, if we use a different graphic for our particles which is a different size, we need to set the random number between 0 and the new size. We can add a variable called maxSize and replace the 40 with that variable.

var maxSize:Number = 40;

. . .
event.target.y = Math.random() * maxSize;

Control the Maximum Movement

The last variable that we will add will control the amount of pixels that our particles move downward with each cycle of the particlefall function. Now, we have this hard coded to 5 pixels for the vertical movement and 10 pixels for the horizontal movement.

{ event.target.y += 5; event.target.x += Math.random() * 10; }

Let's call our new variable "maxMove" and set it to 5. Then we can replace the 5 in the code with the variable and the 10 with a value of twice the variable.

var maxMove:Number = 5;

. . .
{ event.target.y += maxMove; event.target.x += Math.random() * (maxMove * 2); }

Test your movie. With all these changes, it still works. But we might also wish to use a different source for our particle other than the mcSnowflake movie clip.

Change the Particle Source

If you remember, we are using the mcSnowflake movie clip as the source for our particles. Each time the program cycles through the for loop, it creates an instance of the mcSnowflake and adds it to the display list.

var _Particle:mcSnowflake;

_Particle = new mcSnowflake();

Let's assume that we want to make it rain and we have created a movie clip that we named mcRain. To use this movie clip, we just replace the "mcSnowflake" with "mcRain" in two lines of code in our program.

var _Particle:mcRain;

_Particle = new mcRain();

Test your movie. Save your movie as particle_system.fla.

Now, let's see if it still works correctly when we change the value of the variables. Go to the Properties panel and change the size of the stage to 800 x 600. Next, go back to the Actions panel and change the value of the stageWidth to 800 and the stageHeigth to 600. Change the size of the particles to 30, number of particles to 50 and maximum movement in pixels to 20. Test your movie again and it should still work.

And we have finally come to the end of this series. You have a modular particle system code that you can use as a starting point to build any Flash project that needs a particle system. Just import a graphic or draw an object, convert it to a movie clip symbol and give it a unique name. Then, in the Actions panel, plug in the values for the variables and rename the mcSnowflake movie clip. You can now tell everyone that you have created your own particle system in Flash CS3. Here is the finished code.

← Back

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 © 2021 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.