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

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