<?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>Ant&#039;s Corner</title>
	<atom:link href="http://eyes-squared.co.uk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://eyes-squared.co.uk/blog</link>
	<description>Point, Click, Win!</description>
	<lastBuildDate>Fri, 18 May 2012 19:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Procedural Cityscape</title>
		<link>http://eyes-squared.co.uk/blog/procedural-cityscape/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=procedural-cityscape</link>
		<comments>http://eyes-squared.co.uk/blog/procedural-cityscape/#comments</comments>
		<pubDate>Fri, 18 May 2012 19:39:11 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Arty]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=1009</guid>
		<description><![CDATA[Hey Here&#8217;s my entry to the 2nd Procedurally Generated Graphics Contest on the Kongregate forums. It&#8217;s also a preview of the next tutorial I&#8217;ll be doing! Here we go: (Click to generate, press any key to save a screenshot) I&#8217;m &#8230; <a href="http://eyes-squared.co.uk/blog/procedural-cityscape/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hey <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here&#8217;s my entry to the 2nd <a href="http://www.kongregate.com/forums/4-game-programming/topics/262353-pggc-2-cities" target="_blank">Procedurally Generated Graphics Contest</a> on the Kongregate forums. It&#8217;s also a preview of the next tutorial I&#8217;ll be doing!</p>

<a href='http://eyes-squared.co.uk/blog/procedural-cityscape/cityscape-2/' title='Cityscape'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape-150x150.png" class="attachment-thumbnail" alt="City 1 - all purple and junk!" title="Cityscape" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-cityscape/cityscape2/' title='Cityscape2'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape2-150x150.png" class="attachment-thumbnail" alt="For the tree people out there" title="Cityscape2" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-cityscape/cityscape3/' title='Cityscape3'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape3-150x150.png" class="attachment-thumbnail" alt="Desaturated, for your pleasure..." title="Cityscape3" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-cityscape/cityscape4/' title='Cityscape4'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape4-150x150.png" class="attachment-thumbnail" alt="Overkill on the purple in these shots maybe" title="Cityscape4" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-cityscape/cityscape5/' title='Cityscape5'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape5-150x150.png" class="attachment-thumbnail" alt="Moon and Bokeh collide" title="Cityscape5" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-cityscape/cityscape6/' title='Cityscape6'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape6-150x150.png" class="attachment-thumbnail" alt="Mystical" title="Cityscape6" /></a>

<p>Here we go: (Click to generate, press any key to save a screenshot)</p>
<p align="center"><embed type="application/x-shockwave-flash" width="640" height="320" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/05/Cityscape.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="Cityscape" bgcolor="#000000" quality="high"></embed></p>
<p>I&#8217;m fairly pleased with the end result but had planned a LOT more. I&#8217;ve had to stop early to begin marking a mountain of work; I could quite happily work on this for a long while (and probably will do in months to come). My plan had 16 different &#8216;cities&#8217; &#8211; as you can see, I&#8217;ve only managed 2 of them so far.</p>
<p>Hope you enjoy the results, I&#8217;ll see you in the tutorial!</p>
<div id="tweetbutton1009" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fprocedural-cityscape%2F&amp;text=Procedural%20Cityscape&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fprocedural-cityscape%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/procedural-cityscape/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a &#8216;Copter&#8217; style game &#8211; The Projects</title>
		<link>http://eyes-squared.co.uk/blog/making-a-copter-style-game-the-projects/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-a-copter-style-game-the-projects</link>
		<comments>http://eyes-squared.co.uk/blog/making-a-copter-style-game-the-projects/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 20:20:53 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=966</guid>
		<description><![CDATA[Hello &#8211; April&#8217;s project is here at last! Nice and simple this time around; we&#8217;re going to make a game like &#8216;Copter&#8217;. I&#8217;ve covered this in the video but I&#8217;ll mention it here too: Copying games can be a nice &#8230; <a href="http://eyes-squared.co.uk/blog/making-a-copter-style-game-the-projects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hello &#8211; April&#8217;s project is here at last!</p>
<p>Nice and simple this time around; we&#8217;re going to make a game like &#8216;Copter&#8217;. I&#8217;ve covered this in the video but I&#8217;ll mention it here too: Copying games can be a nice way to learn, just don&#8217;t make a habit of it. If you&#8217;re developing something for commercial purposes you&#8217;re not going to make any friends by ripping other people off. The people who made the original Copter can&#8217;t lay claim the genre, so feel free to do your own thing with it (as <a href="http://itunes.apple.com/gb/app/jetpack-joyride/id457446957?mt=8" target="_blank">Halfbrick have done with &#8216;Jetpack Joyride&#8217;</a>) but make sure it is unique enough to not be considered a clone.</p>
<p>With that out of the way, hop into the video to make something like this:</p>
<p align="center"><embed type="application/x-shockwave-flash" width="640" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/04/Copter.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="Copter" bgcolor="#000000" quality="high"></embed></p>
<p><span id="more-966"></span></p>
<p><strong>Video</strong>:</p>
<p><iframe width="640" height="480" src="http://www.youtube.com/embed/eIgyHAmfJIM" frameborder="0" allowfullscreen></iframe></p>
<p><strong>Source files</strong>:</p>
<ul>
<li><a href='http://eyes-squared.co.uk/blog/wp-content/uploads/2012/04/EndOfPart1.zip'>EndOfPart1</a></li>
<li><a href='http://eyes-squared.co.uk/blog/wp-content/uploads/2012/04/EndOfPart2.zip'>EndOfPart2</a></li>
<li><a href='http://eyes-squared.co.uk/blog/wp-content/uploads/2012/04/EndOfPart3.zip'>EndOfPart3</a></li>
</ul>
<p>The video doesn&#8217;t cover everything from the sample game above, so there are plenty of extra bits for you to look in to. As a rough guide, the clouds are basically the same as the blocks (without collision) and the steam at the back of the chopper is very similar to the explosion pieces. I used the <a href="http://eyes-squared.co.uk/blog/randomly-generated-hills-the-projects/" target="_blank">random hills project</a> as a background and various other little techniques from the <a href="http://eyes-squared.co.uk/blog/actionscript-3-tutorials/" target="_blank">Point, Click, Win! tutorial series</a> to do the rest.</p>
<p>Your turn! Show me what you&#8217;ve got <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Ant</p>
<div id="tweetbutton966" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fmaking-a-copter-style-game-the-projects%2F&amp;text=Making%20a%20%26%238216%3BCopter%26%238217%3B%20style%20game%20%26%238211%3B%20The%20Projects&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fmaking-a-copter-style-game-the-projects%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/making-a-copter-style-game-the-projects/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>1 year later</title>
		<link>http://eyes-squared.co.uk/blog/1-year-later/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=1-year-later</link>
		<comments>http://eyes-squared.co.uk/blog/1-year-later/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 00:03:21 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=962</guid>
		<description><![CDATA[Ant&#8217;s Corner is 1 year old &#8211; how time flies! I&#8217;m away on holiday for a little longer but, don&#8217;t worry, I haven&#8217;t dropped off the planet and I am still open to tutorial suggestions for when I get back &#8230; <a href="http://eyes-squared.co.uk/blog/1-year-later/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ant&#8217;s Corner is 1 year old &#8211; how time flies!</p>
<p>I&#8217;m away on holiday for a little longer but, don&#8217;t worry, I haven&#8217;t dropped off the planet and I am still open to tutorial suggestions for when I get back from my travels. I obviously haven&#8217;t been able to answer emails and/or YouTube comments recently (and that will continue for the rest of the week) but try again if you&#8217;re still stuck next week and I should be able to respond.</p>
<p>Happy Easter and Antiversary! <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="tweetbutton962" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2F1-year-later%2F&amp;text=1%20year%20later&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2F1-year-later%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/1-year-later/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Game highlight &#8211; GemCraft Labyrinth</title>
		<link>http://eyes-squared.co.uk/blog/game-highlight-gemcraft-labyrinth/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=game-highlight-gemcraft-labyrinth</link>
		<comments>http://eyes-squared.co.uk/blog/game-highlight-gemcraft-labyrinth/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 10:46:36 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=932</guid>
		<description><![CDATA[A shining example of a tower defense game &#8211; and of Flash games in general. There are plenty more in the GemCraft series; go play them all&#8230; now! Click to play GemCraft Labyrinth &#160; Tweet]]></description>
			<content:encoded><![CDATA[<p><a href="http://eyes-squared.co.uk/blog/gemcraft-labyrinth/"><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/thumb_100x100.png" alt="" title="GemCraft thumbnail" width="100" height="100" style="float:left; margin-right:20px; margin-bottom:20px;"/></a>A shining example of a tower defense game &#8211; and of Flash games in general. There are plenty more in the GemCraft series; go play them all&#8230; now!</p>
<div align="center"><a href='http://eyes-squared.co.uk/blog/gemcraft-labyrinth/'>Click to play GemCraft Labyrinth</a></div>
<p>&nbsp;</p>
<div id="tweetbutton932" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fgame-highlight-gemcraft-labyrinth%2F&amp;text=Game%20highlight%20%26%238211%3B%20GemCraft%20Labyrinth&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fgame-highlight-gemcraft-labyrinth%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/game-highlight-gemcraft-labyrinth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Procedural Forest</title>
		<link>http://eyes-squared.co.uk/blog/procedural-forest/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=procedural-forest</link>
		<comments>http://eyes-squared.co.uk/blog/procedural-forest/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 21:52:46 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Arty]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=914</guid>
		<description><![CDATA[I saw this post on Kongregate and decided to have a go! The challenge is to generate a random forest with no drawings or imported graphics. The end result can be a computer hog, so only click it if you&#8217;re &#8230; <a href="http://eyes-squared.co.uk/blog/procedural-forest/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I saw <a href="http://www.kongregate.com/forums/4-game-programming/topics/244475-pggc-1-forest" target="_blank">this post</a> on Kongregate and decided to have a go! The challenge is to generate a random forest with no drawings or imported graphics.</p>
<p>The end result can be a computer hog, so only click it if you&#8217;re not doing anything too important <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Here are some pictures of various results:<br />

<a href='http://eyes-squared.co.uk/blog/procedural-forest/img1/' title='Img1'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Img1-150x150.png" class="attachment-thumbnail" alt="Img1" title="Img1" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-forest/img2/' title='Img2'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Img2-150x150.png" class="attachment-thumbnail" alt="Img2" title="Img2" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-forest/img3/' title='Img3'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Img3-150x150.png" class="attachment-thumbnail" alt="Img3" title="Img3" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-forest/img4/' title='Img4'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Img4-150x150.png" class="attachment-thumbnail" alt="Img4" title="Img4" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-forest/img5/' title='Img5'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Img5-150x150.png" class="attachment-thumbnail" alt="Img5" title="Img5" /></a>
<a href='http://eyes-squared.co.uk/blog/procedural-forest/img6/' title='Img6'><img width="150" height="150" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Img6-150x150.png" class="attachment-thumbnail" alt="Img6" title="Img6" /></a>
</p>
<p>And here&#8217;s the SWF:</p>
<p align="center"><embed type="application/x-shockwave-flash" width="600" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Trees2.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="Forest" bgcolor="#333333" quality="high"></embed></p>
<div id="tweetbutton914" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fprocedural-forest%2F&amp;text=Procedural%20Forest&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fprocedural-forest%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/procedural-forest/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pathfinding &#8211; The Projects</title>
		<link>http://eyes-squared.co.uk/blog/pathfinding-the-projects/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=pathfinding-the-projects</link>
		<comments>http://eyes-squared.co.uk/blog/pathfinding-the-projects/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 13:05:25 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Freebies]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=789</guid>
		<description><![CDATA[Hey PointClickWinners After a lot of failed attempts to make this as a video, I&#8217;ve resigned myself to a written tutorial. I found I was waffling too much about pointless stuff &#8211; we can attempt to cut to the chase &#8230; <a href="http://eyes-squared.co.uk/blog/pathfinding-the-projects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hey PointClickWinners <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>After a lot of failed attempts to make this as a video, I&#8217;ve resigned myself to a written tutorial. I found I was waffling too much about pointless stuff &#8211; we can attempt to cut to the chase here.</p>
<p>So, here&#8217;s a little example of the sort of product you could use pathfinding for (if you fancy yourself as a Frankenstein-esque, life-giving, scientist type):</p>
<p align="center"><embed type="application/x-shockwave-flash" width="600" height="350" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Town.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="PathfindingTown" bgcolor="#006231" quality="high"></embed></p>
<p align="center"><em>Live my pretties!</em></p>
<p>Before you continue, <strong>make sure you have read <a href="http://www.policyalmanac.org/games/aStarTutorial.htm" target="_blank">this page on A* pathfinding theory</a></strong>. The link suitably covers the logic we&#8217;ll be working to throughout this tutorial.</p>
<p align="center">Click to download the <a href="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/PathfindingFiles.zip">tutorial source files</a></p>
<p><span id="more-789"></span><br />
<strong>Explanation of grid files:</strong> (Click <a href="#using">here</a> to skip straight to &#8216;Using the grid files&#8217;)</p>
<p>The chances are, if you&#8217;re at a level suitable for attempting pathfinding, grids are fairly trivial to you &#8211; but I&#8217;ll explain it anyway!</p>
<p>Before starting any pathfinding, I created 2 classes that combine to make a basic grid: SimpleCell and SimpleGrid. Now, there are hundreds of ways to implement grids and to store the information about cells for use in pathfinding; this is just a quick one put together to provide a platform for the pathfinding. The SimpleCell stores information about its position in the grid and the f, g, h and parent values mentioned in <a href="http://www.policyalmanac.org/games/aStarTutorial.htm" target="_blank">the theory</a>. You could write a pathfinding system that generates all this information each time the grid is used &#8211; but that requires making and deleting a lot of objects dynamically. This setup makes the grid and cells at the start, then simply reuses them in the pathfinding logic.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package &nbsp;<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// no imports, no extension</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleCell <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// public variables - all will be used in the other classes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> value:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the cell's value - 0 for passable</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> x:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// x position in grid (not in the world)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> y:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// y position in grid</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> g:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// g score</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> h:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// heuristic</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> parent:SimpleCell; &nbsp; <span style="color: #808080; font-style: italic;">// parent cell - for pathfinding purpose.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the constructor takes a value plus x and y coordinates</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleCell<span style="color: #66cc66;">&#40;</span>v:<span style="color: #0066CC;">int</span>, sx:<span style="color: #0066CC;">int</span>, sy:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = v;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x=sx;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y=sy;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// f is a get function - we don't really need to store it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> f<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> g+h;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The whole SimpleCell class in one go; it&#8217;s a glorified collections of numbers, really. Public variables are used because the pathfinding will eventually need access to everything. This could be done with private variables and get/set functions if you like. The numbers are all integers to keep things simple; the pathfinding would be more accurate if floating point numbers were used but it&#8217;d be more expensive in terms of processing time. The f value isn&#8217;t stored, it&#8217;s simply calculated on request by adding g and h (<a href="http://www.policyalmanac.org/games/aStarTutorial.htm" target="_blank">theory page</a>!). The constructor takes a cell value (in this tutorial, 0 means walkable and any other number is a wall), an x and a y position for the cell.</p>
<p align="center"><em>&#8220;Simples&#8221;</em>.</p>
<p>The SimpleGrid has a bit more to it, so I&#8217;ll break it down into sections:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package &nbsp;<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// import the Sprite class so we can draw the grid if requested</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// no extension</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleGrid <span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid:<span style="color: #0066CC;">Array</span>; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// a 1-dimensional array to store the cells</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_width</span>:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the width of the grid in cells, not pixels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_height</span>:<span style="color: #0066CC;">int</span>;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the height of the grid in cells.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> cellSize:<span style="color: #0066CC;">int</span>;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the width and height of each cell in pixels</span></div></div>
<p>The class doesn&#8217;t extend anything, because it&#8217;s just a bunch of numbers and some functions to process them. However, Sprite has been imported so the class can draw itself to another object if required. There&#8217;s no reason for the grid, width or height to be edited from outside in this example; if you want a dynamic grid, you might reconsider this &#8211; or at least add some functionality to alter the value of cells.</p>
<p>The array is 1-dimensional (what, for a 2D grid?? Madness!) because it&#8217;s easier to draw out in code and, arguably, faster to process at runtime. I&#8217;ll explain this a bit more further down</p>
<p>The width and height properties will have get functions (explained in the <a href="http://eyes-squared.co.uk/blog/randomly-generated-hills-the-projects/" title="Randomly Generated Hills – The Projects" target="_blank">random hills project</a>), so they are led by an underscore. The cellSize represents the size, in pixels, of each cell and is public so that the characters can use it later on (again, this could have just been a get function).</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// pass a width, height, cell size and cell array to the constructor</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleGrid<span style="color: #66cc66;">&#40;</span>w:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">10</span>, h:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">10</span>, c:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">10</span>, <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set width and height, don't allow less than 2 on each axis</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">_width</span> = <span style="color: #66cc66;">&#40;</span>w<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #cc66cc;">2</span> : w;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">_height</span> = <span style="color: #66cc66;">&#40;</span>h<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #cc66cc;">2</span> : h;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set cell size</span><br />
&nbsp; &nbsp; cellSize=c;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the user didn't pass in an array, make it a blank one</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">array</span>==<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0066CC;">array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// populate the grid (finish it if an incomplete one was passed).</span><br />
&nbsp; &nbsp; grid = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>grid.<span style="color: #006600;">length</span><span style="color: #66cc66;">&lt;</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">_height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">array</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SimpleCell<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#91;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grid.<span style="color: #006600;">length</span><span style="color: #66cc66;">%</span><span style="color: #0066CC;">_width</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SimpleCell<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grid.<span style="color: #006600;">length</span><span style="color: #66cc66;">%</span><span style="color: #0066CC;">_width</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p> The constructor takes 4 pieces of information:</p>
<ol>
<li>A value for width &#8211; as mentioned above, this is the width, in cells, of the grid</li>
<li>A value for height &#8211; as above but for height!</li>
<li>A value for cellSize &#8211; this is the width and height of each cell of the grid in pixels</li>
<li>An array of cell values &#8211; <strong>not SimpleCells</strong> &#8211; a list of integers to determine which cells are passable</li>
</ol>
<p>The first 2 lines of code in the body set the width and height of the grid based on the values passed in. If they look odd, it&#8217;s because they make use of the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#conditional" target="_blank">ternary or conditional operator</a> &#8211; not something I&#8217;ve ever covered in a tutorial. It&#8217;s a shorthand version of an if/else statement&#8230; so:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">_width</span> = <span style="color: #66cc66;">&#40;</span>w<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #cc66cc;">2</span> : w;</div></div>
<p>Could actually be written as:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>w<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">_width</span>=<span style="color: #cc66cc;">2</span>;<br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">_width</span>=w;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>They mean the same thing. There&#8217;s no noticeable performance boost from using the short version (possibly the opposite) but given that this tutorial is quite advanced, it can&#8217;t hurt to add stuff like this in for some extra learning <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Back to the constructor&#8230;</p>
<p>The cellSize, array and grid being set are nothing new &#8211; we&#8217;re just making sure they exist to prevent errors in future. The section developing the grid array, however, is new. Let&#8217;s take a closer look:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>grid.<span style="color: #006600;">length</span><span style="color: #66cc66;">&lt;</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">_height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">array</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; grid.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SimpleCell<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#91;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grid.<span style="color: #006600;">length</span><span style="color: #66cc66;">%</span><span style="color: #0066CC;">_width</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; grid.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SimpleCell<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grid.<span style="color: #006600;">length</span><span style="color: #66cc66;">%</span><span style="color: #0066CC;">_width</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>grid.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The while loop will run until the grid array&#8217;s length matches the grid&#8217;s area (<em>width</em> x <em>height</em> for the area of a rectangle). The if statement decides if there was enough information passed in to the constructor to populate this cell. If there was, we use the given information. If not, we generate a default (walkable in this case) cell.</p>
<p>The trickiest part of the constructor is figuring out the x and y positions of the cells when working with a 1-dimensional array. The image below has a comparison of 2 grids: one is stored in a 2D array (an array of arrays), the other is 1D:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid1.png" alt="" title="Grid1" width="551" height="388" class="aligncenter size-full wp-image-829" /></p>
<p>The text to the right of the grid explains where the cells are located in the array(s). In our array (1D) we take the x value and add y*_width to find that particular cell. In our current predicament, we need to reverse engineer this sum to get the x and the y from the final index.</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid2.png" alt="" title="Grid2" width="152" height="81" class="aligncenter size-full wp-image-833" /></p>
<p>This image shows the formula rearranged to make x or y the subject. In our code, the index is grid.length (the next element to be added to the grid array), so we substitute it in. If the % symbol is foreign to you, it means <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#modulo" target="_blank">Modulo</a> and it gives the remainder of number 1 after dividing by number 2. I also changed the y formula a touch &#8211; removing the need to take x away from the index by dismissing anything after a decimal point (by converting to int).</p>
<p><em>Yikes&#8230; can you see why I was struggling to cram all of this into an hour long video yet?</em></p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// getCell - return the cell at x,y</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCell<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>:SimpleCell <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> x<span style="color: #66cc66;">&lt;</span><span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">&amp;&amp;</span> y<span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> y<span style="color: #66cc66;">&lt;</span><span style="color: #0066CC;">_height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> grid<span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">*</span><span style="color: #0066CC;">_width</span>+x<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCell<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1</span>,x,y<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// return an impassable cell if out of bounds</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>An easy one, yey! This function simply returns the cell at the specified position, using the formula for index mentioned earlier. The if checks that x and y are in the grid&#8217;s range; if not, a new, impassable cell is returned.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// get functions for height and width</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">_width</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">_height</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Get functions for the width and height, not much to see here!</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// get the cells adjacent to the specified cell if they are accessible and walkable</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAdjacents<span style="color: #66cc66;">&#40;</span>cell:SimpleCell<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> adjacents = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the cell isn't part of the grid, return an empty list</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>grid.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>cell<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> adjacents;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// loop around the cell and determine if the cells are valid</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> ix=cell.<span style="color: #006600;">x</span>-<span style="color: #cc66cc;">1</span>; ix<span style="color: #66cc66;">&lt;</span>cell.<span style="color: #006600;">x</span>+<span style="color: #cc66cc;">2</span>; ix++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> iy=cell.<span style="color: #006600;">y</span>-<span style="color: #cc66cc;">1</span>; iy<span style="color: #66cc66;">&lt;</span>cell.<span style="color: #006600;">y</span>+<span style="color: #cc66cc;">2</span>; iy++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> potential:SimpleCell = getCell<span style="color: #66cc66;">&#40;</span>ix, iy<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>potential.<span style="color: #006600;">value</span>==<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> potential<span style="color: #66cc66;">!</span>=cell <span style="color: #66cc66;">&amp;&amp;</span> passable<span style="color: #66cc66;">&#40;</span>cell, potential<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjacents.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>potential<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> adjacents;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>This function returns an array of cells adjacent to the specified cell, provided that cell actually belongs to the grid. The comments sum it up fairly well but I should point out that it makes use of the following function to decide whether diagonal cells are accessible from the current position. Remember, the getCell function from earlier will return an impassable cell if the supplied position was out of bounds; bear that in mind if you ever choose to change the way that function works.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// a function to determine if movement between 2 cells is possible - checks diagonals</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> passable<span style="color: #66cc66;">&#40;</span>c1:SimpleCell, c2:SimpleCell<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the x or y values of the cells are equal - movement is possible </span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>c1.<span style="color: #006600;">x</span>==c2.<span style="color: #006600;">x</span> <span style="color: #66cc66;">||</span> c1.<span style="color: #006600;">y</span>==c2.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// ternary operator use to determine starting x and y values</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> startX = <span style="color: #66cc66;">&#40;</span>c1.<span style="color: #006600;">x</span><span style="color: #66cc66;">&gt;</span>c2.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> ? c2.<span style="color: #006600;">x</span> : c1.<span style="color: #006600;">x</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> endX = <span style="color: #66cc66;">&#40;</span>c1.<span style="color: #006600;">x</span><span style="color: #66cc66;">&gt;</span>c2.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> ? c1.<span style="color: #006600;">x</span> : c2.<span style="color: #006600;">x</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> startY = <span style="color: #66cc66;">&#40;</span>c1.<span style="color: #006600;">y</span><span style="color: #66cc66;">&gt;</span>c2.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> ? c2.<span style="color: #006600;">y</span> : c1.<span style="color: #006600;">y</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> endY = <span style="color: #66cc66;">&#40;</span>c1.<span style="color: #006600;">y</span><span style="color: #66cc66;">&gt;</span>c2.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> ? c1.<span style="color: #006600;">y</span> : c2.<span style="color: #006600;">y</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// check a 2x2 square starting from the above position</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if any of the 4 cells are impassable, diagonal movement is impossible</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> x=startX; x<span style="color: #66cc66;">&lt;</span>=endX; x++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> y=startY; y<span style="color: #66cc66;">&lt;</span>=endY; y++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">*</span><span style="color: #0066CC;">_width</span>+x<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">!</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if all 4 cells are passable, return true</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The passable function determines if movement between the given cells is possible. It&#8217;s inaccurate for cells that are not actually touching but we don&#8217;t really need to worry about that given how it is used in this class. There&#8217;s some more use of the ternary operator to decide where the top left point of the check area is. Once the start point is determined, a square of cells finishing at the end point is checked for walkability. If any of the cells are walls, movement is deemed impossible, as below:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid3.png" alt="" title="Grid3" width="396" height="196" class="aligncenter size-full wp-image-841" /></p>
<p>Note that in the right hand image, movement between the 2 cells is technically possible but the function is only there to determine direct movement, so it would return false in that case.</p>
<p>&#8230; and finally:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// draw the grid to a specified Sprite. Pass the sprite in from outside</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> draw<span style="color: #66cc66;">&#40;</span>sprite:Sprite<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// clear graphics and set line style</span><br />
&nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// draw horizontal lines</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>=<span style="color: #0066CC;">_height</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, i<span style="color: #66cc66;">*</span>cellSize<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span>cellSize, i<span style="color: #66cc66;">*</span>cellSize<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// draw verticals</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>=<span style="color: #0066CC;">_width</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">*</span>cellSize,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">*</span>cellSize, <span style="color: #0066CC;">_height</span><span style="color: #66cc66;">*</span>cellSize<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// fill in impassable spaces with solid rectangles</span><br />
&nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>grid.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">!</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>cellSize<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">%</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span>, cellSize<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">/</span><span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>, cellSize, cellSize<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; sprite.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// close class</span><br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// close package!</span></div></div>
<p>This optional function will draw your grid to a given Sprite-based object. For more information about drawing with code, check the <a href="http://eyes-squared.co.uk/blog/randomly-generated-hills-the-projects/" title="Randomly Generated Hills – The Projects" target="_blank">Random Hills project</a>.</p>
<p align="center"><em>Phew!</em></p>
<p><strong id="using">Using the grid files:</strong></p>
<p>Make sure your SimpleCell.as and SimpleGrid.as files are in your working directory. You can make a default 10&#215;10 grid by simply making a new instance of SimpleGrid:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> sg:SimpleGrid = <span style="color: #000000; font-weight: bold;">new</span> SimpleGrid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>The grid will have cells 10 pixels in size and they will all be walkable. You can see this by drawing the cell to any old Sprite/MovieClip object. The code below is thrown into the Actions box on the first keyframe of a new Flash file:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> sg:SimpleGrid = <span style="color: #000000; font-weight: bold;">new</span> SimpleGrid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
sg.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>&#8230; and it produces the following:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid4.png" alt="" title="Grid4" width="552" height="425" class="aligncenter size-full wp-image-858" /></p>
<p>That&#8217;s great and all&#8230; but let&#8217;s add some walls.</p>
<p>The SimpleGrid constructor accepts an array of numbers to generate walls &#8211; 0 for walkable and anything else means a wall. You could cram your array declaration into the SimpleGrid creation line but I&#8217;ll spread things out a little:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> sg:SimpleGrid = <span style="color: #000000; font-weight: bold;">new</span> SimpleGrid<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#41;</span>;<br />
sg.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>Gives us:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid5.png" alt="" title="Grid5" width="552" height="177" class="aligncenter size-full wp-image-861" /></p>
<p>We have a wall! The SimpleGrid class is doing it&#8217;s job and populating the grid with empty cells where we haven&#8217;t provided any information.</p>
<p>Let&#8217;s step it up a notch and make a full grid:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> sg:SimpleGrid = <span style="color: #000000; font-weight: bold;">new</span> SimpleGrid<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#41;</span>;<br />
sg.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>&#8230; makes:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid6.png" alt="" title="Grid6" width="597" height="297" class="aligncenter size-full wp-image-862" /></p>
<p>You get the idea! Now, adjust the values and array you pass into the SimpleGrid class to make a grid to your liking:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</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: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</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: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> sg:SimpleGrid = <span style="color: #000000; font-weight: bold;">new</span> SimpleGrid<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span>,<span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#41;</span>;<br />
sg.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>&#8230; and I&#8217;ll finish this section of the tutorial with:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid7.png" alt="" title="Grid7" width="549" height="422" class="aligncenter size-full wp-image-864" /></p>
<p>Time for some pathfinding I suppose!</p>
<p><strong>Explanation of pathfinding file:</strong> (Click <a href="#pathfinding">here</a> to skip straight to &#8216;Using the pathfinding files&#8217;)</p>
<p>I&#8217;ve tried to stay close to the <a href="http://www.policyalmanac.org/games/aStarTutorial.htm" target="_blank">theory page</a>&#8216;s logic &#8211; so, again, make sure you&#8217;ve read and understood it.
<p>The start of the file is fairly standard:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package &nbsp;<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// No extension needed</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleAStar <span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// private variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> current:SimpleCell; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the current cell being looked at</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid:SimpleGrid;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the grid of all cells</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sx:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// starting x position of path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sy:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// starting y position of path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tx:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// target x position of path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ty:<span style="color: #0066CC;">int</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// target y position of path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> openList:<span style="color: #0066CC;">Array</span>; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// a list of 'open' cells</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> closedList:<span style="color: #0066CC;">Array</span>; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// a list of 'closed' cells</span></div></div>
<p>No import necessary, although mine originally had Sprite and Event imported so I could make it generate over time and draw itself to the screen. Most of the variables used are also remnants of that system; you could generate most (all) of them at the time of use, rather than storing them all the time. The comments explain the variables appropriately, no need to hang around here any longer.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// pass a grid in to the constructor</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleAStar<span style="color: #66cc66;">&#40;</span>theGrid:SimpleGrid<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; grid = theGrid;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The constructor takes and stores a reference to the grid being used. This is actually another remnant of the generate-over-time version of the system and could just as easily be passed into the pathfinding function later on, if you prefer.</p>
<p>Just before the pathfinding begins, I&#8217;m going to explain the function that calculates a cell&#8217;s heuristic (h score):</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// the heuristic (h) generator. Can be done in many different ways</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getH<span style="color: #66cc66;">&#40;</span>x1:<span style="color: #0066CC;">int</span>, y1:<span style="color: #0066CC;">int</span>, x2:<span style="color: #0066CC;">int</span>, y2:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// manhattan method</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> x = <span style="color: #66cc66;">&#40;</span>x1<span style="color: #66cc66;">&gt;</span>x2<span style="color: #66cc66;">&#41;</span> ? x1-x2 : x2-x1;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> y = <span style="color: #66cc66;">&#40;</span>y1<span style="color: #66cc66;">&gt;</span>y2<span style="color: #66cc66;">&#41;</span> ? y1-y2 : y2-y1;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>x+y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">10</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// 'real' method</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2))*10;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>I&#8217;ve gone for the Manhattan method mentioned in the theory page but also left in a slightly more accurate and costly version should you choose to swap. The Manhattan method works, funnily enough, like Manhattan&#8230; it&#8217;s not possible to navigate diagonally through blocks, so the distance is calculated by zig-zagging around cells. The result is the difference between the start and end x position, plus the difference between the start and end y position, all multiplied by 10. See below:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid8.png" alt="" title="Grid8" width="437" height="322" class="aligncenter size-full wp-image-876" /></p>
<p>We multiply by 10 so that the distance is in the same format as the 10 or 14 used to generate the g scores in <a href="http://www.policyalmanac.org/games/aStarTutorial.htm" target="_blank">the theory</a>. With that done, let&#8217;s look at generating the path:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// a function to start the pathfinding and return the end result as an array of cells</span><br />
<span style="color: #808080; font-style: italic;">// pass in start and end coordinates</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPath<span style="color: #66cc66;">&#40;</span>startX:<span style="color: #0066CC;">int</span>, startY:<span style="color: #0066CC;">int</span>, targetX:<span style="color: #0066CC;">int</span>, targetY:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; sx=startX;<br />
&nbsp; &nbsp; sy=startY;<br />
&nbsp; &nbsp; tx=targetX;<br />
&nbsp; &nbsp; ty=targetY;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// open list and closed list</span><br />
&nbsp; &nbsp; openList = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; closedList = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the start or end are impassable, return no path</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>grid.<span style="color: #006600;">getCell</span><span style="color: #66cc66;">&#40;</span>sx,sy<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">!</span>=<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">||</span> grid.<span style="color: #006600;">getCell</span><span style="color: #66cc66;">&#40;</span>tx,ty<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">!</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></div>
<p>Here&#8217;s the first snip of the real pathfinding code. This starts the process by taking start and end co-ordinates from the user (feel free to use 2 Points instead of 4 integers), creates the open and closed lists and checks that the start and end points are valid. You can change the logic if you want your pathfinding to target invalid cells &#8211; you might want a &#8216;near as possible&#8217; type solution when the goal itself is invalid.</p>
<p>Nothing groundbreaking there, so let&#8217;s look at the real brains behind the system:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// do some pathfinding!</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set the start position as the current cell</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// g to 0 and calculate heuristic</span><br />
&nbsp; &nbsp; current=grid.<span style="color: #006600;">getCell</span><span style="color: #66cc66;">&#40;</span>sx,sy<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; current.<span style="color: #006600;">g</span>=<span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; current.<span style="color: #006600;">h</span>=getH<span style="color: #66cc66;">&#40;</span>sx,sy,tx,ty<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set parent to null</span><br />
&nbsp; &nbsp; current.<span style="color: #006600;">parent</span>=<span style="color: #000000; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// add to open list</span><br />
&nbsp; &nbsp; openList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// recursive stuff starts here</span><br />
&nbsp; &nbsp; recurseToGoal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// work backwards from the current cell to get the path</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// go from the current to its parent until the start is reached</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> path:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// optional - draw a line to each cell to show the path (import Sprite)</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>current.<span style="color: #006600;">x</span><span style="color: #66cc66;">!</span>=sx <span style="color: #66cc66;">||</span> current.<span style="color: #006600;">y</span><span style="color: #66cc66;">!</span>=sy<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; current=current.<span style="color: #006600;">parent</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; path.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return the path</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> path;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The cell found at the starting coordinates is stored as the current cell. As it is the first cell in the path, it has a g score of 0 (it&#8217;s 0 distance along the path). We calculate its h (heuristic) with the getH function mentioned earlier and set its parent to null to prevent infinite loops further down the line. Once all the appropriate values are set, we add the starting cell to the open list and start a <em>recursive</em> function to generate the path. If recursive is a new word to you, it means a function that calls itself. Clever use of recursive functions can cram seemingly large calculations into a tiny number of lines of code. Below is a fairly common example; it returns the cumulative total of all numbers up to, and including, a specified number:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid9.png" alt="" title="Grid9" width="503" height="203" class="aligncenter size-full wp-image-879" /></p>
<p>Just before I explain the recurseToGoal function down, let&#8217;s finish off the getPath function:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// work backwards from the current cell to get the path</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// go from the current to its parent until the start is reached</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> path:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// optional - draw a line to each cell to show the path (import Sprite)</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>current.<span style="color: #006600;">x</span><span style="color: #66cc66;">!</span>=sx <span style="color: #66cc66;">||</span> current.<span style="color: #006600;">y</span><span style="color: #66cc66;">!</span>=sy<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; current=current.<span style="color: #006600;">parent</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; path.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//return the path</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> path;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>After the recursive function has done its work, the cells in the grid will contain all the necessary information to generate the path. A new array named path is made and we work backwards from the current cell (which will be the target of the path if any path existed) until we hit the cell we started from.<br />
The cells are added to the start of the path array (hence the use of unshift instead of push) and the current cell is swapped for its parent. If the current cell has the same x and y as the start of the path, we have completed the path and the while loop stops. The final unshift call puts the starting cell at the top of the path and we can finally return the list of cells to whatever requested the path in the first place!</p>
<p>I hope at least some of this is sticking <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Ok, the recurseToGoal function:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// the recursive search - this function reuses itself until a path is generated</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> recurseToGoal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// remove the current cell from the open list</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; openList.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>openList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// add it to the closed list</span><br />
&nbsp; &nbsp; closedList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the current cell is the goal, the path is complete, so return.</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>current.<span style="color: #006600;">x</span>==tx <span style="color: #66cc66;">&amp;&amp;</span> current.<span style="color: #006600;">y</span>==ty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// get a list of cells adjacent to the current cell</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> adjacents:<span style="color: #0066CC;">Array</span> = grid.<span style="color: #006600;">getAdjacents</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// optional - randomly shuffle them to generate varying paths of the same length</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>adjacents.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> checking:SimpleCell = adjacents.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the cell is not on the closed list </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>closedList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>checking<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set its h score</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">h</span>=getH<span style="color: #66cc66;">&#40;</span>checking.<span style="color: #006600;">x</span>, checking.<span style="color: #006600;">y</span>, tx, ty<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if it is already on the open list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>checking<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// check to see if its g score would be lower with the current cell as its parent</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> compG = current.<span style="color: #006600;">g</span>+getDistance<span style="color: #66cc66;">&#40;</span>current, checking<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if so, make the current cell its parent</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>compG<span style="color: #66cc66;">&lt;</span>=checking.<span style="color: #006600;">g</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">parent</span> = current;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">g</span>=compG;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// else, if this adjacent cell wasn't on the open list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// make the current cell its parent and assign a g value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">parent</span>=current;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">g</span>=current.<span style="color: #006600;">g</span>+getDistance<span style="color: #66cc66;">&#40;</span>current,checking<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// add this adjacent cell to the open list for future reference</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>checking<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// after all adjacents have been checked...</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// update the current cell to be the cell with the lowest f score in the open list</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> nextCell:SimpleCell=<span style="color: #000000; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>openList.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>nextCell==<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> openList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">f</span> <span style="color: #66cc66;">&lt;</span> nextCell.<span style="color: #006600;">f</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextCell=openList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if nextCell is null (the open list was empty), there is no path.</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// optional - choose the lowest h score from the closed list as a &quot;that'll do&quot; cell</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>nextCell==<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>closedList.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>closedList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">h</span><span style="color: #66cc66;">&lt;</span>current.<span style="color: #006600;">h</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current=closedList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// else... if there are still cells to recurse through</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set the current cell the be the next cell (decided above)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; current=nextCell;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// call this function again (a function that calls itself = recursive)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; recurseToGoal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p><em>Eek!</em>, better break it down a little:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// the recursive search - this function reuses itself until a path is generated</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> recurseToGoal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// remove the current cell from the open list</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; openList.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>openList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// add it to the closed list</span><br />
&nbsp; &nbsp; closedList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the current cell is the goal, the path is complete, so return.</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>current.<span style="color: #006600;">x</span>==tx <span style="color: #66cc66;">&amp;&amp;</span> current.<span style="color: #006600;">y</span>==ty<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></div>
<p>The function takes no additional information as it is all saved in the class variables. Following the logic of the theory site, we removed the current cell from the open list (there&#8217;s no reason for it not being in the open list but I put a check just in case). We then add it to the closed list.</p>
<p>If the current cell is the cell we are trying to reach, we end the pathfinding with a return command.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// get a list of cells adjacent to the current cell</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> adjacents:<span style="color: #0066CC;">Array</span> = grid.<span style="color: #006600;">getAdjacents</span><span style="color: #66cc66;">&#40;</span>current<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// optional - randomly shuffle them to generate varying paths of the same length</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>adjacents.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> checking:SimpleCell = adjacents.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the cell is not on the closed list </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>closedList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>checking<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set its h score</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">h</span>=getH<span style="color: #66cc66;">&#40;</span>checking.<span style="color: #006600;">x</span>, checking.<span style="color: #006600;">y</span>, tx, ty<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if it is already on the open list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openList.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>checking<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// check to see if its g score would be lower with the current cell as its parent</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> compG = current.<span style="color: #006600;">g</span>+getDistance<span style="color: #66cc66;">&#40;</span>current, checking<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if so, make the current cell its parent</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>compG<span style="color: #66cc66;">&lt;</span>=checking.<span style="color: #006600;">g</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">parent</span> = current;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">g</span>=compG;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// else, if this adjacent cell wasn't on the open list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// make the current cell its parent and assign a g value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">parent</span>=current;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checking.<span style="color: #006600;">g</span>=current.<span style="color: #006600;">g</span>+getDistance<span style="color: #66cc66;">&#40;</span>current,checking<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// add this adjacent cell to the open list for future reference</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>checking<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></div>
<p>We make use of the getAdjacents function from the SimpleGrid class to get a list of cells to check. You&#8217;ll need to do this differently depending on your grid/nodes. Note: at this point you could shuffle the array to make the pathfinding a little more random; as it stands, the system will take the same route for a path every time that path is checked. That might not sound too bad (it&#8217;s not, really) but you might want your characters to go down and around a single piece of wall once in a while, rather than up and around every single time.</p>
<p>For each cell in the list of adjacents, we check to see if it is already in the closed list. If it isn&#8217;t, we set its h score with the getH function from earlier. If the cell being checked is already on the open list, we figure out what its g score would be if the current cell was its parent. To calculate the g score we&#8217;re using a very basic function called getDistance (explained further down) that returns either 10 or 14 depending on the distance from the current cell to the one being checked. If the new g score is smaller, we make the current cell the parent of the one being checked.</p>
<p>If the cell being checked is not already on the open list, we calculate its g score and set the current cell as its parent, before adding it to the open list.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// after all adjacents have been checked...</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// update the current cell to be the cell with the lowest f score in the open list</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> nextCell:SimpleCell=<span style="color: #000000; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>openList.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>nextCell==<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> openList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">f</span> <span style="color: #66cc66;">&lt;</span> nextCell.<span style="color: #006600;">f</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextCell=openList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></div>
<p>When every adjacent cell has been checked, we go through the open list to pick the cell with the lowest f score (g and h combined) to act as our current cell next time this function is called. This is done with a standard for loop and there really isn&#8217;t much more to point out here.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if nextCell is null (the open list was empty), there is no path.</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// optional - choose the lowest h score from the closed list as a &quot;that'll do&quot; cell</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>nextCell==<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>closedList.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>closedList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">h</span><span style="color: #66cc66;">&lt;</span>current.<span style="color: #006600;">h</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current=closedList<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// else... if there are still cells to recurse through</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set the current cell the be the next cell (decided above)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; current=nextCell;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// call this function again (a function that calls itself = recursive)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; recurseToGoal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>If the open list was empty (our nextCell variable will still be null), it means there is no path between the 2 points. At this point we can find the cell in the closed list with the lowest h score to act as a replacement for the full path. This will give us the nearest cell &#8216;as the crow flies&#8217; to the desired target.</p>
<p>If the open cell wasn&#8217;t empty, we will have a non-null nextCell variable to work with. We change the current cell variable to hold the nextCell and call the recurseToGoal function again. This repeats everything you&#8217;ve just read using a different cell and, by repeating it enough times, the goal is eventually either found or deemed inaccessible.</p>
<p>And that&#8217;s the hard part done! Let&#8217;s just have a quick look at the getDistance function:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// this function returns 10 or 14 based on the 2 cells' positions</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// return 14 if diagonal, 10 otherwise</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDistance<span style="color: #66cc66;">&#40;</span>c1:SimpleCell, c2:SimpleCell<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>c1.<span style="color: #006600;">x</span><span style="color: #66cc66;">!</span>=c2.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&amp;&amp;</span> c1.<span style="color: #006600;">y</span><span style="color: #66cc66;">!</span>=c2.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">14</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">10</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #808080; font-style: italic;">// don't forget to close the class and package!</span></div></div>
<p>It&#8217;s taken for granted that this function is working with touching cells, so the logic is fairly straight-forward. If the x values are equal or the y values are equal, the 2 cells share a wall. If not, the cells are diagonally spaced. A value of 10 is used in A* to make the numbers workable; 14 is a close replacement for the actual distance between 2 diagonal cells (using an integer makes for faster processing).</p>
<p><em>&#8220;Yeah, yeah, yeah Ant&#8230; just tell me how to use the thing&#8230;&#8221;</em></p>
<p><strong id="pathfinding">Using the SimpleAStar class</strong>:</p>
<p>Make sure you have the SimpleCell, SimpleGrid and SimpleAStar class files in your working directory. The key to everything, the point of the whole tutorial, is the getPath function inside the SimpleAStar class. Everything below is a means to give that function space to operate.</p>
<p>Draw an object to use as a character, I&#8217;ll stick with the dot from the project preview example:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid10.png" alt="" title="Grid10" width="665" height="620" class="aligncenter size-full wp-image-892" /></p>
<p>The character can be entirely controlled on its timeline if you like but I&#8217;m going to stick to AS files for this one:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the dude is a Sprite and will update every frame</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dude <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// a reference to the grid and the pathfinding</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid:SimpleGrid;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pathfinding:SimpleAStar;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// a path to follow</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> path:<span style="color: #0066CC;">Array</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// a movement speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> speed:<span style="color: #0066CC;">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Dude<span style="color: #66cc66;">&#40;</span>g:SimpleGrid, pf:SimpleAStar, sx:<span style="color: #0066CC;">Number</span>, sy:<span style="color: #0066CC;">Number</span>, s:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set the variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid = g;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathfinding = pf;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = sx;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = sy;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speed = s;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// make the Dude move every frame</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, loop<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loop<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the path isn't empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>path.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// move towards to the first point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> dx = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>-x;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> dy = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>-y;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> distance = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>dx<span style="color: #66cc66;">*</span>dx<span style="color: #66cc66;">&#41;</span>+<span style="color: #66cc66;">&#40;</span>dy<span style="color: #66cc66;">*</span>dy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> angle = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span>dy,dx<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the distance is more than the Dude's speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>distance <span style="color: #66cc66;">&gt;</span> speed<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y += <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// if the distance is less than speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// move to the target point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// remove the top cell from the path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// if the path is empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// pick a random cell in the grid and attempt to go to it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = pathfinding.<span style="color: #006600;">getPath</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</span>, y<span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</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>grid.<span style="color: #0066CC;">width</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>grid.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Let&#8217;s take a quick look at the class in smaller pieces, starting with the constructor:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Dude<span style="color: #66cc66;">&#40;</span>g:SimpleGrid, pf:SimpleAStar, sx:<span style="color: #0066CC;">Number</span>, sy:<span style="color: #0066CC;">Number</span>, s:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set the variables</span><br />
&nbsp; &nbsp; grid = g;<br />
&nbsp; &nbsp; pathfinding = pf;<br />
&nbsp; &nbsp; x = sx;<br />
&nbsp; &nbsp; y = sy;<br />
&nbsp; &nbsp; speed = s;<br />
&nbsp; &nbsp; path = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// make the Dude move every frame</span><br />
&nbsp; &nbsp; addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, loop<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The Dude takes in a reference to the grid he/she (Dudette?) is part of, a reference to the pathfinding system to use, a starting position and a speed. A blank array for the path is created and an event listener is added that will update the Dude every frame.</p>
<p>The loop function is where the pathfinding and the grid are actually put to use:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loop<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the path isn't empty</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>path.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// move towards to the first point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> dx = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>-x;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> dy = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>-y;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> distance = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>dx<span style="color: #66cc66;">*</span>dx<span style="color: #66cc66;">&#41;</span>+<span style="color: #66cc66;">&#40;</span>dy<span style="color: #66cc66;">*</span>dy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> angle = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span>dy,dx<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the distance is more than the Dude's speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>distance <span style="color: #66cc66;">&gt;</span> speed<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y += <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// if the distance is less than speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// move to the target point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// remove the top cell from the path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// if the path is empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// pick a random cell in the grid and attempt to go to it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; path = pathfinding.<span style="color: #006600;">getPath</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</span>, y<span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</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>grid.<span style="color: #0066CC;">width</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>grid.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The logic, in a nutshell, is:</p>
<ul>
<li>If there is a path to follow</li>
<ul>
<li>Move towards the next node</li>
<li>If the node is reached in this step</li>
<ul>
<li>Delete that node from the path</li>
</ul>
</ul>
<li>If there wasn&#8217;t a path to follow</li>
<ul>
<li>Attempt to make a new (random) one</li>
</ul>
</ul>
<p>The path array starts empty, so the very first thing the loop function will do is attempt to generate a path. grid.cellSize is used to make sure all the measurements relate to the grid spacing. Bear in mind that the pathfinding itself just works with integer-based grid coordinates.</p>
<p>That&#8217;s about as simple as it gets! Now, if we throw a Dude into our grid from earlier:</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/Grid11.png" alt="" title="Grid11" width="486" height="292" class="aligncenter size-full wp-image-895" /></p>
<p>Here&#8217;s the code for you copy/paste types:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</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: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</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: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</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>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</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: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> grid:SimpleGrid = <span style="color: #000000; font-weight: bold;">new</span> SimpleGrid<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span>,<span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#41;</span>;<br />
grid.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> aStar:SimpleAStar = <span style="color: #000000; font-weight: bold;">new</span> SimpleAStar<span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> dude = <span style="color: #000000; font-weight: bold;">new</span> Dude<span style="color: #66cc66;">&#40;</span>grid, aStar,<span style="color: #cc66cc;">75</span>,<span style="color: #cc66cc;">75</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;<br />
addChild<span style="color: #66cc66;">&#40;</span>dude<span style="color: #66cc66;">&#41;</span>;</div></div>
<p>We get:</p>
<p align="center"><embed type="application/x-shockwave-flash" width="550" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/AStarSample.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="AStarSample" bgcolor="#FFFFFF" quality="high"></embed></p>
<p>That is as far as the <a href="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/PathfindingFiles.zip">sample files</a> go. If you are interested in making a character move towards a click, follow along below.</p>
<p>Start by removing the else part of the main loop in the Dude class. We don&#8217;t want him/her to choose a random path when the end of the current path is reached.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loop<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the path isn't empty</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>path.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// move towards to the first point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> dx = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>-x;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> dy = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>-y;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> distance = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>dx<span style="color: #66cc66;">*</span>dx<span style="color: #66cc66;">&#41;</span>+<span style="color: #66cc66;">&#40;</span>dy<span style="color: #66cc66;">*</span>dy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> angle = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span>dy,dx<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if the distance is more than the Dude's speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>distance <span style="color: #66cc66;">&gt;</span> speed<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y += <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// if the distance is less than speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// move to the target point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = path<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">*</span>grid.<span style="color: #006600;">cellSize</span>+grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// remove the top cell from the path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Now we&#8217;ll add an event to the stage (not always a good idea, but it will give you an idea of the thought process) that listens for a click. To guarantee that the stage is available, we will only add the listener after the Dude has been added to the stage, which means updating the constructor too:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Dude<span style="color: #66cc66;">&#40;</span>g:SimpleGrid, pf:SimpleAStar, sx:<span style="color: #0066CC;">Number</span>, sy:<span style="color: #0066CC;">Number</span>, s:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set the variables</span><br />
&nbsp; &nbsp; grid = g;<br />
&nbsp; &nbsp; pathfinding = pf;<br />
&nbsp; &nbsp; x = sx;<br />
&nbsp; &nbsp; y = sy;<br />
&nbsp; &nbsp; speed = s;<br />
&nbsp; &nbsp; path = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// add an added to stage event</span><br />
&nbsp; &nbsp; addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, added<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> added<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// make the Dude move every frame</span><br />
&nbsp; &nbsp; addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, loop<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// listen for a click</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clicked<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>For the click listener to work, we need to make sure that MouseEvent is imported at the top of the class, or that the whole events folder is imported if you&#8217;re feeling particularly lazy:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br />
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;</div></div>
<p>&#8230; or&#8230;</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;</div></div>
<p>For the purposes of this tutorial, I&#8217;m going to assume that your grid starts at 0,0 on the stage and there&#8217;s no funky scaling and/or other movement that would mean that the position of the mouse on the stage is different to its position in the grid. If your project is different, you&#8217;ll have to adjust the mouseX and mouseY values accordingly.</p>
<p>Using the getPath function in almost the same way as before, we simply swap the random location for the position of the mouse cursor:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> clicked<span style="color: #66cc66;">&#40;</span>m:MouseEvent<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// the screen has been clicked somewhere...</span><br />
&nbsp; &nbsp; path = pathfinding.<span style="color: #006600;">getPath</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</span>, y<span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</span>, <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">mouseX</span><span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</span>, <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">mouseY</span><span style="color: #66cc66;">/</span>grid.<span style="color: #006600;">cellSize</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>And there we have it!</p>
<p align="center"><embed type="application/x-shockwave-flash" width="550" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/03/AStarSample1.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="AStarSample2" bgcolor="#FFFFFF" quality="high"></embed></p>
<p>We finally got there! It&#8217;s time for my keyboard to take a well earned break. Feel free to discuss or ask any questions in the comments below and make sure you show me anything you make <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>For the pros out there, there are plenty of ways you can improve/build on this system; even typing it out again made me cringe at some of the code. Why not try:
<ul>
<li>Making it more universal, so it accepts any type of grid?</li>
<li>Making the whole system 1 standalone class (generate the cells when the getPath is called?)</li>
<li>Making it a static class so we don&#8217;t need to instance/reference it?</li>
<li>anything else that you can think of <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>
<p>Any feedback is welcome &#8211; if you&#8217;re a regular I&#8217;d be interested in how you rate it against my videos (more text in future?!)</p>
<p>Finally, if you scrolled down hoping to make the little town at the top, sorry! I can&#8217;t give everything away at once! Let&#8217;s just say you&#8217;ll need to skew your grid Sprite and add some other graphical jiggery-pokery. I&#8217;d advise you to resist the urge to decompile it too, as the code inside it is horrendously rushed and ugly.</p>
<p>See you next time.<br />
Ant</p>
<div id="tweetbutton789" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fpathfinding-the-projects%2F&amp;text=Pathfinding%20%26%238211%3B%20The%20Projects&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fpathfinding-the-projects%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/pathfinding-the-projects/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>March project preview</title>
		<link>http://eyes-squared.co.uk/blog/march-project-preview/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=march-project-preview</link>
		<comments>http://eyes-squared.co.uk/blog/march-project-preview/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 13:18:40 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[WIP]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Maths]]></category>
		<category><![CDATA[ShowYourClass]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=774</guid>
		<description><![CDATA[Hello! Here&#8217;s a quick look at what&#8217;s in store for you in the next project &#8211; A* pathfinding! It&#8217;s slowed down a touch so you can see it &#8216;thinking&#8217;. Click on any passable (white) grid space to move to that &#8230; <a href="http://eyes-squared.co.uk/blog/march-project-preview/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hello!</p>
<p>Here&#8217;s a quick look at what&#8217;s in store for you in the next project &#8211; A* pathfinding!</p>
<p align="center"><embed type="application/x-shockwave-flash" width="550" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/02/Example.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="PathfindingExample" bgcolor="#FFFFFF" quality="high"></embed></p>
<p>It&#8217;s slowed down a touch so you can see it &#8216;thinking&#8217;. Click on any passable (white) grid space to move to that space.</p>
<p>It&#8217;s a tricky subject &#8211; and one I hadn&#8217;t actually looked at in over 5 years &#8211; so all the theory is from an excellent and easily accessible source: <a href="http://www.policyalmanac.org/games/aStarTutorial.htm">http://www.policyalmanac.org/games/aStarTutorial.htm</a>. Go swot up <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>This is basic grid-based pathfinding; if it&#8217;s something that appeals to you, this is a decent start but it&#8217;s only a very small section of the bigger pathfinding picture. Even so, I&#8217;m still struggling to cram it all into the hour I allocate for videos at the moment; it should be with you within a week *fingers crossed*</p>
<p>Ant</p>
<p><strong>Update</strong>: Couldn&#8217;t help pushing it a bit further:</p>
<p align="center"><embed type="application/x-shockwave-flash" width="550" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/02/Example2.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="PathfindingExample" bgcolor="#FFFFFF" quality="high"></embed></p>
<div id="tweetbutton774" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fmarch-project-preview%2F&amp;text=March%20project%20preview&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fmarch-project-preview%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/march-project-preview/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Drawing, Animating and Scripting an Interactive Character &#8211; The Projects</title>
		<link>http://eyes-squared.co.uk/blog/drawing-animating-and-scripting-an-interactive-character-the-projects/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=drawing-animating-and-scripting-an-interactive-character-the-projects</link>
		<comments>http://eyes-squared.co.uk/blog/drawing-animating-and-scripting-an-interactive-character-the-projects/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 11:20:09 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=755</guid>
		<description><![CDATA[A nice and simple one this month. If you&#8217;re a Flash newbie, this is the one for you. If you&#8217;re already fairly proficient, skip to the last section for a tutorial on following the mouse cursor with the character&#8217;s eyes. &#8230; <a href="http://eyes-squared.co.uk/blog/drawing-animating-and-scripting-an-interactive-character-the-projects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A nice and simple one this month. If you&#8217;re a Flash newbie, this is the one for you. If you&#8217;re already fairly proficient, skip to the last section for a tutorial on following the mouse cursor with the character&#8217;s eyes.</p>
<p><iframe width="640" height="360" src="http://www.youtube.com/embed/gBOa06lcRWA" frameborder="0" allowfullscreen></iframe></p>
<p><strong>Source files:</strong></p>
<ul>
<li><a href="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/02/EndOfPart1.zip">File at the end of section 1</a></li>
<li><a href="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/02/EndOfPart2.zip">File at the end of section 2</a></li>
<li><a href="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/02/Final.zip">Final .fla</a></li>
</ul>
<p><strong>Sample file:</strong></p>
<p align="center"><embed type="application/x-shockwave-flash" width="550" height="400" src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/02/Character.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="false" align="middle" name="CharacterExample" bgcolor="#FFFFFF" quality="high"></embed></p>
<div id="tweetbutton755" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fdrawing-animating-and-scripting-an-interactive-character-the-projects%2F&amp;text=Drawing%2C%20Animating%20and%20Scripting%20an%20Interactive%20Character%20%26%238211%3B%20The%20Projects&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fdrawing-animating-and-scripting-an-interactive-character-the-projects%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/drawing-animating-and-scripting-an-interactive-character-the-projects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Game highlight &#8211; Rebuild</title>
		<link>http://eyes-squared.co.uk/blog/game-highlight-rebuild/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=game-highlight-rebuild</link>
		<comments>http://eyes-squared.co.uk/blog/game-highlight-rebuild/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 08:44:41 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=745</guid>
		<description><![CDATA[A great zombie-apocalypse indie game here. There is a sequel&#8230; but you just can&#8217;t beat originals! Retake your town, one square at a time. Click to play Rebuild &#160; Tweet]]></description>
			<content:encoded><![CDATA[<p><a href="http://eyes-squared.co.uk/blog/rebuild/"><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/01/thumb_100x100.jpg" alt="" title="Rebuild thumbnail" width="100" height="100" style="float:left; margin-right:20px; margin-bottom:20px;"/></a>A great zombie-apocalypse indie game here. There is a sequel&#8230; but you just can&#8217;t beat originals! Retake your town, one square at a time.</p>
<div align="center"><a href='http://eyes-squared.co.uk/blog/rebuild/'>Click to play Rebuild</a></div>
<p>&nbsp;</p>
<div id="tweetbutton745" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fgame-highlight-rebuild%2F&amp;text=Game%20highlight%20%26%238211%3B%20Rebuild&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Fgame-highlight-rebuild%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/game-highlight-rebuild/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Randomly Generated Hills &#8211; Show Your Class</title>
		<link>http://eyes-squared.co.uk/blog/randomly-generated-hills-show-your-class/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=randomly-generated-hills-show-your-class</link>
		<comments>http://eyes-squared.co.uk/blog/randomly-generated-hills-show-your-class/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 11:30:14 +0000</pubDate>
		<dc:creator>Ant</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Inspiration]]></category>
		<category><![CDATA[ShowYourClass]]></category>

		<guid isPermaLink="false">http://eyes-squared.co.uk/blog/?p=728</guid>
		<description><![CDATA[It&#8217;s mini-competition time! I&#8217;m hoping that there are now enough of you watching these videos to make some healthy competition possible. Using the Randomly Generated Hills tutorial and resulting classes, I want you to make something impressive to share with &#8230; <a href="http://eyes-squared.co.uk/blog/randomly-generated-hills-show-your-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s mini-competition time!</p>
<p><img src="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/01/SYC.png" alt="" title="SYC" width="332" height="411" class="aligncenter size-full wp-image-729" /></p>
<p>I&#8217;m hoping that there are now enough of you watching these videos to make some healthy competition possible. Using the <a href="http://eyes-squared.co.uk/blog/randomly-generated-hills-the-projects/">Randomly Generated Hills</a> tutorial and <a href="http://eyes-squared.co.uk/blog/wp-content/uploads/2012/01/Random-Peaks-and-Hills.zip">resulting classes</a>, I want you to make something impressive to share with other readers/viewers.</p>
<p><span id="more-728"></span></p>
<p><iframe width="640" height="360" src="http://www.youtube.com/embed/_Xe4qYOpBnc" frameborder="0" allowfullscreen></iframe></p>
<p>I&#8217;m aiming to make these little competitions every 2 months, so try to be finished by March. I&#8217;ll pick a few of my favourites (from different ability ranges) and post them on here. If you can&#8217;t make the March deadline, share it anyway!</p>
<p>Post links to your SWFs or videos on this thread and include a bit about:</p>
<ul>
<li>Your programming experience level</li>
<li>Your influences</li>
<li>Any other useful tutorial links that you used</li>
</ul>
<p>Impress me! <img src='http://eyes-squared.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="tweetbutton728" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Frandomly-generated-hills-show-your-class%2F&amp;text=Randomly%20Generated%20Hills%20%26%238211%3B%20Show%20Your%20Class&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Feyes-squared.co.uk%2Fblog%2Frandomly-generated-hills-show-your-class%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://eyes-squared.co.uk/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://eyes-squared.co.uk/blog/randomly-generated-hills-show-your-class/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

