LogoLogo
  • Home
  • Projects
  • About
  • Contact

Game Timer Download

Devon O. · January 02, 2010 · Actionscript, Flash · 6 comments
7

Just playing around with an idea for a game timer and thought I’d share the results for anyone who might need something similar.

Basically, this is just an ‘odometer’ style scrolling number thing that counts down from a specified number of seconds to 0. It only allows 2 digits, meaning you can only count down from a max number of 99 seconds, but what do you want for free?

Once you have the .swc (available below) in your library and you’ve imported com.onebyonedesign.utils.timer.CountdownTimer, you’ll have access to these public methods/properties:

  • setTime(time) – call this first – it sets the number of seconds to count down from
  • setColor(color) – sets the color of the scrolling digits
  • start() – starts the timer
  • currentTime – read only property that tells what second the timer’s at. Useful, if you want to change the color at say 5 seconds or whatever.
  • reset() – resets the timer back to the original time set with setTime()
  • toggle() – will pause the timer if it’s running or re-start it, if it’s paused.

it dispatches the two events Event.CHANGE (every second) and Event.COMPLETE (when the timer’s done).

Here’s a quickie example:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package {
 
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.utils.setTimeout;
    import com.onebyonedesign.utils.timer.CountdownTimer;
 
    /**
     * Example usage of CountdownTimer
     * @author Devon O.
     */
 
    [SWF(width='400', height='200', backgroundColor='#FFFFFF', frameRate='40')]
    public class Main extends Sprite {
 
        private var _countdowntimer:CountdownTimer;
        private var _tf:TextField;
 
        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
 
        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
 
            initTimer();
            initText();
        }
 
        private function initTimer():void {
            _countdowntimer = new CountdownTimer();
 
            // place on "whole" pixels for best visual result
            _countdowntimer.x = Math.round(stage.stageWidth * .5 - _countdowntimer.width * .5);
            _countdowntimer.y = Math.round(stage.stageHeight * .5 - _countdowntimer.height * .5);
 
            _countdowntimer.filters = [new DropShadowFilter(1)];
            _countdowntimer.addEventListener(Event.CHANGE, onTimerChange);
            _countdowntimer.addEventListener(Event.COMPLETE, onTimerComplete);
 
            // set the countdown time first
            _countdowntimer.setTime(20);
 
            // change color to dark red
            _countdowntimer.setColor(0x660000);
            addChild(_countdowntimer);
 
            stage.addEventListener(MouseEvent.CLICK, startTimer);
        }
 
        private function startTimer(event:MouseEvent):void {
            stage.removeEventListener(MouseEvent.CLICK, startTimer);
 
            // start the timer
            _countdowntimer.start();
        }
 
        private function onTimerChange(event:Event):void {
            _tf.text = String(_countdowntimer.currentTime);
        }
 
        private function onTimerComplete(event:Event):void {
            _tf.text = "TIMER'S DONE!";
 
            // wait 2 seconds then reset timer
            setTimeout(resetTimer, 2000);
        }
 
        private function resetTimer():void {
            _countdowntimer.reset();
 
            _tf.text = "Click stage to start timer.";
            stage.addEventListener(MouseEvent.CLICK, startTimer);
        }
 
        private function initText():void {
            _tf = new TextField();
            _tf.selectable = false;
            _tf.mouseEnabled = false;
            _tf.defaultTextFormat = new TextFormat("_sans");
            _tf.autoSize = TextFieldAutoSize.LEFT;
            _tf.x = _countdowntimer.x;
            _tf.y = _countdowntimer.y + _countdowntimer.height + 5;
            _tf.text = "Click stage to start timer.";
            addChild(_tf);
        }
    }
}

And that will give you:

Get Adobe Flash player

Download the .swc file here (good for Flash Player 9 up).

Incidentally, it makes use of the great Greensock Tweenlite tweening engine. You won’t need this as it’s included in the .swc file, but I like to give credit where credit’s due.

  Facebook   Pinterest   Twitter   Google+
downloadswctimer
  • Gone Bowling
    June 21, 2009 · 0 comments
    2088
    4
    Read more
  • Feeling Lucky?
    May 29, 2011 · 0 comments
    2516
    4
    Read more
  • FOTB 2010 – Some Closing Thoughts and some Play Time
    October 07, 2010 · 1 comments
    2540
    8
    Read more
6 Comments:
  1. As far as I remember majority of games have “pause” option ;)

    szataniol · January 02, 2010
  2. Always one in the bunch.. :)
    Just updated to include a toggle() method which will pause or unpause the timer…

    Devon O. · January 02, 2010
  3. Very nice Devon! Great work as usual !

    Dave · January 02, 2010
  4. Looking good.
    Only 3 months too late for me…

    Shoom · January 07, 2010
  5. nice work!

    felisan · January 13, 2010
  6. is it possible to use this stuff in flex application

    rabi · October 11, 2010

Leave a Comment! Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Devon O. Wolfgang

AIR | Unity3D | AR/VR

Unity Certified Developer

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development” and “Starling Game Development Essentials”

Reviewer of “The Starling Handbook”

Unity Engineer at Touch Press.

Categories
  • Actionscript (95)
  • AIR (16)
  • Flash (99)
  • Games (7)
  • Liberty (13)
  • Life (53)
  • Shaders (20)
  • Unity3D (21)
Recent Comments
  • MainDepth on Unity Ripple or Shock Wave Effect
  • Devon O. on Unity Ripple or Shock Wave Effect
  • Feral_Pug on Unity Ripple or Shock Wave Effect
  • bavvireal on Unity3D Endless Runner Part I – Curved Worlds
  • Danielius Vargonas on Custom Post Processing with the LWRP
Archives
  • December 2020 (1)
  • December 2019 (1)
  • September 2019 (1)
  • February 2019 (2)
  • December 2018 (1)
  • July 2018 (1)
  • June 2018 (1)
  • May 2018 (2)
  • January 2018 (1)
  • December 2017 (2)
  • October 2017 (1)
  • September 2017 (2)
  • January 2017 (1)
  • July 2016 (1)
  • December 2015 (2)
  • March 2015 (1)
  • September 2014 (1)
  • January 2014 (1)
  • August 2013 (1)
  • July 2013 (1)
  • May 2013 (1)
  • March 2013 (2)
  • December 2012 (1)
  • November 2012 (1)
  • September 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (1)
  • December 2011 (2)
  • October 2011 (3)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (1)
  • May 2011 (2)
  • April 2011 (2)
  • March 2011 (1)
  • February 2011 (1)
  • January 2011 (2)
  • December 2010 (3)
  • October 2010 (5)
  • September 2010 (1)
  • July 2010 (2)
  • May 2010 (5)
  • April 2010 (2)
  • March 2010 (7)
  • February 2010 (5)
  • January 2010 (5)
  • December 2009 (3)
  • November 2009 (1)
  • October 2009 (5)
  • September 2009 (5)
  • August 2009 (1)
  • July 2009 (1)
  • June 2009 (2)
  • May 2009 (6)
  • April 2009 (4)
  • March 2009 (2)
  • February 2009 (4)
  • January 2009 (1)
  • December 2008 (5)
  • November 2008 (2)
  • September 2008 (1)
  • August 2008 (6)
  • July 2008 (6)
  • June 2008 (9)
  • May 2008 (4)
  • April 2008 (3)
  • March 2008 (4)
  • February 2008 (9)
  • January 2008 (7)
  • December 2007 (6)
Copyright © 2021 Devon O. Wolfgang