ENTER_FRAME Event Listener - 2

ENTER_FRAME Event Listener - 2
Easy enough, but we still need to call the function to make it work. We will do this with a new event listener called the ENTER_FRAME event listener. This event listener will "listen" for each time our movie enters a new frame and call the snowfall function, the event handler, each time. Therefore, because our Frame Rate is set to the default 12 frames per second in the Document Properties, the snowfall function will be triggered 12 times per second.

So, we need to attach this event listener to something. Because we won't be listening for a mouse event as in our previous tutorial about event listeners, we can add or attach the event listener to the stage.

stage.addEventListener();

We need to identify the event that we want to listen for as an ENTER_FRAME event.

stage.addEventListener(Event.ENTER_FRAME);

And we need to identify the snowfall function as our event handler.

stage.addEventListener(Event.ENTER_FRAME, snowfall);

Finally, we need to coordinate the snowfall function with the event.

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


Test your movie and you will finally see our snowflake fall down the stage.

Here is the code that we have written.

stage.addEventListener(Event.ENTER_FRAME, snowfall);

var _Snowflake:mcSnowflake;

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

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


Save your snow scene as SnowScene4.fla.

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

Back





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








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