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.

6 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…

  5. rabi says:

    is it possible to use this stuff in flex application

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 Flash Engineer PopCap Games, International Ltd.

Portfolio

Santabot: A Unity3D Flash Game


All right, so a Christmas game like “Santabot vs. The Flying Saucers from Mars” may be a day late[...]

Magnify – a jQuery Plugin


Let me begin by saying right up front, I have not given up on Flash[...]

It’s a Starling Halloween


Getting some practice for the upcoming Zombie Apocalypse[...]

Getting Started with Proscenium

So I had a chance this weekend to sit down and play around with Adobe’s new 3D framework for Stage3D, Proscenium, and thought I’d share a few of the results (a word of caution, there are no preloaders for any of the examples and may load a bit slowly). The first shows some reflections and [...]

Particle Editor for Starling Framework

An in-browser particle editor for the Starling 2D Framework for Flash Player 11.

So Long and Thanks for all the Flash on the Beach

So, another Flash on the Beach has just has just drawn to a close[...]

Game Development Tips from the Trenches of PopCap

Well, this is a post that’s a bit overdue, but, thanks to a well timed bank holiday, I finally had the opportunity to sit down and type up what I’ve been meaning to for some time now…

Old Skool Demoscene FX as 3D Textures

Many moons ago, I got the idea to create some demoscene plane deformation effects in Flash based on the formulas found here: http://www.iquilezles.org/www/articles/deform/deform.htm. I posted my less than desired results up on wonderfl. Thankfully, fellow wonderfl user, Hasufel, forked my attempt and optimized the hell out of it coming up with this. Well, today, for no [...]

Making The Gaming Scene (A Change in Careers)

And for my second blog post of the day, a much more personal note. After about three years of working with, what I would consider as objectively as possible, the best digital agency in Ireland, vStream Digital Media, I have made the immense career decision to leave the agency world and enter the arena of [...]

Feeling Lucky?

Images to dice, kick ascii style [...]

Adventures in Playbook Land

Adventures in Playbook Land


Now that the ordeal is over, I thought I’d take the time to sit down and share my account of what it was like to develop a Blackberry Playbook application using the Adobe Flex SDK[...]

Flash

Draw it for Me

So many ideas – so little time….

Kinect Application Running in Dublin

So, on Friday I just wrapped up our latest project at vStream Digital Media, a Kinect powered flipbook that lets users flip through the hand written notebooks of Philip Lynott of Thin Lizzy. The app uses OpenNI, runs in Adobe AIR and is currently on display at a pretty bitchin’ Phil Lynott exhibition running in [...]

Beach Ball Kinect Party

So we finally got a Kinect camera hooked up to a pc at work and, while it doesn’t seem to be legal to use it for commercial projects (but, hey, I’m no lawyer), the boss asked me to get it figured out and come up with some ideas just in case it would be feasible [...]

Facebook and Flash – A Book Review

Now, I should begin by saying I absolutely hate building Facebook applications. And I build a lot of them at work. Every time I get the word from above that we’re doing another FB app, I just groan – both inwardly and out. It’s become a running joke of the office. Why do I dislike [...]

Multitouch Fluid Dynamics with AIR for Android and RTMFP

The other day I was having some fun playing around with Eugene Zatepyakin’s (aka @inspirit) FluidSolverHD (Actionscript port of C++ fluid dynamics library, MSAFluid. Or is MSAFluid, the processing/java port of the C++ library? In any case it’s a very cool fluid dynamics thingamabob – the HD version using Alchemy). After a bit of tinkering, [...]