Here's a little fun exercise that we can do with math: we'll use the random number function in Flash. We will create a single snowflake, then write code that will randomly determine the number, placement, speed, rotation, and drift of the flakes (see example). This exercise is based on one found in SAMS Teach Yourself Flash MX Actionscript in 24 Hours by Gary Rozenzweig.
onClipEvent (load) {
this._x = Math.random()*550;
this._y = Math.random()*400;
speed = Math.random()*3+3;
drift = Math.random()*2-1;
rotate = Math.random()*6-3;
}
This first bit of code establishes a series of variables and fills them with random numbers when the movie clip (snowflake) first loads. First, a random number from 1 to 550 (the width of the stage in pixels) is generated and assigned to this._x, the horizontal position of the snowflake movie clip. Next, the vertical position (this._y) is also given a random number from 1 to 400 (the height of the stage). Taken together, these place the snowflake in a random spot somewhere on screen.
Next three new variables are created, speed, drift, & rotate, and each is assigned a random number. Math.random()*3+3 generates a number from 3 to 6 (random()*3 by itself would generate a number from 0 to 3; here we then add 3 to the product). Math.random()*2-1 generates a number from -1 to 1; and the final line generates a random number from -3 to 3.
onClipEvent(enterFrame) {
this._y += speed;
this._x += drift;
this._rotation += rotate;
The onClipEvent(enterFrame) handler is triggered whenever the playhead enters the frame. Since the movie is one frame long, the playhead is perpetually in the frame so what we have in effect an infinite loop; everything inside this handler will continue until stopped.
This portion of the script modifies the current position of the snowflake by adding the speed variable to the y and drift variable to the x. Since the speed variable is a number from 3 to 6, the snowflake is moved down the screen that many pixels each time the movie clip loops. Likewise, it is moved horizontally -1 pixel, 0 pixel, or +1 pixel. The angle of rotation is also altered up to 3º in either direction.
Note that we did not close the onClipEvent handler yet, that's because there's more to come:
if (this._y > 400) this._y = 0;
if (this._x < 0) this._x = 550;
if (this._x > 550) this._x = 0;
}
These three lines reset the position of our snowflake should it stray too close to the edge of the stage. If it's y position exceeds 400 (the bottom edge of the stage) it is repositioned at the top (0). Likewise, if a snowflake runs off the side of the stage, it will reappear on the other side and continue on its journey. Pretty slick, eh?
for(var i=0; i<50; i++) {
snowflake_in.duplicateMovieClip("snowflake_in" +i, i);
}
There's a lot packed into this simple little script. First, a variable (i) is created and assigned a starting value of zero. This is put in a for/next loop (even though we don't have to write the word "next" it's implied). This loop checks the value of i and if it's less than 50 (i<50), it adds one to the value (i++). Then the snowflake instance is duplicated and given a new instance name: snowflake_in plus the value of i (so it would be snowflake_in1, snowflake_in2, etc.). In addition, each movie clip is assigned its own level (z position on stage); the levels correspond to the instance number (so snowflake_in5 will be on level 5, etc.). Finally, when i hits 50, no more snowflakes are generated; the ones appearing at the top of the stage are simply the ones that fell off the bottom and were reset.
10 pts |
Went above & beyond and made something special |
9 pts |
Above and beyond the basics. |
8 pts |
Did the basics; it works, but it's nothing fancy. |
7 pts |
Something's not working right. |
0-6 pts |
Poor showing; mostly incomplete or full of errors. |