Apr 16 2009

AS3.0 TutBits #02: Unknown number of parameters

The second TutBit is dealing with creating a method/function which takes an arbitrary number of arguments. This makes the creation of very flexible function calls possible. Have a look at the following example:

private function showFavouriteMovies( name:String, ...movies:Array ):void
{
	var retString:String = name + "'s favourite movies are: ";
 
	for (var i:uint = 0; i < movies.length; i++)
	{
		retString += movies[i];
		if ( i < (movies.length-1) )
		{
			retString += ", ";
		}
	}
 
	trace(retString + ".");
}

The created method called showFavouriteMovies() takes two arguments. The first parameter called “name” is a normal parameter of type String. The second parameter “…movies” defines an array to hold any arguments passed to the method. The name after the “…”-construct defines the name of the array (here it is “movies”).

So, calling the function for example with the following paramters:

showFavouriteMovies("Joshua", "Wargames", "13th Floor", "The beach");

will result in:


Joshua's favourite movies are: Wargames, 13th Floor, The beach.

You see that the first named paramter called “name” can be used normally. The list of movies are stored in an indexed array where the first element (i.e. index 0) is the left-most value of the method call (in our example it is “Wargames”) proceeding to the right.

Always keep in mind, that the “…”-construct must be the last parameter in your method’s signature.


Apr 12 2009

AS3.0 TutBits #01: Getter and Setter

AS3.0 TutBits is a series of small Actionscript mini tutorials. Their purpose is to point out certain differences of the actionscript 3.0 language compared to other programming languages. Each tutorial just concentrates on a tiny technical issue. The AS3.0 TutBits are not tutorials that teaches you how to programm in actionscript.

Ok, enough introductional text - let’s start:

First topic is Getter and Setter. Well, I do no want to start the discussion again if they are useful or not - here it is how they are supported in Actionscript3.0:

package
{
	public class GetterSetter
	{
		private var hiddenName:String = "OLD: Joshua";
 
		public function get name():String
		{
			return this.hiddenName;
		}		
 
		public function set name(newName:String):void
		{
			this.hiddenName = "NEW: " + newName;
		}
	}
 
}

On line #05, we have the class property called hidden name we want to make accessible via getter and setters. Therefore, we create two methods one line #07 and line #12 which are identified by the keyword get and set followed by the same methodname name(). With this construct you create a faked public class property called name of the class GetterSetter. You access it like a normal property using the following syntax:

var gs:GetterSetter = new GetterSetter();
trace(gs.name);
gs.name = "Joshua";
trace(gs.name);

The output of that would be:

OLD: Joshua
NEW: Joshua

That means that when you access the faked class property you actually call the respective get- or set- method. So you have the easiness of simulating direct access of a property value but still have the chance to keep control of it.

Nevertheless, this technique has some drawbacks. First, it is not possible that the property has the same name as the get/set methodname. So, if we would have named our property “name” instead of “hiddenName” it would resulted in a compile-time error. In addition, whenever you call the set method - the get method is called automatically as the result of the set method call. This hidden “feature” could also causes some confusion if you do not know that.

In the end it is up to your personal taste if you want to use this feature of actionscript or not. Of course you could implement getters and setters in the standard way writing methods like getHiddenName() and setHiddenName().


Mar 27 2009

Experimental PV3D clock

Here is a little experimental 3D-Clock realized with Papervision3D. It is my entry to the DBF Democode TIME CHALLENGE. The little effect shows seconds (purple box) and minutes (greenish box). The seconds clock hand moves every 5 seconds and the minutes clock hand every five minutes. The hour clock hand is missing.

Screenshot of PV3D clock

Well, as always I ran out of time (deadline is today). That’s why the following features I actually wanted to implement are missing:

  • shadow casting
  • hour clock hand
  • moving camera
  • correct some 3D issues

The experiment requires flash 9 and uses Papervision3D and the Tweener.


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 &lt; 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 &lt; 10 ) ? strSecs = "0" + secs.toString() : strSecs = secs.toString();
				( mins &lt; 10 ) ? strMins = "0" + mins.toString() : strMins = mins.toString();
				( hrs &lt; 10  ) ? strHrs  = "0" + hrs.toString()  : strHrs = hrs.toString();
				if ( days &lt; 10 )
				{
					strDays = "00" + days.toString();
				}
				else if ( days &gt; 10 &amp;&amp; days &lt; 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.