AS3 Kitchen – Timer and ENTER_FRAME

Author: seven November 27, 2006

There is no mc.onEnterFrame in AS3 anymore, but there is something so much better – ENTER_FRAME event! Since pretty much everything in AS3 is event based, that makes perfect sense. There is also a new event based Timer class which replaces darn setInterval. The guru of ActionScripted motion – Keith Peters wrote a great post on subject of common misconceptions in usage.

AS3 EnterFrame event:
[ftf w=”490″ h=”400″]
package {
import flash.display.*;
import flash.events.*;

public class TestEnterFrame extends Sprite
{
public function TestEnterFrame()
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(ev:Event):void
{
trace(“onenterframe:”+ev);
}
}

}
[/ftf]
AS3 Timer class test:
[ftf w=”490″ h=”400″]
package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.getTimer;
import flash.utils.Timer;
public class Tests extends Sprite
{
private var start:Number;
public function Tests()
{
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
start = getTimer();
timer.start();
}
private function onTimer(event:TimerEvent):void
{
trace(getTimer() – start);
start = getTimer();
for(var i:uint = 0; i <1000000; i++) { var j:Number = Math.random(); } trace("elapsed time in function: " + (getTimer() - start)); } } } [/ftf]

Author
seven
CEO/CTO at Nivas®
Neven Jacmenović has been passionately involved with computers since late 80s, the age of Atari and Commodore Amiga. As one of internet industry pioneers in Croatia, since 90s, he has been involved in making of many award winning, innovative and successful online projects. He is an experienced full stack web developer, analyst and system engineer. In his spare time, Neven is transforming retro-futuristic passion into various golang, Adobe Flash and JavaScript/WebGL projects.

    3 thoughts on “AS3 Kitchen – Timer and ENTER_FRAME”

  • On top of the site, you’we got a link called Contact. :)

  • how would I specify interval( time ) in Enter_Frame , like in Timer class you can specify time
    in my application I need to use multiple Enter_Frame with differnet time

  • @Umer all enter frame events are triggering at the same time, defined in FPS of the main Flash movie.

    So eg, if you have set up 35 FPS movie, all enter frames events will in theory dispatch enter frame event 35 times in second. In reality, it depends on complexity of your animations, hardware of the client etc.

    If you need more detailed timings you should use the Timer function to call a method at independent way from enter frame.

    eg:

    var timer:Timer = new Timer(1000, 0);
    timer.addEventListener(TimerEvent.TIMER, timerHandler);
    timer.start();

    private function timerHandler(event:TimerEvent):void
    {
    // do something

    event.updateAfterEvent();

    }

  • Leave a Reply

    Your email address will not be published. Required fields are marked *