• Understanding how Timer works in actionscript 3.0

    by  • November 13, 2011 • Titbits

    Before getting into how timer works, lets take a look on why do we need timers. In developing games sometimes you have to carry out  some tasks periodically e.g.

    • You want to display a timer running for a player in a turn based game.
    • Every 15 minutes you want to update hunger level of your character.
    • If you are developing a Roulette game you might wanna have a countdown saying betting closes in 15 seconds, or if you are  are making Bingo you wanna show buying ends in 30 seconds.
    In a life cycle of any game you have to encounter time based events several times. Actionscript 3.0 has provided a very neat class to handle all your problems and make your life very easy. Now that we know how important a Timer Class is, lets look into how exactly it works.
    Timer Class has some public properties and public methods that helps you handle your periodic tasks.

    Public Properties

    • currentCount: The total number of times the timer has fired since it started at zero.
    • delay: The delay, in milliseconds, between timer events.
    • repeatCount: The total number of times the timer is set to run.
    • running: The timer’s current state; true if the timer is running, otherwise false.
    Public Methods
    • Timer: Constructs a new Timer object with the specified delay and repeatCount states.
    • start: Starts the timer, if it is not already running.
    • stop: Stops the timer.
    • reset: Stops the timer, if it is running, and sets the currentCount property back to 0, like the reset button of a stopwatch.
    • timer: Dispatched whenever a Timer object reaches an interval specified according to the Timer.delay property.
    • timerComplete: Dispatched whenever it has completed the number of requests set by Timer.repeatCount.


    import flash.utils.Timer;
    import flash.events.TimerEvent;
    var timer:Timer;
    timer = new Timer(1000, 5);
    timer.addEventListener(TimerEvent.TIMER, TimerTask);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerCompleteTask);
    function TimerTask(e:TimerEvent) {
    function TimerCompleteTask(e:TimerEvent) {
    	trace("the end");

    Line 1 and 2 imports the Timer and TimerEvent classes. You must import the required classes before using them.

    Line 4 declares the timer variable of type Timer.

    Line 5 creates a new Timer object with delay of 1000 milliseconds and 5 as repeatCount.

    Line 6 adds a listener function that gets executed evertime the timer even is thrown, here it is 1000 milliseconds.

    Line 7 adds a listener function that gets executed when the Timer finishes.

    Line 8 starts the timer.

    TimerTask and TimerCompleteTask are the listener functions for the timer event and timerComplete event respectively.


    An accomplished Game Developer with 4 years’ experience covering all aspects of the game development life-cycle including designing, development, testing and maintenance. Have extensive experience in flash based web, desktop and mobile games and other rich internet applications like small websites, software prototypes, portfolios and many more.