Timer Events
Part 1
Download and open alienTime.fla
Create a new layer and name it actions:
Select frame 1 of the actions layer and open the actions panel and type:
var count:Number=0; var repeat:Number =5;
var timer:Timer = new Timer(100, repeat);
The first thing we do is declare two variables a count that will be used for display purposes and a repeat which is used to indicate how many times the counter can be called, in this instance the variable is set to 5. The next line is the timer function the first number in the parentheses is the time delay in millisecond for each timer event being dispatched. The repeat is optional if left blank it will be infinite or until removed.
Underneath this code add the eventListeners and functions.
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onCompTime);
timer.addEventListener(TimerEvent.TIMER, onTime);
timer.start();
function onTime (evt:TimerEvent):void{
alien.rotation+=10;
timeText.text=String(count);
count++;
repeat--;
}
function onCompTime (evt:TimerEvent):void{
alien.alpha = .1
timeText.text="alien is dead";
}
There are two event listeners attached to the timer one for each time the timer is called and one for when it is complete. The onTime function rotates the alien movie clip evertime the function is called and displays and updates the value of count on the stage, also repeat is reduced by 1.
When the timer is complete the alpha value is changed to 10% and the new text is displayed on the screen.
Excercise 1
Now see if you can remove the eventListeners from within the onCompTime function, this is essential for garbage collection! This will ensure that no future errors arise from evenetListeners still running.
Excercise 2
Download rocketTime.fla.
You are required to builder a timer that counts down from 10 seconds displaying this in a text box on the screen. After 10 seconds the count down will display 0 and the animation of the rocket taking off will play.
