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:
[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]
3 thoughts on “AS3 Kitchen – Timer and ENTER_FRAME”
April 28, 2012 at 12:01
On top of the site, you’we got a link called Contact. :)
October 26, 2013 at 21:52
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
January 23, 2014 at 13:41
@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();
}