<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Richard Lord &#187; Game development</title>
	<atom:link href="http://www.richardlord.net/blog/tag/game-development/feed" rel="self" type="application/rss+xml" />
	<link>http://www.richardlord.net</link>
	<description>Actionscript/Flex, PHP and Java developer</description>
	<lastBuildDate>Fri, 25 Jun 2010 07:34:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Actionscript 3 tetris source code</title>
		<link>http://www.richardlord.net/blog/actionscript-3-tetris-source-code</link>
		<comments>http://www.richardlord.net/blog/actionscript-3-tetris-source-code#comments</comments>
		<pubDate>Thu, 13 Dec 2007 11:35:13 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Free code]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Tetris]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/actionscript-3-tetris-source-code/</guid>
		<description><![CDATA[This will certainly be useful to anyone wanting to make a tetris game. It's probably also useful to anyone learning Actionscript 3, particularly if they want to make games.

The <a href="http://flashgamecode.net/free/tetris-engine">source code</a> is on the <a href="http://flashgamecode.net/">Flash Game Code</a> web site. It's in two parts - an engine, which does the game part, and a sample game that uses the engine. Have fun.]]></description>
			<content:encoded><![CDATA[This will certainly be useful to anyone wanting to make a tetris game. It&#8217;s probably also useful to anyone learning Actionscript 3, particularly if they want to make games.

The <a href="http://code.google.com/p/bigroom/wiki/TetrisGameEngine">source code</a> is on my <a href="http://code.google.com/p/bigroom/">free code repository</a>. It&#8217;s in two parts &#8211; an engine, which does the game part, and a sample game that uses the engine. Have fun.]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/actionscript-3-tetris-source-code/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Keyboard polling class &#8211; bug fix</title>
		<link>http://www.richardlord.net/blog/keyboard-polling-class-bug-fix</link>
		<comments>http://www.richardlord.net/blog/keyboard-polling-class-bug-fix#comments</comments>
		<pubDate>Tue, 04 Sep 2007 07:53:07 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Free code]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Keyboard polling]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/keyboard-polling-class-bug-fix/</guid>
		<description><![CDATA[There was an error in the Keyboard polling class that I posted yesterday. If you downloaded the class you&#8217;ll want the new version, which is available here.]]></description>
			<content:encoded><![CDATA[<p>There was an error in the Keyboard polling class that I <a href="/blog/polling-the-keyboard-in-actionscript-3">posted yesterday</a>. If you downloaded the class you&#8217;ll want the new version, <a href="http://code.google.com/p/bigroom/wiki/KeyPoll">which is available here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/keyboard-polling-class-bug-fix/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Polling the keyboard in Actionscript 3</title>
		<link>http://www.richardlord.net/blog/polling-the-keyboard-in-actionscript-3</link>
		<comments>http://www.richardlord.net/blog/polling-the-keyboard-in-actionscript-3#comments</comments>
		<pubDate>Mon, 03 Sep 2007 17:36:35 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Free code]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Keyboard polling]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/polling-the-keyboard-in-actionscript-3/</guid>
		<description><![CDATA[<p>Games often need to get the current state of various keys in order to respond to user input. This is not the same as responding to key down and key up events, but is rather a case of discovering if a particular key is currently pressed.</p>

<p>In Actionscript 2 this was a simple matter of calling Key.isDown() with the appropriate key code. But in Actionscript 3 Key.isDown no longer exists and the only intrinsic way to react to the keyboard is via the key up and key down events. To rectify this I created the KeyPoll class...</p>]]></description>
			<content:encoded><![CDATA[<p>Games often need to get the current state of various keys in order to respond to user input. This is not the same as responding to key down and key up events, but is rather a case of discovering if a particular key is currently pressed.</p>

<p>In Actionscript 2 this was a simple matter of calling Key.isDown() with the appropriate key code. But in Actionscript 3 Key.isDown no longer exists and the only intrinsic way to react to the keyboard is via the key up and key down events.</p>

<p>To rectify this I created the KeyPoll class, which has isDown and isUp methods, each taking a key code as a parameter and returning a Boolean. The class and an example of its use are <a href="http://code.google.com/p/bigroom/wiki/KeyPoll">in my free code repository</a>.</p>

<p>The class uses a ByteArray to hold the current keyboard state, which is updated in response to Keyboard events and queried by the public methods, isDown and isUp. Using a ByteArray is more compact than using an Array of Booleans, and in tests it also turned out to be marginally faster, although the difference in speed was not significant enough to matter.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/polling-the-keyboard-in-actionscript-3/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Physics for Flash Games: Slides</title>
		<link>http://www.richardlord.net/blog/physics-for-flash-games-slides</link>
		<comments>http://www.richardlord.net/blog/physics-for-flash-games-slides#comments</comments>
		<pubDate>Fri, 01 Jun 2007 13:30:05 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[LFPUG]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/physics-for-flash-games-slides/</guid>
		<description><![CDATA[<p>It was great to see so many people at my presentation yesterday at LFPUG. The presentation seemed to go down very well and I had a great evening. Here's the slides from my presentation. If you weren't there, they may not make a lot of sense without my talking over the top of them, but I suspect lots of people who were there want to look at the code again.</p>
]]></description>
			<content:encoded><![CDATA[<p>It was great to see so many people at my presentation yesterday at <a href="http://lfpug.com/">LFPUG</a>. The presentation seemed to go down very well and I had a great evening. Here&#8217;s the slides from my presentation. If you weren&#8217;t there, they may not make a lot of sense without my talking over the top of them, but I suspect lots of people who were there want to look at the code again.</p>

<p>The presentation is driven entirely from the keyboard arrow keys &#8211; left and right to move through the slides and up and down to scroll the text in the code displays.</p>

<p><b><a href="/images/slides/lfpug1/Slideshow.swf">Click here for the slides</a></b> (flash required)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/physics-for-flash-games-slides/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Physics for Flash Games</title>
		<link>http://www.richardlord.net/blog/physics-for-flash-games</link>
		<comments>http://www.richardlord.net/blog/physics-for-flash-games#comments</comments>
		<pubDate>Tue, 29 May 2007 07:29:17 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[LFPUG]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/physics-for-flash-games/</guid>
		<description><![CDATA[<p>On Thursday this week, I'm doing a presentation on Physics for Flash Games at the <a href="http://www.lfpug.com/31st-may-2007-31052007/">London Flash Platform User Group</a>. It's a tricky presentation to plan because there's such a wide range of knowledge and ability among the audience but I think I've come up with a presentation that has something for everyone. Hope to see lots of London Flash users there. There's no membership requirement so if you're interested just turn up. Or better still <a href="http://www.lfpug.com/31st-may-2007-31052007/">sign up on the web page</a> because then you have a chance to win one of the prizes in the raffle.</p>]]></description>
			<content:encoded><![CDATA[<p>On Thursday this week, I&#8217;m doing a presentation on Physics for Flash Games at the <a href="http://www.lfpug.com/31st-may-2007-31052007/">London Flash Platform User Group</a>. It&#8217;s a tricky presentation to plan because there&#8217;s such a wide range of knowledge and ability among the audience but I think I&#8217;ve come up with a presentation that has something for everyone. Hope to see lots of London Flash users there. There&#8217;s no membership requirement so if you&#8217;re interested just turn up. Or better still <a href="http://www.lfpug.com/31st-may-2007-31052007/">sign up on the web page</a> because then you have a chance to win one of the prizes in the raffle.</p>

<p>The slides from this talk are now <a href="/presentations/physics-for-flash-games">online here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/physics-for-flash-games/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Casual Multiplayer Gaming</title>
		<link>http://www.richardlord.net/blog/casual-multiplayer-gaming</link>
		<comments>http://www.richardlord.net/blog/casual-multiplayer-gaming#comments</comments>
		<pubDate>Fri, 27 Apr 2007 09:14:01 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/casual-multiplayer-gaming/</guid>
		<description><![CDATA[<p>Until recently I wasn't sure if casual multiplayer gaming was possible. In my experience, multiplayer gaming required a serious investment of time and energy.</p>

<p>But I spent most of yesterday afternoon playing Zwok, a multiplayer game that is so simple, and so much fun, I figure there may be something to learn from it. (And if I do learn from it I won't have to write off yesterday afternoon as wasted time). So here's a brief summary of my thoughts so far...</p>
]]></description>
			<content:encoded><![CDATA[<p>Until recently I wasn&#8217;t sure if casual multiplayer gaming was possible. In my experience, multiplayer gaming required a serious investment of time and energy. Even with the simple multiplayer flash games you have to choose a user name, set some options, read the instructions (I don&#8217;t want to look stupid in front of the other players), choose a room in the lobby and then ask to play with some one or more other people. At which point I often wonder, since I&#8217;ve not played the game before and so am probably rubbish at it, will they wish they&#8217;d said no when I asked to play with them?</p>

<p>But I spent most of yesterday afternoon playing <a href="http://zwok-game.com/">Zwok</a>, a multiplayer game that is so simple, and so much fun, I figure there may be something to learn from it. (And if I do learn from it I won&#8217;t have to write off yesterday afternoon as wasted time). So here&#8217;s a brief summary of my thoughts so far.</p>

<ul>
<li>Joining the game is very simple &#8211; just click the guest button (here called &#8220;quick play&#8221;) and you&#8217;re in.</li>

<li>There is no lobby, no decisions to be made, no worrying what others think and if they&#8217;ll play with you &#8211; having clicked the guest button you are dropped straight into a game. It couldn&#8217;t be any simpler &#8211; click one button and start playing.</li>

<li>The game is played in two teams of three. As a player you&#8217;re not totally reliant on your team-mates (as I got better I won some games with little or no help from my team-mates) so it really doesn&#8217;t matter how bad you are when you start out.</li>

<li>The game play is very simple &#8211; throw stuff at the other team. We all understand that and have enough experience with gravity in the real world to figure out roughly where our chosen missile will land.</li>

<li>Games last between two and five minutes, so it&#8217;s perfect to slot a few games in at lunchtime.</li>

<li>Registered users get to design their own character, build a reputation and gain access to some additional styles of missiles, but the game-play and fun are not limited for unregistered users.</li>
</ul>

<p>So, in a nutshell, make it as simple as possible to start playing. I know we apply that to single player casual games too but I&#8217;ve not seen it applied so effectively to a multiplayer game before. Add to that the standard &#8220;make it fun&#8221; and &#8220;make it short&#8221; and it sounds like a recipe for a successful casual multiplayer game.</p>

<p>Finally, one sour note in the game &#8211; who thought it would be a good idea to show the message &#8220;You have been kicked out of the game for poor performance&#8221; when your network connection is too slow or unreliable? I can&#8217;t be the only one to have thought the other players had decided I wasn&#8217;t good enough to play with them.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/casual-multiplayer-gaming/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finite State Machines for AI in Actionscript</title>
		<link>http://www.richardlord.net/blog/finite-state-machines-for-ai-in-actionscript</link>
		<comments>http://www.richardlord.net/blog/finite-state-machines-for-ai-in-actionscript#comments</comments>
		<pubDate>Mon, 15 Jan 2007 17:19:50 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Finite State Machine]]></category>
		<category><![CDATA[Game development]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/finite-state-machines-for-ai-in-actionscript/</guid>
		<description><![CDATA[<p>A few months ago <a href="/blog/best-soccer-game/">I said</a> I'd post about the AI in <a href="http://www.sticksportsfootball.com/">Stick Sports Soccer</a>. Here, at last, is the first such post looking at the code used to implement the Finite State Machines in this and other games I've developed.</p>]]></description>
			<content:encoded><![CDATA[<p>A few months ago <a href="/blog/best-soccer-game/">I said</a> I&#8217;d post about the AI in <a href="http://www.sticksportsfootball.com/">Stick Sports Soccer</a>. Here, at last, is the first such post looking at the code used to implement the Finite State Machines in this and other games I&#8217;ve developed.</p>

<p>(For an introduction to Finite State Machines, try <a href="http://en.wikipedia.org/wiki/Finite_state_machine">Wikipedia</a> and <a href="http://ai-depot.com/FiniteStateMachines/">AI-Depot</a>.)</p>

<p>Often finite state machines are implemented (in real projects and tutorials) as a mass of code in a single class, usually a giant switch statement hundreds or (in one instance I&#8217;ve seen) thousands of lines long. A simple switch statement is great for a simple agent with two or three states, but the more complex the agent gets, both in the number and complexity of the states, the more complex the code gets. Using a switch statement also offers very little opportunity to reuse code across different agents and different projects (other than by cutting and pasting).</p>

<p>The solution I&#8217;ve used in a number of projects is to implement each state as a separate class. The class will contain all the code necessary for entering, updating and exiting that state and nothing else. This way, the code for each state is separate and the agent code isn&#8217;t cluttered by it.</p>

<p>In this solution, each state implements the State interface, which looks like this.</p>

<pre class="code">interface State
{
    public function Enter():Void; // called on entering the state
    public function Exit():Void; // called on leaving the state
    public function Update( time:Number ):Void;
                   // called every frame while in the state
}</pre>

<p>Each state implements Entry and Exit methods for one-off actions that the agent takes when entering and leaving the state, in addition to the Update method which is run repeatedly while in the state. The time paramater in the Update method is the duration of the frame we&#8217;re executing.</p>

<p>A couple of states for a patrolling guard in a shoot &#8216;em up might look like this.</p>

<pre class="code">// patrolling the area
class Patrol implements State
{
    private var fsm:StateMachine;
    private var guard:Guard;
    
    public function Patrol( g:Guard )
    {
        guard = g;
        fsm = guard.GetStateMachine();
    }
    
    public function Enter():Void
    {
        // check the gun is loaded
        guard.Reload();
    }
    
    public function Exit():Void
    {
    }
    
    public function Update( time:Number ):Void
    {
        guard.FollowPatrolPath( time );
        var threat:Soldier = guard.Threatened();
                               // returns null if no threat
        if( threat )
        {
            fsm.ChangeState( new Attack( guard, threat ) );
        }
    }
}</pre>

<pre class="code">// attacking an enemy
class Attack implements State
{
    private var fsm:StateMachine;
    private var guard:Guard;
    private var enemy:Soldier;
    
    public function Attack( g:Guard, e:Soldier )
    {
        guard = g;
        fsm = guard.GetStateMachine();
        enemy = e;
    }
    
    public function Enter():Void
    {
    }
    
    public function Exit():Void
    {
    }
    
    public function Update( time:Number ):Void
    {
        guard.ShootAt( enemy );
        if( enemy.IsDead() )
        {
            fsm.ChangeState( new Patrol( guard ) );
        }
    }
}</pre>

<p>I then create the Finite State Machine as a class that is responsible for managing the current state. A simple state machine looks like this.</p>

<pre class="code">class StateMachine
{
    private var currentState:State;
    
    public function StateMachine()
    {
        currentState = null;
    }
    
    // Update the FSM. Parameter is the frametime for this frame.
    public function Update( time:Number ):Void
    {
        if( currentState )
        {
            currentState.Update( time );
        }
    }
    
    // Change to another state
    public function ChangeState( s:State ):Void
    {
        currentState.Exit();
        currentState = s;
        currentState.Enter();
    }
}</pre>

<p>The agent then uses an instance of the StateMachine class to handle its AI.</p>

<pre class="code">class Agent
{
    private var fsm:StateMachine;
    
    public function Agent()
    {
        fsm = new StateMachine();
    }
    
    public function Update( time:Number ):Void
    {
        fsm.Update( time );
    }
    
    public function GetStateMachine():StateMachine
    {
        return fsm;
    }
}</pre>

<p>The agent class is a lot more manageable without the state machine implementation and all the states inside it, and I have a single StateMachine class that can be used by all agents. I can even share states across agents too.</p>

<p>Finally, I add a number of features to the StateMachine class to allow chaining of states and returning to previous states.</p>

<pre class="code">class StateMachine
{
    private var currentState:State;
    private var previousState:State;
    private var nextState:State;
    
    public function StateMachine()
    {
        currentState = null;
        previousState = null;
        nextState = null;
    }
    
    // prepare a state for use after the current state
    public function SetNextState( s:State ):Void
    {
        nextState = s;
    }
    
    // Update the FSM. Parameter is the frametime for this frame.
    public function Update( time:Number ):Void
    {
        if( currentState )
        {
            currentState.Update( time );
        }
    }
    
    // Change to another state
    public function ChangeState( s:State ):Void
    {
        currentState.Exit();
        previousState = currentState;
        currentState = s;
        currentState.Enter();
    }
    
    // Change back to the previous state
    public function GoToPreviousState():Void
    {
        ChangeState( previousState );
    }
    
    // Go to the next state
    public function GoToNextState():Void
    {
        ChangeState( nextState );
    }
}</pre>

<p>Which leads to modified and new classes like the following.</p>

<pre class="code">class Soldier extends Agent
{
    ...
}</pre>

<pre class="code">class Guard extends Soldier
{
    ...
}</pre>

<pre class="code">// attacking an enemy
class Attack implements State
{
    private var fsm:StateMachine;
    private var self:Soldier;
    private var enemy:Soldier;
    
    public function Attack( s:Soldier, e:Soldier )
    {
        self = s;
        fsm = self.GetStateMachine();
        enemy = e;
    }
    
    public function Enter():Void
    {
    }
    
    public function Exit():Void
    {
    }
    
    public function Update( time:Number ):Void
    {
        self.ShootAt( enemy );
        if( enemy.IsDead() )
        {
            fsm.GoToPreviousState();
        }
    }
}</pre>

<pre class="code">// wait for a given period of time then go to next state
class Wait implements State
{
    private var fsm:StateMachine;
    private var self:Agent;
    private var waitTime:Number;
    private var timeRemaining:Number;

    public function Wait( s:Agent, t:Number )
    {
        self = s;
        fsm = self.GetStateMachine();
        waitTime = t;
    }
    
    public function Enter():Void
    {
        timeRemaining = waitTime;
    }
    
    public function Exit():Void
    {
    }

    public function Update( time:Number ):Void
    {
        timeRemaining -= time;
        if( timeRemaining < 0 )
        {
            fsm.GoToNextState();
        }
    }
}</pre>

<p>Having used this solution for a couple of years I can't imagine returning to the mess of giant switch statements, even for the simplest projects. And I use the same state machine, agent base class and many of the states themselves across multiple projects, which speeds up development time.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/finite-state-machines-for-ai-in-actionscript/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
