« AS3 Primer : Packages | Main

Timer Class - ActionScript 3.0

Listen to this article Listen to this article :: Talkr

The Timer class is a new addition to AS 3.0. The Timer class is similar to the setInterval function in AS 2.0 but has some more features added to it. One of such feature is the ability to start and stop the timer when needed.

Timer in AS 3.0 behaves a little different than the setInterval call in AS 2.0. A timer fires a timer event at regular intervals (the interval which is defined when defining the timer). As with the case of setInterval one can specify the time interval in which the event has to be fired.

The Timer class is part of the flash.utils package and has to be imported before one can use it. The declaration goes like this:

import flash.utils.Timer;

As mentioned earlier a Timer has a event associcated with it which is called a TimerEvent. The TimerEvent class is present in the flash.events package and if thie TimerEvent is used, one has to import the event before using it.

import flash.events.TimerEvent;

Now for the syntax of declaring a Timer it goes like this:

var myTimer:Timer = new Timer(delay, iterations);

One primary difference between the setInterval call and using a Timer is that, the Timer has a extra parameter which says how many times the timer has to fire (iterations). If you want the timer to iterate forever then the iteration can be declared as 0. This would keep firing the Timer forever.

To listen to the timer you need to add a listener which listens to the TimerEvent.

myTimer.addEventListener(TimerEvent.TIMER, drawRectangle);

and then start the timer.

myTimer.start();

At this point its also important to note that the getTimer function in AS 2.0 still exists and is in the package flash.utils and can be used as:

flash.utils.getTimer();

Sample code:

package {
     import flash.utils.Timer;
     import flash.utils.getTimer;
     import flash.events.TimerEvent;
     import flash.display.Sprite;
    

     public class TimerExample extends Sprite
     {
          public function TimerExample()
          {
               var myTimer:Timer = new Timer(100, 10);
               myTimer.addEventListener(TimerEvent.TIMER, drawRectangle);
               myTimer.start();
           }
          private function drawRectangle(evt:TimerEvent):void
          {
                trace(flash.utils.getTimer()+ " : " + evt);
          }
      }
}

The other useful properties that are present in the Timer class are:

currentCount - Returns the total number of times the timer has fired since it started at zero.

repeatCount - Returns the total number of times the timer is set to run.

running - Returns true if the timer is running else false.

delay - Returns the delay in milliseconds and can also be set while the timer is running.

The other two methods of the Timer class which the example above doesn't cover are:

stop() - Stops the current running timer.

reset() - Resets the currently running timer and sets the current count to 0.

Another event which is associated with the TimerEvent is:

TimerEvent.TIMER_COMPLETE - This event is fired when the timer is completed and can used as:

myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, drawRectangle);

TrackBack

TrackBack URL for this entry:
http://www.lastashero.com/tutorials/mt-tb.cgi/9

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)