• November 27, 2006

    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 

Reader comments:

  1.  

    April 28, 2012 @ 12:01

    seven says:

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

  2.  

    October 26, 2013 @ 21:52

    Umer says:

    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

  3.  

    January 23, 2014 @ 13:41

    seven says:

    @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();

    }

Have your say:

 
This blog is Gravatar friendly. Don't have Gravatar? Get one fast!
 

Proudly running on Word Press, and above all, proudly using Comic Sans.

Nivas.hr © Copyright 2009    All right reserved    Made in Croatia Yeah, we made our own site!Nivas.hr