Feb 15 2009

Simple countdown timer in ActionScript3.0

For a private site-project I did a very simple countdown timer in pure actionscript3.0. The timer extends a textfield and updates itself every seconds. Since it is very simple it should be easy to extend it in various ways. Right now it shows the remaining time in the following format:

DAYS:HOURS:MINUTES:SECONDS

countdown-timer-scr

The code itself is very basic - nothing really special. Only one thing really confused me. Have a look at line number 20. The second parameter of Date-constructor specifies the month. There is something special about it - months are enumerated from 0 to 11 whereas days for example are enumerated from 1 to 31. This is something I find very confusing and strange. So keep this in mind when you are using the Date object in actionscript.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package
{
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	/**
	 * Little countdown timer extending a normal textfield
	 *
	 * @author benny!weltenkonstrukteur.de
	 */
	public class Countdown extends Sprite
	{
		// CONFIG THIS VALUE TO THE DESTINATION DATE
                // FORMAT YYYY:MM:DD:HH::MM:SS::MSMS
                // MONTH STARTS WITH ZERO
		private static var DEST_DATE:Date = new Date(2009, 05, 15, 20, 42, 0, 0);
 
		// Member variables
		private var cntTxt:TextField = null;
		private	var cntTime:uint = 0;
		private var cntTimer:Timer = null;
 
		/**
		 * Constructor
		 */
		public function Countdown()
		{
			this.init();
			cntTimer.start();
		}
 
		/**
		 * Inits all stuff on startup
		 */
		private function init():void
		{
			// Setup the text format
			var tf:TextFormat = new TextFormat();
			tf.font = 'Courier New';
			tf.size = 24;
			tf.color = 0x33ff33;
			tf.bold = true;
 
			// Setup text field
			cntTxt = new TextField();;
			cntTxt.width = stage.stageWidth;
			cntTxt.defaultTextFormat = tf;
 
			// Setup timer
			cntTimer = new Timer(1000);
			cntTimer.addEventListener(TimerEvent.TIMER, onTimer);
 
			// Add text field to stage
			addChild(cntTxt);
		}
 
		/**
		 * Handler for timer event
		 * @param	e, TimerEvent
		 */
		private function onTimer(e:TimerEvent):void
		{
			// Calculate the remaining time
			var asec:int = Math.round((DEST_DATE.getTime() - new Date().getTime()) / 1000);
 
			trace(asec);
 
			// If countdown is passed
			if ( asec < 0 )
			{
				// Show zeros instead
				cntTxt.text = "000:00:00:00";
			}
 
			// Otherwise
			else
			{
				// Calculate time parts
				var secs:uint = asec % 60;
				var amin:uint = Math.round(asec / 60);
				var mins:uint = amin % 60;
				var ahr:uint  = Math.round(amin / 60);
				var hrs:uint  = ahr % 24;
				var days:uint = Math.round(ahr / 24);
				var strSecs:String, strMins:String, strHrs:String, strDays:String;
 
				// Format with inital 0
				( secs < 10 ) ? strSecs = "0" + secs.toString() : strSecs = secs.toString();
				( mins < 10 ) ? strMins = "0" + mins.toString() : strMins = mins.toString();
				( hrs < 10  ) ? strHrs  = "0" + hrs.toString()  : strHrs = hrs.toString();
				if ( days < 10 )
				{
					strDays = "00" + days.toString();
				}
				else if ( days > 10 && days < 100 )
				{
					strDays = "0" + days.toString();
				}
				else
				{
					strDays = days.toString();
				}
 
				// Update time
				cntTxt.text = strDays + ":" + strHrs + ":" + strMins + ":" +strSecs;
			}
		}
	}
}

Feb 4 2009

Beta phase closed. Game dev in progess.

Ok, after getting a lot of valuable feedback about my game I decided to close the beta-testing phase for now and to disable the link to the game.

I picked up all ideas and thought about them a lot. Thanks at this point to all the great ideas, suggestions and bug reports. Hope you will understand that I cannot implement all ideas. Guess you all know how easy you can set your goals to high and run the risk of never finish a project. So after some struggle with myself I came up with the following lists of feature I wanted to implement. Note, missing things can always be implemented in a follow up version ;-)

Gameplay

Most of the beta testers asked what the goal of the game was. There was no real rewarding system that motivates them to play. Well, the most requested features about the gameplay was to add levels and/or a highscore system.

I really like the suggestion to add different levels, because it could be implemented rather easily. A level is just a set of platforms (their coordinates and size) where the blocks should be dropped of. In addition I though about declaring two values/restriction to each level:

  • Number of blocks that should be placed on the platforms
  • Total number of blocks available

So, in each level you have to place a certain number of blocks on the platforms without losing too much blocks. I think with these three factors I can create a challenging level system (hopefully).

Generally, I like games with a highscore system, too. They have a real arcade feeling. However, I decided against it. Main reason for that is that an online highscore system can become rather complex for such a small project. First, think about the backend. You need a reliable database server and some non-exploitable communication with the server. If you have set this up - you need to do some frontend stuff in your flash game that a.) communicates with
the server anb b.) shows the answers from the server. This could end up in too much work which doesn’t pay off in the end.

Media

Well, since I am neither a musician nor a designer I reduce the use of media to a minimum. Especially license-free sounds are hard to find I think it doesn’t do any harm to ship the game without sound.

Concerning the graphics I need to think about a proper menu and navigation concept. In addition I need to rework the wind display - since a lot of testers complained that it is not prominent enough and I think they are right. Finally, I will try to spend some time in GIMP to create a little logo.

Speaking of the logo, I came up with a name of the game finally. The game is called:

PHLOCKS

This is a composition of the two words: PHsics and and Blocks. Well, maybe not very original but since a google search for the terms game and phlocks doesn’t return any relevant results - I think it is okayish.

Ok - enough words written. Back to work - and again: THANKS FOR ALL BETA TESTING. It is highly appreciated!

Stay tuned.


Feb 1 2009

Beta version of a simple physics game

In the last two days I played around with Box2D. Box2D is an open source 2D physics engine for games. In addition there exists an actionscript port for the Flash platform. Studying various examples on the internet and the wonderful tutorials of Emanuele Feronato I came up with the following little game. It is still in beta phase and there are plenty of things which needs to be added and polished.

scr-tblocks

Note: Link to the game is deactivated. The final game is in progress. Beta is closed.

Anyway, maybe you want to have a try on it. The logic is very easy. The aim of the game is to drop as many blocks as possible on the green bar. Move your mouse to determine the x-position of the block to drop. Press left button to drop it. On the upper right you see the percentage of wind and the direction in which the wind is blowing. That wind effects the following blocks.

Here is a little list of things that are still to do:

  • Highscore system
  • Start screen
  • Polish score and wind system
  • Fixing bugs

Please let me know if you have any more ideas or if you find any bugs. By the way, my current highscore is 22 blocks ;-)