Game Timer Download

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:

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.

5 Comments »

  1. szataniol says:

    As far as I remember majority of games have “pause” option ;)

  2. Devon O. says:

    Always one in the bunch.. :)
    Just updated to include a toggle() method which will pause or unpause the timer…

  3. Dave says:

    Very nice Devon! Great work as usual !

  4. Shoom says:

    Looking good.
    Only 3 months too late for me…

RSS feed for comments on this post. / TrackBack URI

Leave a Reply

Devon O. Wolfgang

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development”

Contributing Author of “Flash AS3 for Interactive Agencies”

Senior Software Developer in Dublin, Ireland

Portfolio

Bayer Pixel Bender Filter

Bayer Mosaic Filter in Pixel Bender


Now, seeing as how I’ve been trying to learn some Pixel Bender coding lately[...]

Liquitext

Liquid Text Effect


Haven’t been too active around here lately due to a major on going project at work lately as well as the fact that my wife and I are in the slow process of moving homes. Really, the only free time it seems I have these days is during the lunch hour. So, over the past two days, I came up with this fun little toy during lunches[...]

Google’s Text To Speech Engine in Flash

I’m not sure how I managed to miss this, but I just happened to run across a ‘new’ (well, newish), albeit still unofficial, offering of Google today: text-to-speech. You can see what few details there are on this Techcrunch post. Basically, it just boils down to this though – you send your phrase to be [...]

Quick Sound Effects Generator


Need some beeps, boops, or bops, to go with your UI or games [...]

Quick QuickBox2D Tip II – Collision Detection

Custom collision detection/handling in QuickBox2D [...]

None of This Runs Eternal


No Flash/Actionscript stuff here. Just me rambling about the upcoming Current 93 concert [...]

Playing Around with the New UndoManager

Included in the Flex 4.0 SDK and the, just released, Flash Professional CS5 lies a new hidden little gem of a class: flashx.undo.UndoManager (although the Flex 4.0 SDK’s been out for awhile, I have to admit I didn’t even notice this until I installed Flash CS5 and started poking around the documentation looking for new [...]

Making Waves

In a previous post, “Digging into the Microphone in Flash Player 10.1″, reader David Law asked in the comments how it would be possible to save .wav files to the server. I wasn’t sure right offhand, but thought I’d spend my lunch hour yesterday looking into it. Well, after reading this quick tutorial on Adobe [...]

Some Drawing Fun and a Bad Movie You’ll Never See

Earlier today, Dave Gillem posted a link on Facebook to an incredible Processing based drawing application. Thought I’d have a go at reproducing something similar in Flash. Well, I failed miserably, but the results were still interesting enough to check out. You can play around with it below. Just mouse down to draw in the [...]

Animating Bezier Curves


The other day I got the notion in my head that I wanted to draw some bezier curves in an animated fashion. I’m sure this has been done a million times before, but sometimes reinventing the wheel can be a good learning experience [...]

My God, It’s Full of Stars…


Just a quick little thing I got an idea for (i.e. pretty much ripped off) while browsing Processing examples.

Another Year, Another Look

As a few folks may have noticed, the blog is looking a little bit different as of today [...]

The Webcam Warholizer


Just a little bit of quick Friday fun with webcam and bitmapata…

More With the JiglibFlash Terrain

Playing around a bit more with the animated JiglibFlash terrain idea from my last post, I wanted to see how it would work in conjunction with a webcam. So, needless to say, you’ll need a webcam to check out the results below.

Rockin and Rollin with the JiglibFlash Terrain


Finally got the chance to play around with the new JiglibFlash height map terrain, today, and have to say I am very impressed [...]