g Text Version
Beauty & Self
Books & Music
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
News & Politics
Religion & Spirituality
Travel & Culture
TV & Movies

Bored? Games!
Take a Quiz
Rate My Photo

Natural Living
Folklore and Mythology
Distance Learning

All times in EST

Full Schedule
g Flash and Animation Site

BellaOnline's Flash and Animation Editor


Write Modular ActionScript 3 Code in Flash CS3 by Using Variables in a Particle System

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 (> 370 ||> 550)

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

if (> stageHeight ||> 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. = - (stageWidth * .60) + Math.random() * (stageWidth * 1.60);

Test your movie again and it will still work.

← Back |Next →

Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Twitter Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Facebook Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to MySpace Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Digg Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Yahoo My Web Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Google Bookmarks Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Stumbleupon Add Write+Modular+ActionScript+3+Code+in+Flash+CS3+by+Using+Variables+in+a+Particle+System+ to Reddit


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

For FREE email updates, subscribe to the Flash and Animation Newsletter

Past Issues

Printer Friendly
tell friend
Tell a Friend
Email Editor

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


g features
Subtle Animation Website Trends

Wix Editor - Background Video

Video on Wix Websites

Archives | Site Map


Past Issues

Note: BellaOnline uses cookies to help provide a consistent user experience. Our advertisers may use cookies to help customize ads. Please contact us with any question about our cookie use.

Summertime Foods
Corn on the Cob
Burgers on the Grill
Apple Pie


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2018 Minerva WebWorks LLC. All rights reserved.

BellaOnline Editor