View comments | RSS feed

Controlling time intervals

When you develop applications using Adobe Flash CS3 Professional, you have access to the timeline, which provides a steady, frame-by-frame progression through your application. In pure ActionScript projects, however, you must rely on other timing mechanisms.

Subtopics

Loops versus timers
The Timer class
Timing functions in the flash.utils package

Loops versus timers

In some programming languages, you must devise your own timing schemes using loop statements like for or do..while.

Loop statements generally execute as fast as the local machine allows, which means that the application runs faster on some machines and slower on others. If your application needs a consistent timing interval, you need to tie it to an actual calendar or clock time. Many applications, such as games, animations, and real-time controllers, need regular, time-driven ticking mechanisms that are consistent from machine to machine.

The ActionScript 3.0 Timer class provides a powerful solution. Using the ActionScript 3.0 event model, the Timer class dispatches timer events whenever a specified time interval is reached.

The Timer class

The preferred way to handle timing functions in ActionScript 3.0 is to use the Timer class (flash.utils.Timer), which can be used to dispatch events whenever an interval is reached.

To start a timer, you first create an instance of the Timer class, telling it how often to generate a timer event and how many times to do so before stopping.

For example, the following code creates a Timer instance that dispatches an event every second and continues for 60 seconds:

var oneMinuteTimer:Timer = new Timer(1000, 60);

The Timer object dispatches a TimerEvent object each time the given interval is reached. A TimerEvent object's event type is timer (defined by the constant TimerEvent.TIMER). A TimerEvent object contains the same properties as a standard Event object.

If the Timer instance is set to a fixed number of intervals, it will also dispatch a timerComplete event (defined by the constant TimerEvent.TIMER_COMPLETE) when it reaches the final interval.

Here is a small sample application showing the Timer class in action:

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

    public class ShortTimer extends Sprite
    {
        public function ShortTimer() 
        {
            // creates a new five-second Timer
            var minuteTimer:Timer = new Timer(1000, 5);
            
            // designates listeners for the interval and completion events
            minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
            minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
            
            // starts the timer ticking
            minuteTimer.start();
        }

        public function onTick(event:TimerEvent):void 
        {
            // displays the tick count so far
            // The target of this event is the Timer instance itself.
            trace("tick " + event.target.currentCount);
        }

        public function onTimerComplete(event:TimerEvent):void
        {
            trace("Time's Up!");
        }
    }
}

When the ShortTimer class is created, it creates a Timer instance that will tick once per second for five seconds. Then it adds two listeners to the timer: one that listens to each tick, and one that listens for the timerComplete event.

Next, it starts the timer ticking, and from that point forward, the onTick() method executes at one-second intervals.

The onTick() method simply displays the current tick count. After five seconds have passed, the onTimerComplete() method executes, telling you that the time is up.

When you run this sample, you should see the following lines appear in your console or trace window at the rate of one line per second:

tick 1
tick 2
tick 3
tick 4
tick 5
Time's Up!

Timing functions in the flash.utils package

ActionScript 3.0 contains a number of timing functions similar to those that were available in ActionScript 2.0. These functions are provided as package-level functions in the flash.utils package, and they operate just as they did in ActionScript 2.0.

Function

Description

clearInterval(id:uint):void

Cancels a specified setInterval() call.

clearTimeout(id:uint):void

Cancels a specified setTimeout() call.

getTimer():int

Returns the number of milliseconds that have elapsed since Adobe Flash Player was initialized.

setInterval(closure:Function, delay:Number, ... arguments):uint

Runs a function at a specified interval (in milliseconds).

setTimeout(closure:Function, delay:Number, ... arguments):uint

Runs a specified function after a specified delay (in milliseconds).

These functions remain in ActionScript 3.0 for backward compatibility. Adobe does not recommend that you use them in new ActionScript 3.0 applications. In general, it is easier and more efficient to use the Timer class in your applications.


Flash CS3


Comments


AMPO webdesign&multimedia said on May 22, 2007 at 2:57 AM :
What about updateAfterEvent, does it work with Timer object in AS3? In AS2 you can use the updateAfterEvent together with intervals to create smooth script-driven animations independent of the fps (frames per second) set in the swf file. Is it still possible to do such fps-independent script-driven animations in AS3?
AMPO webdesign&multimedia said on May 22, 2007 at 12:03 PM :
Well, I found the answer to my question.

1. The Timer object in AS3 dispatches the TimeEvent.TIMER event - this behavior is similar to an interval in AS2 as it can launch a function.
2. You create an event listener that listens to the TimeEvent.TIMER event. Register it with the addEventListener method of your Timer object. That event listener can update the animation content every time it is called by the event.
3. When the animation content is updated, you call the TimerEvent.updateAfterEvent() method, in the very same listener function that updated the animation. This will refresh the stage independently of the fps, showing the updated animation state.

This way you can achieve animation of an element far smoother (i.e. more frequently refreshed) than the set fps would allow, using the new Timer object.
No screen name said on Aug 1, 2008 at 7:30 AM :
It would be nice if the docs pointed out that the timer event overshoots it's intervals if the interval time is very small, around 10 milliseconds. I had an issue where I was using a timer object to keep track of my time, and display it on the screen. It seem that time was "slowing" down every time I ran my fla.
Joe ... Ward said on Aug 8, 2008 at 11:09 AM :
When using extremely short time intervals (<20-30 milliseconds), your timer is competing for attention with other function calls and even itself. The runtime may not be able to call the timer event handler at the exact instant the event is scheduled to be generated.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000074.html