Modular ActionScript 3 Code in Flash CS3

Modular ActionScript 3 Code in Flash CS3
Rename the Particles

Our _Snowflake particle needs a more generic name. Let's rename it from "_Snowflake" to "_Particle". Use the Find and Replace to do this and you should get a message that nine changes were made. Test again and it still works.

Find what: _Snowflake
Replace with: _Particle

A Stage of Any Size

Our particle system is written for the default stage size of 550 x 400. Now, we will add a few variables to our program and use them to make it easier to change the stage size to match the needs of any Flash project. We will put these variables at the top of our program. It is a standard practice to put variables at the top and it makes it easy to change the values of these variables as needed. Let's call our variables "stageWidth" and "stageHeight" and give both of them a data type of Number. While we are still in the testing phase, let's keep the default width and height. Add these two lines to the top of the code.

var stageWidth:Number = 550;
var stageHeight:Number = 400;


Now we are ready to use these variables to replace the hard coded settings in our particle system. Let's first work on the line of code that places the new particles in a random position along the Y axis.

_Particle.y = Math.random() * 400;

Replace the 400 with the stageHeight variable.

_Particle.y = Math.random() * stageHeight;

We can use the new variables in the line of code that tests to see if the particle has gone beyond the right edge or bottom of the stage.

if ( event.target.y> 370 || event.target.x> 550)

Replace the 370 with the stageHeight variable and the 550 with the stageWidth variable.

if ( event.target.y> stageHeight || event.target.x> stageWidth)

Changing the code that places the particles in a random position on the X axis will be a little more complicated. Here is what we have now.

_Particle.x = - 350 + Math.random() * 900;

If you remember, we used the -350 to set the initial position of some particles to beyond the left edge of the stage. The 350 is about 60% of the width of the stage. Let's use math and our stageWidth variable to replace -350 with a negative value of 60% of the stageWidth.

_Particle.x = - (stageWidth * .60) + Math.random() * 900;

Now let's work on the second half of the line. The 900 was used to set the initial position of some particles to beyond the right edge of the stage. The 900 is about 1.60% of the width of the stage. So we can replace 900 with the value of 1.60% of the stageWidth.

_Particle.x = - (stageWidth * .60) + Math.random() * (stageWidth * 1.60);

We need to make the same change to the code that recycles the particles back up to the top of the stage.

event.target.x = - (stageWidth * .60) + Math.random() * (stageWidth * 1.60);

Test your movie again and it will still work.

← 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.