Friday, December 20, 2013

Creating a timer with the Actionscript onEnterFrame

For those who are Actionscript-savvy, the "onEnterFrame" Event is a familiar term. It is in fact an elegant way of handling the animations in the game.

This post, is particularly a demo of how do i use the AS3 "onEnterFrame" to replace the class Timer (which is in the package flash.utils). You might think that this is redundant. But i really favor the way the "onEnterFrame" perform this with ease, while you can implement the other animations simultaneously.

Here's a snippet of the "onEnterFrame" function. What it does is that, the object is able to perform an animation A on each frame, while performing another animation B at an interval of one second (adjustable).

private var timePassed:Number = 0;
private var startInt:int = 1;
private function onEnterFrame(e:EnterFrameEvent):void
{
     timePassed += e.passedTime;
     if(startInt < 10)
    {

         //doAnimationA();
         if(Math.floor(timePassed) == startInt)
        {
             startInt++;

             //doAnimationB();
        }
     }
     else
    {

        this.removeEventListeners(Event.ENTER_FRAME); //clean the event
     }
 }

One thing to note is that, the data type of the timePassed has to be "Number", but not "integer". In the code, after passing 10 seconds, the enterframe event is removed.

No comments:

Post a Comment