logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
European Travel
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel


dailyclick
All times in EST

Low Carb: 8:00 PM

Full Schedule
g
g Flash and Animation Site

BellaOnline's Flash and Animation Editor

g

More Math.random() Method in Particle System


In our last tutorial, we completed the snowfall animation for our Flash CS3 particle system. First we created and animated one snowflake. Then we created many animated snowflakes to complete the snowfall animation. However, when we tested the movie, we weren't very happy with the results because the snowflakes seem to be missing from the bottom left corner of the stage.

What can we do to adjust our animation? As we have it coded now, a snowflake can begin its life anywhere along the top of the stage at a position between 0 and 510 on the X axis. Then this snowflake makes its way downward in an angular direction towards the right. It appears that it is this angular direction that is causing the snowflakes to be missing from the bottom left corner of the stage. We could take out the code that creates the angular movement and our snowflakes will just fall straight down the stage. If we did this, the snowfall would cover the entire stage but we would loose the illusion of wind that the angular movement simulates.

Let's assume that we want to keep this illusion of wind and therefore we need to keep the angular movement. As we mentioned above, our snowflakes can begin anywhere along the top of the stage. However, even the snowflakes that begin falling from the very left of the stage (at the 0 position on the X axis) will land at the bottom center of the stage, leaving the left area empty. How can we change our code so that some of the snowflakes enter the stage at a lower point along the vertical Y axis and, therefore, fall across the left corner of the stage. Without going into too much math for this beginner's tutorial, we need to start creating snowflakes outside of the stage area on the left. Although these snowflakes will be invisible when they begin their life, they will fall at least half of the way down the vertical Y axis before they enter the stage and become visible. By doing this, we insure these snowflakes will fall across the lower left corner of the stage.

Right now, we are generating a random number for the_Snowflake.x property. Remember that it is the _Snowflake.x property which controls the initial X position for our snowflakes. This initial position can be any number between 0 and 510 along the X axis.

_Snowflake.x = Math.random() * 510;

If we tell Flash to use a number between -350 and 510, the snowflakes that begin with a negative number will be the snowflakes that eventually fall across the lower left corner of the stage. We can do this by adding a -350 to the random number that is generated by the Math.random() method.

_Snowflake.x = -350 + Math.random() * 510;

Change this line of code and test your movie. What happened? We fixed our problem and caused another. Now the snowflakes are missing from the top right corner of the stage. Why? The largest number for the_Snowflake.x property (the position of the snowflake along the X axis) will be 160 (510 350). The rest of the X axis between 161 and 550 does not have any snowflakes and that's why the top right corner is empty of snow. How can we get the number for the_Snowflake.x property to range between 0 and 550? We can extend the range of the random number to between 0 and 900 (550 + 350).

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

Change the code and test again. It works. Are we done? No, if you let this animation run for some time, you will notice that it begins to task your computer's resources. We need to make some changes to the ActionScript to make it more efficient.

Note - This animation code is not complete until you reach the end of the tutorial series. Do not use this incomplete code in your Flash projects.





Add More+Math%2Erandom%28%29+Method+in+Particle+System to Twitter Add More+Math%2Erandom%28%29+Method+in+Particle+System to Facebook Add More+Math%2Erandom%28%29+Method+in+Particle+System to MySpace Add More+Math%2Erandom%28%29+Method+in+Particle+System to Del.icio.us Digg More+Math%2Erandom%28%29+Method+in+Particle+System Add More+Math%2Erandom%28%29+Method+in+Particle+System to Yahoo My Web Add More+Math%2Erandom%28%29+Method+in+Particle+System to Google Bookmarks Add More+Math%2Erandom%28%29+Method+in+Particle+System to Stumbleupon Add More+Math%2Erandom%28%29+Method+in+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


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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


g features
Motion Workspace - Checkbox, Dials and More

Motion Workspace - Menu, Icons and Sliders

The Motion Toolbar Tools

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor