Skip to content
November 26, 2010 / edeustace

A simple countdown timer for Actionscript

I was looking around the web for an actionscript example of a countdown class. I found a few flash based examples, but here’s one that is an actionscript class:

package com.ee.example.timing
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    [Event(type="flash.events.Event", name="countdownComplete")]
    public class Countdown extends EventDispatcher
    {
        public static const COUNTDOWN_COMPLETE:String = "countdownComplete";

        public static const COUNTDOWN_UPDATE:String = "countdownUpdate";

        public static const MINUTES:int = SECONDS * 60;

        public static const SECONDS:int = 1000;

        public var unit:int = SECONDS;

        private var _startValue:int;

        private var isCounting:Boolean;

        private var timer:Timer;

        public function start():void
        {
            timer = new Timer(unit, _startValue);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
            isCounting = true;
        }

        public function set startValue(value:int):void
        {
            if (isCounting)
            {
                return;
            }

            _startValue = Math.max(value, 1);
            dispatchEvent(new Event(COUNTDOWN_UPDATE));
        }


        public function stop():void
        {
            timer.stop();
            isCounting = false;
            dispatchEvent(new Event(COUNTDOWN_UPDATE));
            removeTimer();
        }

        [Bindable("countdownUpdate")]
        public function get value():int
        {
            if (isCounting)
            {
                return _startValue - timer.currentCount;
            }
            return _startValue;
        }

        private function onTimer(event:TimerEvent):void
        {

            if (value == 0)
            {
                dispatchEvent(new Event(COUNTDOWN_COMPLETE));
                isCounting = false;
                removeTimer();
            }
            dispatchEvent(new Event(COUNTDOWN_UPDATE));
        }

        private function removeTimer():void
        {
            timer.removeEventListener(TimerEvent.TIMER, onTimer);
            timer = null;
        }
    }
}

Usage:

//...
var countdown : Countdown = new Countdown();
countdown.startValue = 12;
countdown.addEventListener(Countdown.COUNTDOWN_COMPLETE, onCountdownTwoComplete);
//...

private function onCountdownTwoComplete(event:Event):void
{
       Alert.show("Countdown 2 complete");
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: