What are Timer Events?
Timer events allow us to execute code after a specific amount of time. In the example below we have the Flash Essential logo on the stage which has been converted to a movie-clip and been given the instance name "flashEssential". The timer event will rotate the logo clockwise every 2 seconds.
Example;
Let's go through the code;
We start out by creating a Timer variable that we've called flashTimer. Inside the parameters the timer class can have 2 arguments delay and repeatCount;
"delay" specifies the delay, which is fired in milliseconds.
"repeatCount" determines how many times you want the event to fire and is optional, if left blank the Timer will fire infinite amount of times. Below we have left out repeatCount so the timer will execute every 2 seconds infinitely.
flashTimer.addEventListener(TimerEvent.TIMER, onTime);
flashTimer.start()
function onTime(evt:TimerEvent):void
{
flashEssential.rotation += 5;
}
We need to give our flashTimer an event listener. The event listener needs to be a TimerEvent so we access the TimerEvent class inside the parameters and declare it a TIMER event. We finally give our event listener unique name of our own choice in this case its onTime.
flashTimer.addEventListener(TimerEvent.TIMER, onTime);
flashTimer.start()
function onTime(evt:TimerEvent):void
{
flashEssential.rotation += 5;
}
When using timer events you need to tell it to start as they do not start automatically. We do this by giving our flashTimer the start method. You can also use stop() and reset() methods.
flashTimer.addEventListener(TimerEvent.TIMER, onTime);
flashTimer.start()
function onTime(evt:TimerEvent):void
{
flashEssential.rotation += 5;
}
We want our timer to fire off some code every couple of seconds so we create a function to do so. We create a function for our onTime event listener and declare our arguments inside the parameters as evt:TimerEvent as it's a TimerEvent. Inside our parentheses we want our logo to rotate by 5px every time the timer event fires so we give our flashEssential logo the rotation method and tell it to execute by 5 each time.
flashTimer.addEventListener(TimerEvent.TIMER, onTime);
flashTimer.start()
function onTime(evt:TimerEvent):void
{
flashEssential.rotation += 5;
}
This post is tagged AS3 Guide, TimerEvents
Share This Post
Jobs


























No Comments
Leave a Reply