O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Animating with the TimerEvent.TIMER Event

In the previous section, we learned how to use Event.ENTER_FRAME to create animations synchronized with the Flash runtime's frame rate. In this section, we'll see how to synchronize animations with an arbitrary time interval, specified using the flash.utils.Timer class.

The Timer class is a general utility class for executing code after a specified time interval. Each Timer object dispatches TimerEvent.TIMER events at a programmer-specified frequency. Functions wishing to be executed at that frequency register with the Timer object for TimerEvent.TIMER events.

Tip

The Timer class does not guarantee the frequency with which its listener functions are executed. If the system or the Flash runtime is busy at the time a Timer is scheduled to execute its listener functions, the execution will be delayed. For information on accounting for these delays in an animation, see the section "Velocity-Based Animation."

The general steps required to use the Timer class are as follows:

  1. Create a new Timer object:

    var timer:Timer = new Timer();
  2. Set the frequency with which TimerEvent.TIMER events should be triggered, in milliseconds. For example, the following code sets the frequency to 100 milliseconds (10 TimerEvent.TIMER event dispatches per second):

    timer.delay = 100;
  3. Set the total number of TimerEvent.TIMER events to be triggered. For example, the following code tells timer to dispatch a total of five TimerEvent.TIMER events.

    timer.repeatCount = 5;

    The special value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required