AS3 Kitchen – Timer and ENTER_FRAME

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:

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);
}
}

}

AS3 Timer class test:
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 

3 thoughts on “AS3 Kitchen – Timer and ENTER_FRAME”

  1. 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

  2. @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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>