Timer Event
I know that I have not done specific topics, but I think the Timer event is a good thing to have a firm grasp of, as far as ActionScript is concerned.
A Timer Event is like a ticking time bomb
Remember that!
A timer event is set for an amount of time, and when that time is up, BOOM! The function or whatever starts functioning. Now, back to the concept of an actual movie. A Timer Event is like the countdown the director gives for a cue. Like, “3...2...1... action!”
Let’s see what Orielly says:
-------------------------
An alternative to using enter frame events to trigger actions on a recurring basis is to use time-based events. Although it’s among the most straightforward options, using the enter frame event exclusively for this purpose has disadvantages.
On the other hand, time-based events are measured in milliseconds and, therefore, can fire more quickly. Further, time-based events don’t vary from scenario to scenario, so they are more reliable and consistent.
----------------------------
Now, guess what the good news is! A timer is a type of role a variable can play!
Written like this:
var tickingTimerBomb:Timer = new Timer();
The way you set the timer is like this:
var tickingTimerBomb:Timer = new Timer(1000);
That means you have 1000... uh, what?
Sorry, I forgot to tell you, the timer is measured in milliseconds.A millisecond is one thousandth of a second. So, the timer above is set to one second. The reason they did this is so that things can happen quicker (this is a flash movie, not a feature film).
All Timers have a method called ‘start()’.
This is how you start the timer:
tickingTimerBomb.start();
Other timer methods are:
stop();
reset();
I guess you could look at a timer as a stop watch.
Now, the timer can be started, but how does it trigger something to happen? Well, remember Event Listeners?
You have to give your timer actor(variable) some ears. And with those ears it listens to an Event which is a TimerEvent, the same way a variable can listen to a MouseEvent. And it listens to the TIMER property of the timer. And when this happens, it triggers a function. I will give you a full example:
EX1:
var bombTimer:Timer = new Timer(1000);
bombTimer.addEventListener(TimerEvent.TIMER, gotoAndBlowUpBomb);
bombTimer.start();
function gotoAndBlowUpBomb(evt:TimerEvent):void
{
bomb.blowUP();
//not real code, yet
}
Timers don’t start automatically, like intervals or timeouts (I know, I haven‘t made a tutorial for these things yet),that is why I had to do the ‘bombTimer.start();’ thing.
Here is another example:
var stopWatch:Timer=new Timer(4000);
//stopWatch set to 4 seconds
var runner1:Object=new Object();
var horn:Object=new Object();
stopWatch.start();
stopWatch.addEventListener(TimerEvent.TIMER,race)
function race(evt:TimerEvent):void
{
horn.blast();
//blast isn’t a method of horn, yet
runner1.run();
//run() isn’t a method of runner1, yet
}
So, runner1 waits for the horn to blast, which is waiting for 4 seconds, which is timed using a stopWatch. When the 4 seconds are up, the horn blasts and runner1 runs. Remember that we had to start the stopWatch, or we had to turn it on, since it is just an actor that is a Timer that is set to 4 seconds. Know that the stopWatch is the thing that has to listen for the Event and when the stopWatch hears the event other things happen; the horn blasts and the runner runs.
Let’s see what Oriely says
--------------------------
Frame events are not triggered by user input, the way mouse and keyboard events are. Instead, they occur naturally as the Flash file plays. Each time the playhead enters a frame, a frame script is executed. This means that frame scripts execute only once for the life of the frame, making them an excellent location for seldom-executed tasks, such as initializations. In other words, fora frame script to execute more than once, the playhead must leave the frame and return—either because of an ActionScript navigation instruction, or a playback loop that returns the playhead to frame 1 when it reaches the end of the time line.
----------------------------