under construction - send comments!

Creating a timer

One of the first things that comes up when creating prototypes is the ability to have events happen after a number of seconds or at a specified time. When we’ve understood a couple of the functions Actionscript has to use for creating timers, we’ll look at a couple different ways to use them.

You can do this using the timeline, but let’s look at two functions that make this process simple and easy to implement into any project without having to rely on the timeline: getTimer and setInterval.


getTimer

There is one very simple way to access the time since our Flash file started running: the getTimer function. getTimer returns the exact number of milliseconds Flash has been running the current file. If we put the following code in our keyframe, the output panel display a running total of this number.

NOTE: Do not place getTimer in loops as a way to output the number every millisecond, this will crash Flash! Instead, remember that the function is continuing to run in the background and ask getTimer only when you are going to show the number (every second at least).

getTimer can be extremely helpful for creating very basic timers, but they have one major flaw: the number returned is linked to when Flash started playing the file. What if we want a start screen? Or a “start” button? In these cases we create a variable that saves the current time, and subtract that amount from the number from getTimer. This way if we click a button 5 seconds after the Flash file started, we save that number in a variable and subtract that number from getTimer.

step-2.gif

1 | 2 | 3



under construction - send comments!