<?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>Cheezeworld</title>
	<atom:link href="http://cheezeworld.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cheezeworld.com</link>
	<description>The Cheeze to Your Macaroni - Games, Open Source Programming, and Other Odd Ramblings.</description>
	<lastBuildDate>Mon, 04 Jan 2010 18:23:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Retiring Cheezeworld From Further Articles by Myself</title>
		<link>http://cheezeworld.com/retiring-cheezeworld-from-further-articles-by-myself/</link>
		<comments>http://cheezeworld.com/retiring-cheezeworld-from-further-articles-by-myself/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 18:23:58 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Farewell]]></category>
		<category><![CDATA[Goodbye]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=246</guid>
		<description><![CDATA[A final farewell to my readers.]]></description>
			<content:encoded><![CDATA[<p><a href="http://cheezeworld.com/wp-content/uploads/2010/01/goodbye.jpg"><img class="alignleft size-full wp-image-247" title="goodbye" src="http://cheezeworld.com/wp-content/uploads/2010/01/goodbye.jpg" alt="" width="480" height="360" /></a>Hi, as many of my regulars have noticed it has been some time since my last article. I have been doing some soul searching over the last year or so and decided to switch paths to some degree. More than likely I won&#8217;t be focusing on programming any longer. Instead I will be spending my energy to build and develop my new website <a href="http://www.colbycheeze.com">Colbycheeze.com</a> which will be focused on personal growth, productivity, time management, health, and overall happiness.</p>
<p>I want to leave by giving huge thanks to my readers, and have no worries I will continue to leave the website / code up for all to view. In addition I plan on releasing a large part of my unfinished code / projects as open source for anyone to learn / expand upon.</p>
<p>I am also willing to support any guest posts as well, if anyone has a topic they wish to share with my audience. Farewell, and I wish you all a great 2010!</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/retiring-cheezeworld-from-further-articles-by-myself/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Bitmap Animation API Released &#8211; Ginger</title>
		<link>http://cheezeworld.com/bitmap-animation-api-released-ginger/</link>
		<comments>http://cheezeworld.com/bitmap-animation-api-released-ginger/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 17:01:50 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Game Library]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Animated Bitmap]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Ginger]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=229</guid>
		<description><![CDATA[Woot! I am so excited to be able to finally release this to the public. Panayoti from GamePoetry.com and I have been working hard on this for the past 9 months or so ( on and off ) This was originally sparked off by my reading of his request for help: Improving Performance With Animated [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-232" title="keyframeanimation" src="http://cheezeworld.com/wp-content/uploads/2009/09/keyframeanimation.jpg" alt="keyframeanimation" width="328" height="248" />Woot! I am so excited to be able to finally release this to the public. Panayoti from GamePoetry.com and I have been working hard on this for the past 9 months or so ( on and off ) This was originally sparked off by my reading of his request for help:<a href=" http://www.gamepoetry.com/blog/2008/10/31/improving-performance-with-animated-bitmaps/"> Improving Performance With Animated Bitmaps</a>. I said to myself, &#8220;<a href="http://cheezeworld.com/bitmapclip/">Hey I&#8217;ve basically already done all of this!</a>&#8221; So after talking over the vision of the animation system for a while I set out to create a robust and extensible API. It only took a day or so to get the bulk of it coded, however over the last few months we have been using it in projects so that we can test / refine it further. Finally it is to the point that we can both happily release it to you guys!</p>
<p>So without further delay, I now introduce you to Ginger :</p>
<h2>Features :</h2>
<ul>
<li>Extremely fast and optimized animations</li>
<li>Set the rotation precision, and each frame is pre cached for speed</li>
<li>Attach an event dispatch to any frame of animation</li>
<li>Adjustable frame rate</li>
<li>Set your own offset for each frame of animation</li>
<li>Queue up multiple animations</li>
<li>Import Tile sheets and convert to an animation</li>
<li><span style="color: #ff0000;">*NEW &#8211; 9/10/09*</span> Import MovieClips, Sprites, and Bitmaps and convert to an animation</li>
</ul>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_GingerDemo_251034542"
			class="flashmovie"
			width="500"
			height="400">
	<param name="movie" value="http://www.cheezeworld.com/files/GingerDemo.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.cheezeworld.com/files/GingerDemo.swf"
			name="fm_GingerDemo_251034542"
			width="500"
			height="400">
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><a href="http://code.google.com/p/gamepoetry/source/browse/#svn/trunk/projects/ginger/GingerDemo">Download the example files from SVN here.<br />
</a>-or-<br />
<a href="http://code.google.com/p/cheezeworld/downloads/list">Download in .ZIP format here.</a></p>
<h3>Future Plans</h3>
<ul>
<li>Create a real time loader for larger animation files</li>
<li>Optimizations to rotated animations</li>
<li>Tools for building animations using XML format</li>
<li><span style="color: #ff0000;"><span style="text-decoration: line-through;">Tools for loading MovieClips / swf</span>s &#8211; Done!<br />
</span></li>
<li>Easy integration with my game framework</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/bitmap-animation-api-released-ginger/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Finite State Machines</title>
		<link>http://cheezeworld.com/finite-state-machines/</link>
		<comments>http://cheezeworld.com/finite-state-machines/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 16:59:03 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Game Library]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Finite State Machine]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[FSM]]></category>
		<category><![CDATA[RTS]]></category>
		<category><![CDATA[State Design Pattern]]></category>
		<category><![CDATA[State Driven Agent Design]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=198</guid>
		<description><![CDATA[
So after a long hiatus I have decided to jump back into the Flash scene. I am going to kick off my return with a continuation of the Building a Game Stucture series. Today we look into FSM ( Finite State Machines ). FSMs are used as the basic AI for most games, especially RPG [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://cheezeworld.com/wp-content/uploads/2009/08/artificial.intelligence-229x300.jpg" alt="artificial.intelligence" title="artificial.intelligence" width="229" height="300" class="alignleft size-medium wp-image-212" /><br />
So after a long hiatus I have decided to jump back into the Flash scene. I am going to kick off my return with a continuation of the <a href="../game-structure/">Building a Game Stucture</a> series. Today we look into FSM ( Finite State Machines ). FSMs are used as the basic AI for most games, especially RPG / Platformer / RTS type games.</p>
<p>What is an FSM / What is it used for?<br />
An FSM can be used to determine the behavior of an entity based on specific conditions. Essentially they imbue an entity with the illusion of intelligence. Coding a state machine is a simple matter. There are in fact numerous ways to implement an FSM and there isn&#8217;t necessarily one &#8220;right&#8221; way to do it.</p>
<p>First We will look at a simple, although potentially problematic approach.<br />
<span id="more-198"></span></p>
<div align="left"> _</div>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> health <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		changeState<span style="color: #66cc66;">&#40;</span> DIE_STATE <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span> currentState <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> IDLE_STATE:
			patrol<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> isEnemySighted <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				changeState<span style="color: #66cc66;">&#40;</span> CHASE_STATE <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">break</span>;
&nbsp;
		<span style="color: #b1b100;">case</span> CHASE_STATE:
			chaseEnemy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> isEnemyInRange <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> health <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">&amp;&amp;</span> mana <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					castHeal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span>
				<span style="color: #66cc66;">&#123;</span>
					attackEnemy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">break</span>;
&nbsp;
		<span style="color: #b1b100;">case</span> DIE_STATE:
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> isDeathAnimationComplete <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeEntity<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				playDeathAnimation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see setting up a switch and several if statements can accomplish basic AI behavior. The problem with this approach begins to surface as more states, conditions, and transitions are introduced. Imagine how big of a monster it would be to edit this structure of code on an entity that had over a dozen different states and transitions. Also it would be hard to switch out the behaviors of entity. For example, what if you wanted the player to be able to control this unit? You would have to create a separate class altogether. More on that later.</p>
<h2>The State Design Pattern</h2>
<p>The best way that I have found to implement an extensible FSM is with what is called the State Design Pattern. With this approach we will be embedding the rules for the state transitions within the states themselves.</p>
<p>Actionscript doesn&#8217;t allow for any type of function templates unfortunately. So we will have to work around that in order to allow for varying types of entities to be used in our FSM. The user will be forced to do his/her own error checking, so only a small annoyance. With states, generally you will want to have transitions when the states are being changed, so our pattern will allow for an enter, execute, and exit function.</p>
<p>Here is the Interface for the states:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IState <span style="color: #0066CC;">extends</span> IEventDispatcher
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> setEntity<span style="color: #66cc66;">&#40;</span> a_entity:<span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> getEntity<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #66cc66;">*</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">enter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> exit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see the structure is actually quite simple. The states will contain a reference to the entity they are operating upon. Here is an example of a somewhat fleshed out state:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PatrolState <span style="color: #0066CC;">extends</span> EventDispatcher <span style="color: #0066CC;">implements</span> IState
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setEntity<span style="color: #66cc66;">&#40;</span> a_entity:<span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span><span style="color: #66cc66;">&#40;</span>a_entity is Unit<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;&lt;PatrolState&gt; a_entity must be of type Unit!&quot;</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	m_entity = a_entity;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEntity<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> m_entity;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">enter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// set up patrol path and steering behaviors</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// if enemy is close, set state to chase state</span>
	<span style="color: #808080; font-style: italic;">// else continue patrolling</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> exit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// clean up patrol paths</span>
	<span style="color: #808080; font-style: italic;">// remove steering behaviors</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_mine:Mine;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_base:Building;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_entity:Harvester;
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So now that we have a state set up, we can improve this system a lot by creating a reusable state machine that handles the switching / updating of the states. Here is the interface:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StateMachine
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StateMachine<span style="color: #66cc66;">&#40;</span> a_owner:Entity, a_currentState:IState = <span style="color: #000000; font-weight: bold;">null</span>, a_globalState:IState = <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> dispose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> changeState<span style="color: #66cc66;">&#40;</span> newState:IState <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> changeGlobalState<span style="color: #66cc66;">&#40;</span> newState:IState <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> revertToPreviousState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In my implementation I allow for a &#8220;global&#8221; function as well, which will be called every tick in addition to the actual states. This may or may not be used depending on the behavior you are looking for. Also, a reference is being held to the &#8220;previous state&#8221; so that temporary behavior can be given and then the entity can switch back to what it was doing before. For instance, if a unit was on a patrol path and spots an enemy&#8230;it can switch to an attack state, and then once the enemy is dead it can revert to it&#8217;s patrol path.</p>
<p>Here is a quick example I created in order to display the FSM in action. ( might be a bit shitty, but I was in a hurry <img src='http://cheezeworld.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_FSM_97742941"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.cheezeworld.com/files/FSM.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.cheezeworld.com/files/FSM.swf"
			name="fm_FSM_97742941"
			width="550"
			height="400">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>So as you can, FSM&#8217;s can be used to create fairly complex and intelligent behavior. Not only that, but imagine creating an RTS style game where units are given commands by the player and the units all follow those orders. Rather than creating dozens of different classes with built in behaviors based on if they are on the players team or being controlled by the AI, you could toss in the correct states to the FSM controller. Then let&#8217;s say you decided to give one of those units top down shooter style controls if you clicked on them&#8230;that could be done as well, again by just changing the state. So the possibilities are limitless.</p>
<p>With that, I would like to also give some reference to <a href="http://blog.gamingyourway.com/PermaLink,guid,8c079942-1647-4393-a7fc-fab8d014e24b.aspx">another useful method</a> which works well in some less complex scenarios, instead of this full blown approach. For instance, I actually used this method to manage the 3 states of a &#8220;ball&#8221; which can be grabbed, aimed, and then launched on another project of mine.</p>
<p>Be sure to add me to your RSS reader if you haven&#8217;t already. I plan to slowly get back to more consistent posting very soon. Sorry about the long wait guys! So until next time&#8230;be sure to download the new code and examples at my <a href="http://code.google.com/p/cheezeworld/">google project page</a>, and here a list of the updates to the game structure code:</p>
<h2>Code Changes</h2>
<ul>
<li><b>GameFactory:</b></li>
<li>If no parent is specified for getEntity function, it will default to the instance of the gameworld ( if it exists )</li>
<li>No longer need to specify entity t ype on getEntity…IE: var myBoid:Boid = m_factory.getEntity( &#8220;boid&#8221; ) as Boid. You can now take off the &#8220;as Boid&#8221;</li>
<li><b>GameWorld</b></li>
<li>Fixed error with the Gameworld&#8217;s bounds setup.</li>
<li><b>Moving Entity</b></li>
<li>Added BOUNDS_STOP behavior</li>
<li>Fixed the bounds behaviors for entities inside of other entities in different locations than 0,0</li>
<li><b>Entity</b></li>
<li>Entity now implements IEntity which will improve use with other classes etc such as the game factory.</li>
<li>Fixed the bounds size calculation</li>
<li><b>Root</b></li>
<li>Made a change to the stageWidth/Height thing that makes it lookup what the dimensions are.</li>
<li><b>Follow Path</b></li>
<li>Fixed so that if it is set to &#8220;not&#8221; patrol, it will not create an error.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/finite-state-machines/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Actionscript 3 AI Steering Behaviors Library</title>
		<link>http://cheezeworld.com/steering-behaviors-librar/</link>
		<comments>http://cheezeworld.com/steering-behaviors-librar/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 15:32:42 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Game Library]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flocking]]></category>
		<category><![CDATA[Game Engine]]></category>
		<category><![CDATA[game structure]]></category>
		<category><![CDATA[Steering Behaviors]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=190</guid>
		<description><![CDATA[Big update for you guys! Hey I know I&#8217;ve been a bit AWOL on posts lately, but recently I just finished a project which I was on a time crunch to get done. Also I had been having issues with ironing out the last of some bugs with my new big library for you guys. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-191" title="big-parrot-flock9-799781" src="http://cheezeworld.com/wp-content/uploads/2009/02/big-parrot-flock9-799781.jpg" alt="big-parrot-flock9-799781" width="400" height="300" />Big update for you guys! Hey I know I&#8217;ve been a bit AWOL on posts lately, but recently I just finished a project which I was on a time crunch to get done. Also I had been having issues with ironing out the last of some bugs with my new big library for you guys. It is an AS3 Artificial Intelligence Steering Behaviors library which works with the game structure code I&#8217;ve been writing.</p>
<p>Also I&#8217;ve made several adds / changes / fixes to the game structure which I&#8217;ll list out below. I wanted to write a big article ( or several ) explaining this big AI library and how each thing works, but I didn&#8217;t want to keep everyone waiting any longer so I am going to go ahead and release it now and I&#8217;ll write up articles about it as time permits. ( which I have more of now )</p>
<p>For those of you who want to dig in and learn it all on your own go ahead, but for everyone else I promise I&#8217;ll get some articles up soon explaining the changes and some tutorials on how steering behaviors work and how I&#8217;ve designed mine.</p>
<div align="left"></div>
<div align="left"></div>
<div align="left"></div>
<div align="left"></div>
<p>Here is a demo app that I created which shows off everything. You can download the source from the <a href="http://code.google.com/p/cheezeworld/">google project page.</a> I highly suggest setting up a repository folder on your PC and keep it updated with the current game library folder, as that is the one which I will be submitting updates to. I won&#8217;t be updating the various zips any longer. I recommend <a href="http://tortoisesvn.net/downloads">tortoise SVN</a>.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_steering_1645681960"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.cheezeworld.com/files/steering.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.cheezeworld.com/files/steering.swf"
			name="fm_steering_1645681960"
			width="550"
			height="400">
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>So anyhow, here is a list of changes to the game structure that I have made so far. Be sure to check back soon for more AI / Game Structure fun!</p>
<h2>ENGINE CHANGES:</h2>
<ul>
<li> Added more functionality to the Sound Manager class.</li>
<li> GameFactory is no longer a singleton.</li>
<li>Added creation of gameworld to GameFactory for easy game setup.</li>
<li>Fixed dispose function on Entity.</li>
<li>MovingEntity now restricts its movements to the max speed variable.</li>
<li>MovingEntity has a MaxAcceleration var, which is mainly just for external use.</li>
<li>Added a damping variable to MovingEntity.</li>
<li>Fixed an incorrect math function on Matrix2D.rotateVector()</li>
<li> CreateCallback function added to Root</li>
<li>Updated all classes extending EntityParams with the ability to pass variables in the constructor IE:<br />
new EntityParams( { radius:10, type:&#8221;Blob&#8221; } );<br />
NOTE: be sure to call super( a_params:Object ) AFTER you set any of your defaults, or anything passed in will be overwritten ( by the defaults )</li>
<li>Added a createCallback function to Root. Quite useful in some cases, check it out.</li>
<li>Cleaned up Root by taking out the FPS calculation. Use a custom FPS class for this now, such as the Urbansquall one which I am using in the Main() class.<br />
- Added astage width/height const to Root for ease of use. Obviously you will have to change it depending on your games size.</li>
<li>Fixed the lineIntersecton function of the Geometry Class. ( Thanks to Ryan from Mochimedia for pointing this out )</li>
<li>Added a &#8220;getRandomPolygon&#8221; utility function to the Geometry class which returns an array of points based on some parameters</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/steering-behaviors-librar/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Added SVN Repository to the Google Project Page</title>
		<link>http://cheezeworld.com/added-svn-repository-to-the-google-project-page/</link>
		<comments>http://cheezeworld.com/added-svn-repository-to-the-google-project-page/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 15:34:10 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[repository]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=186</guid>
		<description><![CDATA[Hey guys, just wanted to say I am still alive. Haven&#8217;t been able to finish my latest post yet due to lack of time and some bugs I&#8217;m having trouble figuring out.  I decided to go ahead and create an SVN for all of the code here, so that everyone who prefers to stay up [...]]]></description>
			<content:encoded><![CDATA[<p>Hey guys, just wanted to say I am still alive. Haven&#8217;t been able to finish my latest post yet due to lack of time and some bugs I&#8217;m having trouble figuring out.  I decided to go ahead and create an<a href="http://code.google.com/p/cheezeworld/"> SVN for all of the code here</a>, so that everyone who prefers to stay up to date and use that sort of thing can. It is also MUCH easier than updating .zip files, so that is what I will keep most up to date now.</p>
<p>I&#8217;ve also released a little tidbit about <a href="http://cheezeworld.com/constraining-rotation-max-turn-rate/">constraining rotations to a max amount per second.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/added-svn-repository-to-the-google-project-page/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Programming Tidbit #3 &#8211; Constraining Rotation to a Max Turn Rate Per Second</title>
		<link>http://cheezeworld.com/constraining-rotation-max-turn-rate/</link>
		<comments>http://cheezeworld.com/constraining-rotation-max-turn-rate/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 14:57:58 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Tidbits]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[Cannon]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Rotations]]></category>
		<category><![CDATA[Turn Rate]]></category>
		<category><![CDATA[Turret]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=177</guid>
		<description><![CDATA[
This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.
Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-178" title="d_turret" src="http://cheezeworld.com/wp-content/uploads/2009/01/d_turret-300x225.jpg" alt="d_turret" width="300" height="225" /></p>
<p>This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.</p>
<p>Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the turret on a tank or the turning of the tank itself. This can be done with a bit of math. It could seem a bit complicated without using the math libraries I have already created.</p>
<p align="left">
<h4>Here it is using my code:</h4>
<p>We are going to assume the maxTurnRate will be in degrees per second.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #66cc66;">&amp;</span>amp; currentVelocity.<span style="color: #006600;">angleTo</span><span style="color: #66cc66;">&#40;</span> newVelocity <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">*</span> MathUtils.<span style="color: #006600;">DEG_TO_RAD</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> stepSize <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
 	<span style="color: #000000; font-weight: bold;">var</span> mat:Matrix2D = <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	mat.<span style="color: #006600;">rotate</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> maxTurnRate <span style="color: #66cc66;">*</span> MathUtils.<span style="color: #006600;">DEG_TO_RAD</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> stepSize <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> heading.<span style="color: #006600;">sign</span><span style="color: #66cc66;">&#40;</span> newVelocity <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
 	mat.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span> currentVelocity<span style="color: #66cc66;">&#41;</span>;
 	mat.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span> heading <span style="color: #66cc66;">&#41;</span>;
 <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">else</span>
 <span style="color: #66cc66;">&#123;</span>
 	velocity.<span style="color: #006600;">x</span> = _newVel.<span style="color: #006600;">x</span>;
 	velocity.<span style="color: #006600;">y</span> = _newVel.<span style="color: #006600;">y</span>;
 <span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>The Code Explained</h2>
<p>So let&#8217;s take a look at what is happening there. First, we are checking to see if maxTurnRate is &gt; 0&#8230;because we will assume if it is set as 0 then the entity should be able to turn freely. If it is greater, then we will get the angle from our current velocity and check to see if it is greater than the max turn rate multiplied by the step size. The step size is generally just: tickSize / 1000.</p>
<p>The math to get the angle from one vector to another is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> angleTo<span style="color: #66cc66;">&#40;</span> vector1:Vector2D, vector2:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">acos</span><span style="color: #66cc66;">&#40;</span> dotProduct<span style="color: #66cc66;">&#40;</span> vector1, vector2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> dotProduct<span style="color: #66cc66;">&#40;</span> vector1:Vector2D, vector2:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #006600;">x</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span> vector1.<span style="color: #006600;">y</span> <span style="color: #66cc66;">*</span> vector2.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, if the rotation is greater than the max amount aloud&#8230;we will use a matrix to rotate the velocity vector to it&#8217;s correct position and transform it and the heading ( the direction the entity is going ). Note: The &#8220;sign&#8221; function just returns -1 if vector is counterclockwise, else 1 if clockwise. We need this to know which direction to rotate obviously.</p>
<h2>The Matrix Code</h2>
<p>Here is some of the matrix code, though I definitely don&#8217;t plan on explaining matrices in this post. Which is why it&#8217;s best to just use the library if you don&#8217;t actually understand the math part itself. <img src='http://cheezeworld.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Use to rotate.
 * ( Considers that the Vector object represents a point, not an actual vector )
 * @param rot
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rotate<span style="color: #66cc66;">&#40;</span>rot:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">sin</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>rot<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">cos</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>rot<span style="color: #66cc66;">&#41;</span>;
	multiply<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">cos</span>, <span style="color: #0066CC;">sin</span>, <span style="color: #cc66cc;">0</span>, -<span style="color: #0066CC;">sin</span>, <span style="color: #0066CC;">cos</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * Use to multiply two Matrixes...mainly used internally.
 * @param matrix The Matrix to multiply by.
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> multiply<span style="color: #66cc66;">&#40;</span>matrix:Matrix2D<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> m:Matrix2D = <span style="color: #000000; font-weight: bold;">new</span> Matrix2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//first row</span>
	m.<span style="color: #006600;">a1</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">a2</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">a3</span> = <span style="color: #66cc66;">&#40;</span>a1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>a3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//second row</span>
	m.<span style="color: #006600;">b1</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">b2</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">b3</span> = <span style="color: #66cc66;">&#40;</span>b1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//third row</span>
	m.<span style="color: #006600;">c1</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c1</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">c2</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c2</span><span style="color: #66cc66;">&#41;</span>;
	m.<span style="color: #006600;">c3</span> = <span style="color: #66cc66;">&#40;</span>c1 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">a3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">b3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c3 <span style="color: #66cc66;">*</span> matrix.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0066CC;">Set</span><span style="color: #66cc66;">&#40;</span>m.<span style="color: #006600;">a1</span>,m.<span style="color: #006600;">a2</span>,m.<span style="color: #006600;">a3</span>,m.<span style="color: #006600;">b1</span>,m.<span style="color: #006600;">b2</span>,m.<span style="color: #006600;">b3</span>,m.<span style="color: #006600;">c1</span>,m.<span style="color: #006600;">c2</span>,m.<span style="color: #006600;">c3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * This is used to do the final transformation upon the Vector
 * @param point The Vector to apply this Matrix transformation to.
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> transformVector<span style="color: #66cc66;">&#40;</span>point:Vector2D<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> tempX:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>a1<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b1<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c1<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> tempY:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>a2<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>b2<span style="color: #66cc66;">*</span>point.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span>c2<span style="color: #66cc66;">&#41;</span>;
	point.<span style="color: #006600;">x</span> = tempX;
	point.<span style="color: #006600;">y</span> = tempY;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>And that&#8217;s all!</h2>
<p>So anyways&#8230;I&#8217;ve shown the internal workings of the math code there for you, though the first code block is all that should really matter to you in the end&#8230;since it shows how to actually use the math.</p>
<p>No example today, since I&#8217;m pretty sure you can imagine a turret rotating at a max speed&#8230;lol. However, it will be used in the next <a href="http://cheezeworld.com/game-structure/">Game Structure</a> post for the entities.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/constraining-rotation-max-turn-rate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Updates: 12/26/2008</title>
		<link>http://cheezeworld.com/some-updates-12262008/</link>
		<comments>http://cheezeworld.com/some-updates-12262008/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 03:21:28 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Updates]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=168</guid>
		<description><![CDATA[I&#8217;ve updated my coding conventions slightly.

Also for those of you waiting for the next article&#8230;be patient, it is a BIG one! I&#8217;ve created many Artificial Intelligence behaviors and a great OOP structure to use them, and I&#8217;ll be discussing Boids and many of the behaviors in detail. I&#8217;ll try to get it finished as soon [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated my <a href="http://cheezeworld.com/coding-standards/">coding conventions</a> slightly.
<p>
Also for those of you waiting for the next article&#8230;be patient, it is a BIG one! I&#8217;ve created many Artificial Intelligence behaviors and a great OOP structure to use them, and I&#8217;ll be discussing Boids and many of the behaviors in detail. I&#8217;ll try to get it finished as soon as possible.
<p>
I hope everyone is enjoying their Christmas break!</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/some-updates-12262008/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Managing Sounds and Music in Actionscript 3</title>
		<link>http://cheezeworld.com/managing-sounds-and-music-in-actionscript-3/</link>
		<comments>http://cheezeworld.com/managing-sounds-and-music-in-actionscript-3/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 22:36:32 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Game Library]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Sound]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=145</guid>
		<description><![CDATA[
Dealing with sounds in Actionscript can be kind of wacky. Sound Objects, Channels, Transforms&#8230;blah. I don&#8217;t like messing with all of that, so I&#8217;ve built a sweet, easy to use Sound Manager. I don&#8217;t suppose it has any magical powers or anything but that&#8217;s only because I couldn&#8217;t think of anything magical a Sound Manager [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://cheezeworld.com/wp-content/uploads/2008/12/sound.jpg" alt="sound" title="sound" width="450" height="349" class="alignright size-full wp-image-146" /></p>
<p>Dealing with sounds in Actionscript can be kind of wacky. Sound Objects, Channels, Transforms&#8230;blah. I don&#8217;t like messing with all of that, so I&#8217;ve built a sweet, easy to use Sound Manager. I don&#8217;t suppose it has any magical powers or anything but that&#8217;s only because I couldn&#8217;t think of anything magical a Sound Manager should do&#8230;other than play sounds and control volume.</p>
<p align="left">
<p align="left">
<p align="left">
<h2>The Sound Manager Class</h2>
<p>It is a Singleton, and can be used from anywhere in your game / project. Simply register all of your sounds to the manager, and play them using IDs rather than having references to the sounds themselves.</p>
<p>Also, I have made it so that you can play a &#8220;sound&#8221;, or play &#8220;music&#8221; which is controlled at a different volume. Also, it is set up so that only one music sound will play at a time.</p>
<p>Here is the interface for the Sound Manager:<span id="more-145"></span></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SoundManager
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// -- INITIALIZATION ---</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:SoundManager
&nbsp;
        <span style="color: #808080; font-style: italic;">// Volume Control</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> soundVolume<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> soundVolume<span style="color: #66cc66;">&#40;</span> volume:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> musicVolume<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> musicVolume<span style="color: #66cc66;">&#40;</span> volume:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SoundManager<span style="color: #66cc66;">&#40;</span> enforcer:SingletonEnforcer <span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerSound<span style="color: #66cc66;">&#40;</span> a_sound:<span style="color: #0066CC;">Sound</span>, a_id:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playSound<span style="color: #66cc66;">&#40;</span> a_id:<span style="color: #0066CC;">String</span>, a_startTime:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>, a_loops:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopSound<span style="color: #66cc66;">&#40;</span> a_id:<span style="color: #0066CC;">Sound</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playMusic<span style="color: #66cc66;">&#40;</span> a_id:<span style="color: #0066CC;">String</span>, a_start:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>, a_loops:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopMusic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><h2>Example Usage</h2>
<p>I like that I can use ids to play the sounds, because in my games I tend to use 2-3 sounds for certain effects such as death or explosions, which I like to randomly choose. In the example code you can see how I do this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainScreen <span style="color: #0066CC;">extends</span> AScreen
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../assets/gunshot.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> GunshotSound:<span style="color: #000000; font-weight: bold;">Class</span>;
	<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../assets/murloc.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> MurlocSound:<span style="color: #000000; font-weight: bold;">Class</span>;
	<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../assets/diemaggot.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> DieMaggotSound:<span style="color: #000000; font-weight: bold;">Class</span>;
	<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../assets/mario.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> MarioSound:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainScreen<span style="color: #66cc66;">&#40;</span>a_parentScreen:IScreenItem=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>a_parentScreen<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		SoundManager.<span style="color: #006600;">instance</span>.<span style="color: #006600;">registerSound</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> GunshotSound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;sound0&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		SoundManager.<span style="color: #006600;">instance</span>.<span style="color: #006600;">registerSound</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MurlocSound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;sound1&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		SoundManager.<span style="color: #006600;">instance</span>.<span style="color: #006600;">registerSound</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> DieMaggotSound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;sound2&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		SoundManager.<span style="color: #006600;">instance</span>.<span style="color: #006600;">registerSound</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MarioSound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;music&quot;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> tf:<span style="color: #0066CC;">TextField</span> = Utils.<span style="color: #0066CC;">createTextField</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span> <span style="color: #66cc66;">&#41;</span>;
		tf.<span style="color: #0066CC;">htmlText</span> = Utils.<span style="color: #006600;">createHtmlText</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Click the mouse to play 1 of 3 random sounds. <span style="color: #000099; font-weight: bold;">\n</span> Press 'spacebar' to play music. <span style="color: #000099; font-weight: bold;">\n</span> Press 'up' or 'down' to control sound volume.&quot;</span>, <span style="color: #ff0000;">&quot;#FFFFFF&quot;</span>, <span style="color: #cc66cc;">16</span> <span style="color: #66cc66;">&#41;</span>;
		tf.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">550</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span> - tf.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;
		tf.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">400</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span> - tf.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span>a_timePassed:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> Input.<span style="color: #006600;">instance</span>.<span style="color: #006600;">isMousePressed</span> <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> rand:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">4</span> - <span style="color: #cc66cc;">1</span>;
			SoundManager.<span style="color: #006600;">instance</span>.<span style="color: #006600;">playSound</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;sound&quot;</span> + rand <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> Input.<span style="color: #006600;">instance</span>.<span style="color: #006600;">isKeyPressed</span><span style="color: #66cc66;">&#40;</span> KeyCode.<span style="color: #006600;">SPACEBAR</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			SoundManager.<span style="color: #006600;">instance</span>.<span style="color: #006600;">playMusic</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;music&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>
As always, download the source at the <a href="http://code.google.com/p/cheezeworld/">Google project page.</a></p>
<p> Here it is in action.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ManagingSounds_949310425"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.cheezeworld.com/files/ManagingSounds.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.cheezeworld.com/files/ManagingSounds.swf"
			name="fm_ManagingSounds_949310425"
			width="550"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/managing-sounds-and-music-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Game Object Creation &#8211; The Factory Method</title>
		<link>http://cheezeworld.com/object-creation-game-factory/</link>
		<comments>http://cheezeworld.com/object-creation-game-factory/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 10:59:42 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Game Library]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[Factory Pattern]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[game structure]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Object Creation]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=135</guid>
		<description><![CDATA[This is the 4th article in the series: Building a Game Structure. Be sure to read the introduction which also has links to the rest of the articles in the series.
In previous articles we have put together a framework to handle various issues with our game. As of now, it would be quite sufficient for [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cheezeworld.com/wp-content/uploads/2008/12/factory.png"><img class="alignleft size-full wp-image-137" title="factory" src="http://cheezeworld.com/wp-content/uploads/2008/12/factory.png" alt="" width="424" height="338" /></a>This is the 4th article in the series: Building a Game Structure. Be sure to read the <a href="../game-structure/">introduction </a>which also has links to the rest of the articles in the series.</p>
<p>In previous articles we have put together a framework to handle various issues with our game. As of now, it would be quite sufficient for creating many simple games. One problem however, is that the creation of multiple types of entities in various places in our code can get cumbersome and is prone to errors and/or problems if we begin to make changes to the way we want our entities to be created</p>
<h2>The Factory Method</h2>
<p>The proper way to handle the creation of multiple objects with similar functionality is to<span id="more-135"></span> use the Factory Pattern. The benefit of using a factory class to create our entities is that not only can it return a new instance of an entity to us, but it can also provide additional functionality. Our GameFactory will also create a renderer for our objects, and add any constant forces such as gravity, wind, etc. This effectively reduces the amount of things we must deal with on creation to just the one call: GameFactory.instance.getEntity( &#8220;type&#8221; );</p>
<p>The goal of a properly implemented factory class should be to encapsulate change. In order to do this, we identify the parts of our code that can change, and separate it from that which does not. In the case of object creation, the word &#8220;new&#8221; is a bad thing. With the factory class which I have created you can always expect to have an entity correctly instantiated by calling GameFactory.instance.getEntity( &#8220;type&#8221; ); Now, if after writing several hundred lines of code which multiple entities being instantiated all over the place I decide that each time a certain entity is created, I will also play a sound&#8230;I simply add that to the Game Factory. I do not have to worry about finding all cases where the object is created and changing it there. Now you can see where the power of the factory method comes into play.</p>
<h2>A Barebones Example</h2>
<p>The basic factory method would be to create a class which looks somewhat like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">getEntity<span style="color: #66cc66;">&#40;</span> a_type:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span> : Entity
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span> a_type <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;type1&quot;</span>:
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Type1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;type2&quot;</span>:
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Type2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;type3&quot;</span>:
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Type3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #000000; font-weight: bold;">default</span>:
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Type not recognized!&quot;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>The Mother of All Factories</h2>
<p>The GameFactory that I have created is actually much more functional than that. What I have done is program all entities and renderers to a common interface, and keep the constructors matching as well. There is no proper way in Actionscript to force subclasses to follow the same constructor format, however if they do not, then a runtime error will be generated to explain the problem.</p>
<p>The goal of the factory class which we will be implementing is to allow us to simplify the creation of any new entities as much as possible. We will create and register entities to the GameFactory using the &#8220;EntityParams&#8221; classes. Once it is time to create new entities, we will simply call: GameFactory.instance.getEntity( &#8220;type&#8221; );</p>
<p>Let&#8217;s take a look at the code for our GameFactory now:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameFactory
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// Game Factory is a Singleton, use this to access its methods</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:GameFactory
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GameFactory<span style="color: #66cc66;">&#40;</span> enforcer:SingletonEnforcer <span style="color: #66cc66;">&#41;</span>
&nbsp;
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEntityParams<span style="color: #66cc66;">&#40;</span> a_type:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span> : EntityParams
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerEntityType<span style="color: #66cc66;">&#40;</span> a_params:EntityParams <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// These two functions are called by the classes themselves</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerGameWorld<span style="color: #66cc66;">&#40;</span> a_gameWorld:GameWorld <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerGameWorldRenderer<span style="color: #66cc66;">&#40;</span> a_renderer:GameWorldRenderer <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
 	<span style="color: #808080; font-style: italic;">// Will add a constant force to all entities of type MovingEntity</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addGlobalForce<span style="color: #66cc66;">&#40;</span> a_force:Vector2D, a_name:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
 	<span style="color: #808080; font-style: italic;">/**
 	 *  This does NOT remove the force from any entities that were created
 	 *  using it! To do that you must remove the force by calling the appropriate
 	 *  function on the entities themselves.
 	 */</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removeGlobalForce<span style="color: #66cc66;">&#40;</span> a_name:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// This is the meat and potatoes of this class. Check out the source.</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEntity<span style="color: #66cc66;">&#40;</span> a_type:<span style="color: #0066CC;">String</span>, a_parent:Entity=<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> : Entity
&nbsp;
 	<span style="color: #808080; font-style: italic;">// Attaches a renderer to an entity</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRenderer<span style="color: #66cc66;">&#40;</span> a_entity:Entity, a_renderer:<span style="color: #000000; font-weight: bold;">Class</span>, a_data:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#41;</span>:ARenderer
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Code Changes</h2>
<p>I have also made some changes and improvements to existing code. Here it is:</p>
<ul>
<li>Added type, customClass, rendererClass, and rendererData to EntityParams for GameFactory purposes.</li>
<li>Removed &#8220;parent&#8221; variable from EntityParams&#8230;it didn&#8217;t make sense to have it there.</li>
<li>Entity class now adds type to the id to further distinguish itself in debugging purposes.</li>
<li>All renderers now use the same constructor template.</li>
<li>GameWorld and GameWorldRenderer now activate themselves in GameFactory. ( This somewhat goes against &#8220;proper&#8221; OOD by coupling with a Singleton class, although I prefer the ease of use in this case. )</li>
<li>Altered the &#8220;Root&#8221; class to use the <a href="http://www.gamepoetry.com/blog/2008/04/18/understanding-the-game-loop/">Timer class</a> from <a href="http://gamepoetry.com">GamePoetry.com</a> for the game loop. The game loop is now more accurate.</li>
<li>Fixed an error caused with the removal / disposal of entities.</li>
</ul>
<h2>The Example</h2>
<p>From now on, you will generally want to create a seperate file of some sort to do the initialization of your entity types. For most people this can just be a designated .as class, however other options would be to use an external .xml file or to REALLY harness the power of this sort of game structure, you could load your entities from a database. This would allow things such as user created content, or whatever you can think of.</p>
<p>For the example, I create a &#8220;BouncyBall&#8221; type in a seperate .as file called EntityTypes. Then for the main code, I simply initialize a couple of editable textfields and a button that changes the BouncyBall params to match what you enter. One thing that you could try out is that you could make some code that lets you register your own entity types and properties, and then another textfield that lets you type in the name of the entity you would like to spawn, and a button that creates that entity.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ObjectCreation_1042478538"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.cheezeworld.com/files/ObjectCreation.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.cheezeworld.com/files/ObjectCreation.swf"
			name="fm_ObjectCreation_1042478538"
			width="550"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<h2>Final Words</h2>
<p>I didn&#8217;t necessarily go over all of the fine details of the ACTUAL Factory Method Pattern here, so if you are curious of the exact implementation of it try searching Google, however I believe that my implementation makes object creation MUCH more flexible and robust.</p>
<p>Our core game structure is now quite solid, and we are moving into things such as game AI, particle effects, collisions, and other such things. The next article in the series will focus on one of these concepts, so be sure to download the new game code at the <a href="http://code.google.com/p/cheezeworld/">google project page</a> and sign up for the RSS feed if you haven&#8217;t already.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/object-creation-game-factory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Managing Game Objects and Rendering a Game World</title>
		<link>http://cheezeworld.com/game-world/</link>
		<comments>http://cheezeworld.com/game-world/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 00:24:09 +0000</pubDate>
		<dc:creator>Colby Cheeze</dc:creator>
				<category><![CDATA[Game Library]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[entities]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[game camera]]></category>
		<category><![CDATA[game objects]]></category>
		<category><![CDATA[game world]]></category>
		<category><![CDATA[Rendering]]></category>

		<guid isPermaLink="false">http://cheezeworld.com/?p=128</guid>
		<description><![CDATA[
This is the 3rd article in the series Building a Game Structure. Be sure to read the introduction which also has links to the rest of the articles in the series.
There comes a time when working on a game that you realize the need for an effective way to manage all of your entities and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cheezeworld.com/wp-content/uploads/2008/12/sporeplanet.jpg"><img class="alignleft size-full wp-image-129" title="sporeplanet" src="http://cheezeworld.com/wp-content/uploads/2008/12/sporeplanet.jpg" alt="" width="300" height="275" /></a></p>
<p>This is the 3rd article in the series Building a Game Structure. Be sure to read the <a href="http://cheezeworld.com/game-structure/">introduction </a>which also has links to the rest of the articles in the series.</p>
<p>There comes a time when working on a game that you realize the need for an effective way to manage all of your entities and game objects. Also, not all games consist of just one simple non-scrolling screen so you must devise a robust way to deal with the problems of displaying your entities in the correct position based on where in the world you want the view to be centered, not to mention handling the mouse&#8217; world position. Not only that, but it would certainly be nice to have some basic, simple, and reusable classes for rendering your entities so that you don&#8217;t ever have to deal with it again.</p>
<p>These are the issues I will be covering in today&#8217;s article, and of course I will wrap up by showing the code that I use and handing you the source to play with. I&#8217;ve made a few slight changes to some of the classes which I will talk about below.<span id="more-128"></span></p>
<h2>Game World: A Central location for managing your objects</h2>
<p>One of the benefits of creating a manager for the game&#8217;s objects is that you can eliminate errors caused by adding or removing entities in the middle of some logic. The manager should queue up additions and removals and then handle them all at once at the end of the update function.</p>
<p>The GameWorld itself is an Entity. It&#8217;s dimensions are the size that you wish your world to be, whether that be the size of the screen or a larger scrolling area.</p>
<p>Collision detection can be handled through the GameWorld class as well. What I have done is set up a hook function that calls an class: ICollisionResponse. If you wish to handle collisions within the GameWorld, you can simply create a class that does this by plugging into that interface.</p>
<p>Finally, We will use the Game World class to set up our GameCamera and activate a helpful &#8220;worldMousePos&#8221; variable within the Input class. Those I will talk about shortly.</p>
<p>The code is quite short and simple. Here is the Interface:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameWorld <span style="color: #0066CC;">extends</span> Entity
<span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Add collision response to the Game World</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> collisionResponse<span style="color: #66cc66;">&#40;</span> a_value:ICollisionResponse <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> m_collisionResponse = a_value; <span style="color: #66cc66;">&#125;</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">camera</span>:GameCamera; <span style="color: #808080; font-style: italic;">// The camera is left public since it may be accessed often.</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// The Constructor will initialize the camera and the collision response if necessary.</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GameWorld<span style="color: #66cc66;">&#40;</span> a_worldSize:Vector2D, a_cameraSize:Vector2D, a_collisionResponse:ICollisionResponse = <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// We can set the size of the world here</span>
 	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSize<span style="color: #66cc66;">&#40;</span> a_width:<span style="color: #0066CC;">int</span>, a_height:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Here we update the entities, process add and removes, and handle collisions</span>
 	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span> a_timePassed:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
 	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> dispose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// We override these and queue up the operations rather than doing them immediately</span>
 	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> addChild<span style="color: #66cc66;">&#40;</span>a_entity:Entity<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
 	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> removeChild<span style="color: #66cc66;">&#40;</span>a_id:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Also the collision response interface is quite small, requiring only one function to operate:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ICollisionResponse
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> handleCollision<span style="color: #66cc66;">&#40;</span> a_entity1:Entity, a_entity2:Entity <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Using a Game Camera to Assist With Rendering</h2>
<p>In order to display the position of entities in a scrolling world, an inexperienced programmer might choose to simply move all other objects in relation to the main character. This is a very inflexible way to handle things. The more sensible approach would be to create a camera entity which all other entities will render thier locations based off of.</p>
<p>The camera class may also allow for nice effects such as movie style camera panning and zooming. For this version of the camera class, we will create a restraint that makes sure that the camera does not pan past the bounds of the game world. Also we will give it a function if called will cause it to follow a position by passing reference to a Vector2D object ( one of the many benefits of using the class, rather than a basic x/y variable )</p>
<p>Once again, the interface is fairly simple:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameCamera <span style="color: #0066CC;">extends</span> Entity
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const MOVE:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;camera_move_event&quot;</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GameCamera<span style="color: #66cc66;">&#40;</span> a_gameWorld:GameWorld, a_width:<span style="color: #0066CC;">Number</span>, a_height:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSize<span style="color: #66cc66;">&#40;</span> a_width:<span style="color: #0066CC;">int</span>, a_height:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span> timePassed:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// The camera will lock onto this position</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> followPos<span style="color: #66cc66;">&#40;</span> a_pos:Vector2D <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Simple Reusable Rendering Classes</h2>
<p>Obviously with this game model the actual rendering of the entities has been seperated from the data and logic. In order to speed up and simplify production of any new projects as well as testing we create several rendering classes to make our lives easier.</p>
<p>These next few basic renderers will handle rendering different data types such as bitmaps and sprites. Also I have written somewhat of a &#8220;debug&#8221; entity renderer which simply renders the radius and rotation of the entity. ( We don&#8217;t always have graphics to attach to our objects right away! )</p>
<p>First off, I use an abstract Renderer function to supply the interface and the common functions that are used for most renderers. The abstract class will use hook functions that are expected to be overridden by subclasses to provide the full functionality. Also, Renderers are considered &#8220;Game Objects&#8221; so they will inherit that interface.</p>
<p>What the renderer class does is take whatever graphics you wish to display for the entity and place it inside of a &#8220;container.&#8221; Each update the container is positioned on the screen relative to the camera based on the entities position in the world. If the entity is scaled, the container will handle that scaling operation. Also on creation the renderer will center the registration point to the graphics being displayed, since that is the most common and easiest way to deal with graphics in games.</p>
<p>If the viewable area of the entity is off of the screen then the renderer will set it&#8217;s visible property to false, to increase rendering speed. Ultimately the only thing you have to worry about is setting up the renderer initially and it will take care of the rest. That is the idea.</p>
<p>Here is the interface for the abstract renderer class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ARenderer <span style="color: #0066CC;">extends</span> EventDispatcher <span style="color: #0066CC;">implements</span> IGameObject
<span style="color: #66cc66;">&#123;</span>	
&nbsp;
        <span style="color: #808080; font-style: italic;">// the a_canvas variable will be the layer which the container is added to.</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ARenderer<span style="color: #66cc66;">&#40;</span> a_entity:Entity, a_camera:GameCamera, a_canvas:DisplayObjectContainer <span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// It is not recommended to override this function unless absolutely necessary.</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span> timePassed:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> dispose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// -- PRIVATE --</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Handle any updating of the entity representation here</span>
	protected <span style="color: #000000; font-weight: bold;">function</span> draw<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Update values m_viewBoxX, m_viewBoxY, m_viewBoxWidth, m_vewBoxHeight
	 * so that the culling will work correctly.
	 *
	 */</span>
	protected <span style="color: #000000; font-weight: bold;">function</span> updateViewBox<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// The view box was left as individual variables rather than using</span>
        <span style="color: #808080; font-style: italic;">// the AABB data structure in order to speed up the renderer class.</span>
  	protected <span style="color: #000000; font-weight: bold;">var</span> _viewBoxX:<span style="color: #0066CC;">Number</span>;
  	protected <span style="color: #000000; font-weight: bold;">var</span> _viewBoxY:<span style="color: #0066CC;">Number</span>;
  	protected <span style="color: #000000; font-weight: bold;">var</span> _viewBoxWidth:<span style="color: #0066CC;">Number</span>;
  	protected <span style="color: #000000; font-weight: bold;">var</span> _viewBoxHeight:<span style="color: #0066CC;">Number</span>;
        <span style="color: #808080; font-style: italic;">// The canvas is the sprite which will be rendered to the screen</span>
  	protected <span style="color: #000000; font-weight: bold;">var</span> _canvas:DisplayObjectContainer;
        <span style="color: #808080; font-style: italic;">// The container is added to the canvas, and contains the actual graphics used</span>
        <span style="color: #808080; font-style: italic;">// to represent the entity.</span>
	protected <span style="color: #000000; font-weight: bold;">var</span> _container:Sprite;
  	protected <span style="color: #000000; font-weight: bold;">var</span> _entity:Entity;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The three renderers I&#8217;ve created for this article are the EntityRenderer (for debug rendering), Sprite Renderer, and the BitmapRenderer. They are pretty basic:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> boundsColor:uint;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> EntityRenderer<span style="color: #66cc66;">&#40;</span> a_entity:Entity, a_camera:GameCamera, a_canvas:DisplayObjectContainer <span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BitmapRenderer<span style="color: #66cc66;">&#40;</span> a_entity:Entity, a_camera:GameCamera, a_canvas:DisplayObjectContainer, a_bitmapData:BitmapData <span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SpriteRenderer<span style="color: #66cc66;">&#40;</span> a_entity:Entity, a_camera:GameCamera, a_canvas:DisplayObjectContainer, a_sprite:Sprite</pre></div></div>

<h2>Game World Renderer</h2>
<p>Finally, the GameWorldRenderer will contain the main canvas which all of the containers should be added to. Also, it it will manage entity layers for us. Here is the interface:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BitmapRenderer<span style="color: #66cc66;">&#40;</span> a_entity:Entity, <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameWorldRenderer <span style="color: #0066CC;">extends</span> ARenderer
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">private</span> const MAX_LAYERS:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">16</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// a_renderSurface should be the stage, or any other display object on stage</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GameWorldRenderer<span style="color: #66cc66;">&#40;</span> a_world:GameWorld, a_renderSurface:DisplayObjectContainer <span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Use this function to get the a_canvas param for the ARenderer constructor</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLayer<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span> : Sprite
&nbsp;
        <span style="color: #808080; font-style: italic;">// If the canvas is not to be rendered at 0,0 then move it here.</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moveCanvas<span style="color: #66cc66;">&#40;</span> x:<span style="color: #0066CC;">int</span>, y:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span> timePassed:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> dispose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Changes to preexisting code</h2>
<p>I&#8217;ve made some slight modifications to the Input, Entity, and MovingEntity classes. I added a &#8220;worldMousePos&#8221; variable to the Input class, which can be activated once the GameWorld is created by calling:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> activateWorldMouse<span style="color: #66cc66;">&#40;</span> a_gameWorld:GameWorld, a_canvasX:<span style="color: #0066CC;">int</span>, a_canvasY:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span></pre></div></div>

<p>I added the &#8220;layer&#8221; and &#8220;isCollidable&#8221; variables to the Entity class, and fixed a problem with the MovingEntity&#8217;s rotation.</p>
<h2>Flying Dogs!</h2>
<p>For the example I just used some images from google to represent a background and some entities. Hold down the mouse to cause chaos! The camera will lock on and follow the last entity generated.
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ObjectManagement_2103709703"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.cheezeworld.com/files/ObjectManagement.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.cheezeworld.com/files/ObjectManagement.swf"
			name="fm_ObjectManagement_2103709703"
			width="550"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Be sure to take a look at the source code and the example to get a better understanding of how everything is working. You can download all of the files at the <a href="http://code.google.com/p/cheezeworld/">google project page here.</a></p>
<h2>So what&#8217;s next?</h2>
<p>Now that we have greatly simplified rendering and managing all of our game objects, the next thing for us to do is to centralize all of our object creation to factory classes. As of now, it is quite tedious to create our entities, and prone to errors and problems if later down the line we wish to change how objects are created. I will be discussing this in the next article.</p>
<p>Until then, be sure to comment with your thoughts and questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://cheezeworld.com/game-world/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

