simple as3 state machine for games and apps


Some sample code for creating a simple state machine for your as3 apps.

The idea of a SM comes into play when you have even a slightly complex application, in which there are sequences of screens, game pause and play, pop-ups, leveling events and so on. Managing how the app responds and keeping track of where it is at can get messy in a hurry. Using the Model-View-Controller patterns, you can manage this complexity, maybe saving a few hours of debugging (or days).

This simple state matching code snip shows how you can change game states using a ‘jump table’ effectively a simple object array that leverages the .call() method of function, allowing us to call a function from a string of its name.

A full example is on its way, but for now I wanted to get the idea out there:

private function sm(s1:Number){
	jtrace("SM -> s0:"+gameState+" s1:"+s1);
	//uses currState, changes is, calls appropriate transition function
	var tbl=[
                {s0:INTRO,		s1:VIEW_HELP,		f1:"showHelp"},
	        {s0:VIEW_HELP,		s1:INTRO,		f1:"hideHelp"},
		{s0:ENTER_HISCORE,	s1:INTRO,		f1:"updateHiscores",		f2:"closeHiscores"},
		{s0:PRELOADING,	        s1:INTRO,		f1:"setupIntroScreen"},
		{s0:LEVEL_STARTING,     s1:INTRO,		f1:"abortLevel",			f2:"setupIntroScreen"},
		{s0:INTRO,		s1:LEVEL_STARTING,	f1:"startNewLevel",		f2:"hideIntro"},
		{s0:LEVEL_STARTING,     s1:LEVEL_PLAYING,	f1:"resumeGame"},
		{s0:LEVEL_PLAYING,	s1:GAME_END,		f1:"setupGameFinish"},
		{s0:GAME_END,		s1:INTRO,		f1:"startGame"},
		{s0:GAME_END,		s1:ENTER_HISCORE,	f1:"setupIntroScreen",		f2:"showHiscore"},
		{s0:LEVEL_PLAYING,	s1:LEVEL_PAUSED,  	f1:"pauseGame",			f2:"setupIntroScreen"},
		{s0:LEVEL_PAUSED,	s1:LEVEL_PLAYING,	f1:"resumeGame",			f2:"hideIntro"},
		{s0:LEVEL_PLAYING,	s1:LEVEL_END,		f1:"showLevelling",		f2:"doTallies"},
		{s0:LEVEL_END,		s1:LEVEL_STARTING,	f1:"startNewLevel"}
		];

	var i:Number;
	var j:Number;
	var func:Boolean=true;
	for(j=0;j s0:"+gameState+" s1:"+s1+" found at index:"+j);
			if( tbl[j].f1!=null ) this[tbl[j].f1].call();
			if( tbl[j].f2!=null ) this[tbl[j].f2].call();
			if( tbl[j].f3!=null ) this[tbl[j].f3].call();
			if( tbl[j].f4!=null ) this[tbl[j].f4].call();
			if( tbl[j].f5!=null ) this[tbl[j].f5].call();
			gameState=s1;
			return;
		}//if
	}//for
	jtrace("no SM action found on s0:"+gameState+" s1:"+s1);
}//func

This is essentially a hard-coded implementation, and a really nice implementation of a State Machine library was pointed out to me by its author, Cassio, at

http://github.com/cassiozen/AS3-State-Machine

Advertisements

2 thoughts on “simple as3 state machine for games and apps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s