Jump to content

How to Use Timer Events with ActionScript 3.0

0
  chco's Photo
Posted Dec 16 2010 06:49 AM

An alternative to using enter frame events to trigger actions on a recurring basis is to use time-based events. This excerpt from Learning ActionScript 3.0, Second Edition shows you how to use them in ActionScript 3.0.
Although it’s among the most straightforward options, using the enter frame event exclusively for this purpose has disadvantages. For example, Flash Player can reliably achieve only moderate frame rates—somewhere between the default 24 frames per second (fps) and perhaps 60 or so fps on the high end. Your mileage may vary, but that’s fairly accurate when averaging the CPU population at large. More importantly, the rate at which the enter frame fires is not always consistent.

On the other hand, time-based events are measured in milliseconds and can therefore sometimes fire more quickly. Further, time-based events don’t vary as much from scenario to scenario, so they are more reliable and consistent.

Previous versions of ActionScript used the setInterval() method for ongoing recurring events and the setTimeout() method for finitely recurring events. ActionScript 3.0 wraps up these approaches neatly behind the scenes of the new Timer class, simplifying the process of using timers.

The first step in using the Timer class is to create an instance of the class. Fortunately, creating instances in ActionScript 3.0 is very consistent, so this may look familiar. A variable is declared and typed using a data type that matches the class being instantiated. The new keyword creates a new instance of the class and that instance is stored in the variable:

var timer:Timer = new Timer(delay, repeatCount);


In this case, the class constructor can take two arguments. The first is mandatory and specifies the delay, in milliseconds, before the timer event is fired. The second is optional and is the number of times the event fires. Omitting the second argument will cause the event to fire indefinitely, each time after the specified delay. Using a positive value, such as 3, will cause the event to fire that finite number of times (again, after the specified delay).

Note: Frame and timer loops are often an attractive alternative to for loops because they allow additional updates to occur throughout the file. A code loop, such as a for loop, is one of the most processor-intensive structures and will execute only the code inside the loop until the loop is finished. This means that animation, sound, or video updates, for example, will all be halted while the loop is working.

In the sample timer_events.fla in the accompanying source code, the timer event (consistently specified as the constant TIMER in the TimerEvent class), occurs every second (or, in Timer units, every 1,000 milliseconds) and calls a function that increases the rotation of a hand nested inside a watch movie clip. The rotation increases 6 degrees every second, making one full 360-degree journey in 60 seconds.

1    var timer:Timer = new Timer(1000);
2    timer.addEventListener(TimerEvent.TIMER, onTimer);
3    timer.start();
4
5    function onTimer(evt:TimerEvent):void {
6        watch.hand.rotation += 6;
7    }


One important thing to note is line 3. The timer you instantiate does not start automatically. This gives you greater flexibility and control over your timer events. You can also stop the timer using the stop() method, and reset the timer using the reset() method. The latter stops the timer and also resets the repeat count to zero. For example, if you specified that the timer call a function five times, but reset it after the third call, the timer would begin counting again from zero rather than picking up from three at the point when it was reset. The image below shows the watch used in timer_events.fla.

Use of the timer event in a stopwatch

Attached Image


Learning ActionScript 3.0

Learn more about this topic from Learning ActionScript 3.0, 2nd Edition.

If you're new to ActionScript 3.0, or want to enhance your skill set, this bestselling book is the ideal guide. Designers, developers, and programmers alike will find Learning ActionScript 3.0 invaluable for navigating ActionScript 3.0's learning curve. You'll learn the language by getting a clear look at essential topics such as logic, event handling, displaying content, classes, and much more. Updated for Flash Professional CS5, this revised and expanded edition delivers hands-on exercises and full-color code samples to help you increase your abilities as you progress through the book.

See what you'll learn


Tags:
0 Subscribe


0 Replies