« July 2004 | Inicio | September 2004 »

August 29, 2004

The State pattern and game development ( IV and final )

Well, finally it's time to develop a full game.

This is how our microgame ( "Drops everywhere" ) will work. Drops keep popping on the screen, but we ensure that the number of drops on the screen is lower than a given number. We can remove a drop by clicking on it. And we have to maintain a low number of drops during a given time. If we succeed, we can play again, or finish the game. If we play again, the maximum number of drops will be lower, so we’ll have to click more of them in the same time. The game goes on and on until we decide to finish it, or until the number of drops is higher than the allowed number.

You can see a statechart diagram here.

The code is distributed in three layers. There’s a model, a view, and a controller, although it is not structured as a MVC. The controller listens to the events fired by the model, and notifies the model when there is an action in the view ( a button click, for instance ).

The initialization process of the game is the following: when the swf is loaded, an instance of DropsController is created. This object creates an instance of DropsWorld, registers itself as an event listener, and starts the state machine associated to the world. The class DropsWorld extends another class called EventSource( in the package net.designnation.events ), that provides the ability to fire events , and to register listeners to those events. The controller ( DropsController ) also creates an empty MovieClip, that will be the main “clock”.

So, when the world is created, we have to start its state machine:

public function initWorld( param: Object ) { this.stageMC = param.baseline; this.initBehaviour( ); var theClass: DropsWorld = this; this.base_MC.onEnterFrame = function( ) { theClass.doProcess( ); } this.mySMachine.startMachine( ); } private function doProcess( ) { this.BEngineVal.doProcess( ); }

So a new cycle of the state machine will be executed with every execution of the base_MC.onEnterFrame event.

We define the state machine here:

private function initBehaviour( ) { var initGame : State= new State( "initGame", new CallbackDecl( this, "initGameAction" ) ); var startGame : State= new State( "startGame", new CallbackDecl( this, "startGameAction" ) ); var createDrop : State= new State( "createDrop", new CallbackDecl( this, "createDropAction" ) ); var overDrops : State = new State( "overDrops", new CallbackDecl( this, "overDropsAction" ) ); var endOfTime : State= new State( "endOfTime", new CallbackDecl( this, "endOfTimeAction" ) ); var defeat : State= new State( "defeat", new CallbackDecl( this, "defeatAction" ) ); var victory : State= new State( "victory", new CallbackDecl( this, "victoryAction" ) ); var endOfGame : State= new State( "endOfGame", new CallbackDecl( this, "endOfGameAction" ) ); new Transition( "initGameToStartGame", initGame, startGame, new CallbackDecl( this, "initGameToStartGameEval" ) ); //---------------------------------------------------------- new Transition( "startGameToCreateDrop", startGame, createDrop, new CallbackDecl( this, "startGameToCreateDropEval" ) ); //---------------------------------------------------------- new Transition( "createDropToEndOfTime", createDrop, endOfTime, new CallbackDecl( this, "createDropToEndOfTimeEval" ) ); new Transition( "createDropToOverDrops", createDrop, overDrops, new CallbackDecl( this, "createBubbleToOverDropsEval" ) ); new Transition( "createDropToSelf", createDrop, createDrop, new CallbackDecl( this, "createDropToSelfEval" ) ); //---------------------------------------------------------- new Transition( "overDropsToDefeat", overDrops, defeat, new CallbackDecl( this, "overDropsToDefeatEval" ) ); //----------------------------------------------------------- new Transition( "endOfTimeToVictory", endOfTime, victory, new CallbackDecl( this, "endOfTimeToVictoryEval" ) ); new Transition( "endOfTimeToStartGame", endOfTime, startGame, new CallbackDecl( this, "endOfTimeToStartGameEval" ) ); //------------------------------------------------------------- new Transition( "defeatToEndOfGame", defeat, endOfGame, new CallbackDecl( this, "defeatToEndOfGameEval" ) ); //------------------------------------------------------------- new Transition( "victoryToEndOfGame", victory, endOfGame, new CallbackDecl( this, "victoryToEndOfGameEval" ) ); this.mySMachine.resetToInit( initGame ); }

Once the states and the transitions are defined, we must implement the callbacks needed.

The class that we use to manage the drops is a very light controller. It also extends EventSource, so it will be able to fire an event when the drop is clicked. I know the way I’ve implemented this functionallity can start an endless discussion, but I’ve decided that a drop is a drop, not a MovieClip, so the MovieClip with the drop graphic is aggregated to the Drop class, and the Drop class does not extend MovieClip.

I’d also like to say that obiously this is not the final implementation. A lot of the DropsWorld code should be common for any game that we develop, so it will be wise to put that common functionallity on a base class, and make DropsWorld extend that class. The game itself is not completely finished ( there’s an obvious lack of feedback ), but, in general, I believe this example can be considered as a start point. And, for your information, the development time has been of about an hour and a half.

Well, the final result is here:

And the source code can be downloaded here ( don’t forget to set your classpath ).

Packages:
net.designnation.behaviours
net.designnation.data
net.designnation.events
net.designnation.physics
net.designnation.PoppingDrops ( game classpath )

I’d also like to thank Celia Carracedo for the game graphics.

And, finally, a disclaimer. There are some days when I find really dificult to express my thoughts in english. Today was one of them.

August 21, 2004

macromedia.com has gone mobile?

Well, maybe this is not new for many of you, but I've noticed today that the whole macromedia.com site can be browsed from my pocketpc. It's amazing!.

I've just created an avantgo channel to read the flash developer center articles offline. ;)

Congratulations. Good work!

August 20, 2004

Cool flash application ( pocketpc )

Mobilympics is a cool pocketpc application that will help you keep track of seven sports during the Olympic Games, developed by pc de bolso.

If you own a pocketpc, I think it's worth a try, but bear in mind that the program is provided as 'Ad-ware' meaning that you will find some information about the developer's sponsors ( mostly HP ) when entering and leaving Mobilympics.

August 14, 2004

design-nation. mobile

Well, we don't want to be the last blog to implement a mobile version, so here it is, and you can even create an avantgo channel!!.

Sadly, we are using a beautifier to format the actionscript code, and we have not implementd it yet in the mobile version, but we are planning to do it in a nearly future.

design-nation.mobile

August 13, 2004

Socket Server

Thanks to my brother César, I have noticed this Socket server at freshmeat. I have just downloaded it, and i´ll make some tests soon. It can be hosted in any machine running PHP5. It sounds interesting.


Project in freshmeat
CSS ( Cyber Socket Server )
freshmeat

The authors claim that it is similar to Flash Communication Server, and that it can be seamesly integrated in our flash applications.

OT: How is the job market in Germany?

I'm seriously thinking about moving to Germany ( first, for personal reasons, second, because it's a country I really like ), but I don't know were to start looking for a job.

So, please, if you can give me some advice about how can I approach the job search, or you know someone that I could send my CV to, or if you even want to take a look at my CV, please drop me a line at blog@design-nation.net, or leave a comment here.

Thanks in advance.

August 12, 2004

Sound.onSoundComplete: ¿Bug or feature?

Maybe this is common place, but it's something that has surprised me.

The problem I've found is related to the last posts I've made about the state machines.

I've been testing a game that I developed some time ago. At a given moment, a transition must be executed when a sound is finished ( that means that something must happen when a sound is finished ). But, although the game always worked, today it didn't!!.

After a two hours debugging session, I've just tried to play the game in a different computer, and it worked!!. And then I remembered. I reinstalled the operating system of my development machine two days ago, after my holydays ( sniff :-( ), and the new system didn't recognize the sound card. In fact, if I look at my control panel, I see that there are not audio devices installed.

So what has happened?. It seems that the onSoundComplete event is never triggered. I don't know if it's the expected behaviour or not, but well, it's something I must remember!

August 10, 2004

OT: Updated to MT3.01

In the last few days we've had to remove almost 300 comments between both this blog and its spanish version.

So, we've finally moved to MT3.01, although it was not, by far, our first choice. We'd like to move to WordPress or b2Evolution, but when we started this blog we made a huge mistake, and used the default naming system for our posts ( based on a number ) instead of using the post title. So now, as we can't modify the server .htaccess file, we cannot change to another CMS without changing the existing posts' urls.

That's why we have updated our MT installation, and configured it to "Enable Unregistered Comment Moderation".

We don't like this solution, because it sounds quite similar to "we are going to censor your comments", but right now, it's the best we can do. We are looking at the registration system and typeKey, so I hope we could find a better solution.

August 08, 2004

The summer break is over :(

A two weeks break is too short. Extremely short. Anyway, here are some pictures.

Vitoria - Gasteiz, Virgen Blanca Square: Here we are, Cesar and Javier. By the way, Vitoria is one of the most beautiful places I've seen. It reminds me a lot of Hamburg ( Germany ) which is the most beautiful city I've seen so far.

vitoria.jpg


Still at Vitoria. An "aizkolaris" exhibition.

aizkolaris.jpg

Luanco ( Asturias ). A little hotel, that surprised me because of its decoration ( everything was bought at Ikea ).

luanco1.jpg

Cabo de Peñas ( Asturias ). The Cantabric Sea.

luanco2.jpg


And back to Madrid, some people wearing the traditional suits.

madrid2.jpg

It's hard to be at home again...