12
one of the most used event properties in flash is probably Event.ENTER_FRAME. it executes as the
flash player prepares to render the next frame to the screen, whether the playback head is moving through
an animation or idling on a single frame.
code assigned to the enter frame property is generally used to update the screen to reflect the current state of the application.
import flash.events.Event; function reportX(e:Event):void { trace(stage.mouseX); } addEventListener(Event.ENTER_FRAME, reportX);
onEnterFrame which occurs once every frame, the Timer class can be
used to schedule a function to be executed after an arbitrary length of time:
import flash.utils.Timer; import flash.events.TimerEvent; function reportX(e:TimerEvent):void { trace(stage.mouseX); } var timer:Timer = new Timer(1000); // milliseconds timer.addEventListener(TimerEvent.TIMER, reportX); timer.start();
Event.ENTER_FRAME
or a TimerEvent.TIMER event listener, we'll have a simple animation:
import flash.display.Shape; import flash.events.Event; var r:Number = 50; var shape:Shape = new Shape(); addChild(shape); shape.graphics.beginFill(0x000000); shape.graphics.drawRect(-r,-r, r+r,r+r); shape.graphics.endFill(); shape.x = stage.stageWidth * .5; shape.y = stage.stageHeight * .5; function onFrame(e:Event):void { shape.rotation++; } addEventListener(Event.ENTER_FRAME, onFrame);or maybe: strand [
swf]
[as]
[fla]
if we base our animation on variables that are changed by the same amount every update event, we tie ourselves to a particular frame rate. this isn't always a problem, but if you've ever played a really old video game on a really new computer, you know that it can be, and the fix is very simple.
by looking at the amount of time elapsed since the last update, we can calculate exactly how far things should travel based on their speed. doing this frees us from any dependency on frame rate—our positioning will always be accurate, regardless of how many times we paint the screen per second.
[as]
[fla]