ENTER_FRAME Event Listener

ENTER_FRAME Event Listener
In this FlashR CS3 tutorial, we will continue to work on and improve our particle system snow scene. We will concentrate on one snowflake and program that flake to fall down the stage. We will do this by progressively increasing the value of the Y position for this snowflake movie clip and we will do this with a new event listener called the ENTER_FRAME event listener.

You might be asking why increasing the value of the Y position will cause the flake to fall instead of rise. Remember, that at the upper left corner of the stage, the X and Y positions are both zero. As we move horizontally along the X axis, the numbers increase and as we move down the Y axis the number also increase. So for our snow scene, the stage is 400 pixels high and the bottom most Y position is 400.

Our first step is to change the code that generates several instances of the mcSnowflake movie clip from the Library onto the stage. If you remember, we used a for loop to do this.

var _Snowflake:mcSnowflake;

for (var i:Number = 0; i < 10; i++)
{
_Snowflake = new mcSnowflake();
addChild(_Snowflake);
_Snowflake.x = Math.random() * 510;
_Snowflake.y = Math.random() * 40;
}


But we only need one instance on the stage for this tutorial. So, let's remove the for loop from the above code. We do not need to change the Math.random() code at this time. Below is the code that is left.

var _Snowflake:mcSnowflake;

_Snowflake = new mcSnowflake();
addChild(_Snowflake);
_Snowflake.x = Math.random() * 510;
_Snowflake.y = Math.random() * 40;


Test your movie several times and you will get only one snowflake each time at a random position.

Now we can add the code that will create the snowfall animation for the movie clip. We will do this with a function that we will call "snowfall". The function will continuously increase the Y position by 10 pixels.

function snowfall():void
{
_Snowflake.y += 10;
}


Continue





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








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