<?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>untoldentertainment.com &#187; AS3</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.untoldentertainment.com/blog</link>
	<description>We Make Flash Games</description>
	<lastBuildDate>Wed, 08 Sep 2010 18:57:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction to A* (A-Star) Pathfinding in ActionScript 3 (AS3)</title>
		<link>http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 13:37:48 +0000</pubDate>
		<dc:creator>Phil Chertok</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2780</guid>
		<description><![CDATA[Introduction
For Spellirium we had to develop a system that could determine the most efficient path between  a series of nodes on a point.  After some diligent research and careful consideration it became clear that the A* search algorithm was the way to go.
Now, I was familiar with the basics behind A* but had [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>For <a href="http://www.untoldentertainment.com/blog/spellirium-designer-diary/">Spellirium</a> we had to develop a system that could determine the most efficient path between  a series of nodes on a point.  After some diligent research and careful consideration it became clear that the A* search algorithm was the way to go.</p>
<p>Now, I was familiar with the basics behind A* but had never actually implemented it.  Thankfully though I had a copy of Keith Peter’s <a href="http://www.amazon.com/AdvancED-ActionScript-Animation-Keith-Peters/dp/1430216085/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1280088548&amp;sr=8-1">Advanced ActionScript 3.0 Animation</a> which has a very nice chapter all about path finding and A*.</p>
<div class="displayed">
<p><a href="http://www.amazon.com/AdvancED-ActionScript-Animation-Keith-Peters/dp/1430216085/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1281753769&amp;sr=8-1"><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/book.jpg" alt="Advanced Actionscript 3.0 Animation" /></a></p>
<p>Why read this post?  Just buy the book instead. ;)</p>
</div>
<p>Before we begin, some prerequisite knowledge is required.  In order to follow along with this you should have a knowledge of AS3 Classes and Interfaces. It is certainly possible to implement A* without knowledge of these concepts but in order to build a flexible, reusable solution, we will be using both. Check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Understanding Classes in AS3</a> tutorials if you need help getting up to speed.</p>
<h2>What is A* path finding?</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/dofus.jpg" alt="Dofus Arena" /></p>
</div>
<p>According to the ever-so-useful <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">Wikipedia</a>,  A* path finding can be described as:</p>
<blockquote><p>(A) computer algorithm that (finds an) efficiently traversable path between points,  called nodes…. A* uses a best-first search and finds the least-cost path from a given initial node to one goal node (out of one or more possible goals).</p></blockquote>
<p>Sounds nice, but what does this mean? It means that A* chooses a node (or tile in a tile based world) searches through all the directly connected nodes and calculates a cost to travel to all of them.  It then selects the node that has the lowest cost and uses it as the starting node for the next phase of the search.  A* continues to do this until it reaches the destination.</p>
<p>To implement the algorithm it is first important to understand a few concepts:</p>
<ul>
<li><strong>Node:</strong> This is a point along a path.  In a tile-based environment you can think of it as a tile.  The reason why we don’t call it a tile is because it can be used for non-tile based worlds.   You will provide A* with a starting node and ending node.  A* will then calculate all the nodes to get from the start to the end.</li>
<li><strong>Parent Node:</strong> When a node is tested in the algorithm all of its directly connected nodes (nodes that are traversable in a single step) are assigned that node as their parent.  By doing so,  we can easily work backwards through all of the parent nodes to create our path after we reach the destination node.</li>
<li><strong>Cost:</strong> Cost is assigned to each node based on two factors.  Nodes with a lower cost are used over nodes with a higher cost.  The two parts that make up the cost are: the cost to get to that node from the starting node and the estimated cost to get from that node to the end node.  The cost of a node is usually expressed via 3 variables f, g and h.</li>
<li><strong>g:</strong> This is the cost to get to this node via the starting node.  We know this value exactly because we can follow the parent nodes all the way to the start and add up their costs.</li>
<li><strong>h:</strong> This is the cost to get from this node to the final node.  This value is estimated.  It is not exact because we don’t know the path that we will take to get to the final node.  Therefore we must estimate the cost and we do so using a function called a heuristic.</li>
<li><strong>f:</strong> This is the total cost of the specific node – it is calculated by adding g + h.</li>
<li><strong>Heuristic:</strong> This is the function that estimates the cost of getting from a particular node to the end node.  The beauty of A* is that it supports a variety of heuristics, therefore you can try out different ones until you get the result you are looking for.  Heuristic functions can differ based on their speed, efficiency and a variety of other criteria.</li>
<li><strong>Open list:</strong> This is a list of all the nodes that have been visited (in an iteration of the search) and have been assigned a cost.  The node that has the lowest cost will be used to perform the next iteration of the search.</li>
<li><strong>Closed list:</strong> This is the list of nodes whose neighbors have been visited.</li>
</ul>
<h2>The Algorithm</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/algorithm.jpg" alt="Algorithm" /></p>
</div>
<p>Now that is out of the way we can move on to the actual algorithm.  Before we actually begin coding, let&#8217;s just take a look at how A* actually works.</p>
<ol>
<li>Provide a starting node and an destination node</li>
<li>Add the starting node to our open list (which should have been empty)</li>
<li>Start our search loop:
<ul>
<li>a. Pick the node currently in the open list with the lowest cost – we will call this node the current node.  When we first run the loop this will obviously be our starting node.</li>
<li>b. If the current node is the same as the destination  then we are done  and we can move on to step 5.</li>
<li>c. Check every directly connected node. In a tile based world this would be up to 8 tiles.  For our implementation we will provide a function that determines all the connected nodes.  For each node that is connected:
<ul>
<li>i. If the node is not traversable (you can’t move to it because it Is occupied or for some other reason) or the node is already in the open list or in the closed list we can skip and move on to the next node in the list of connected nodes. Otherwise continue to ii.</li>
<li>ii. Calculate the cost of that node.</li>
<li>iii. Set the current node as the parentNode.</li>
<li>iv. Add the node to the open list.</li>
</ul>
</li>
<li>d. Add the current node to the closed list.</li>
</ul>
</li>
<li>Now return to step 3 (loop again) with the newly updated open list. (The loop should keep running until it hits the destination node)</li>
<li>We have found the destination node (hurrah!) Now we create a list of nodes that will be our path list and we will add the destination node to that list.</li>
<li>Add the parent node of the destination node to the path list.</li>
<li>Add the parent of the previous node to the path list, we will continue to do this until we have added our starting node.</li>
</ol>
<p>We should now have a list of all the nodes that make up the best path from our starting node to our destination node.</p>
<h2>The Implementation</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/sleeves.jpg" alt="Roll up your sleeves" /></p>
</div>
<p>Now that our theory is out of the way, let’s start writing some code.  First we will start with our Node class.  For this  I will actually create an interface for your node:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> INode
<span style="color: #66cc66;">&#123;</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;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> g<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> h<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> x<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> y<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">parentNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:INode;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> traversable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> f<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> g<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> h<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">parentNode</span><span style="color: #66cc66;">&#40;</span>value:INode<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> traversable<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This is a pretty straight-forward interface.  It includes the variables for all the properties we discussed above (f,g,h and parentNode ).  The other variables are fairly self-explanatory.  X and Y obviously indicate the position of the node. We will use these values in our heuristic to estimate the cost of traveling from the node to the destination.  The traversable variable is a simple Boolean that indicates if the node can be used in our path finding.  If it is not then the algorithm will simply skip it when it is encountered.  This is useful in a world where a node might be occupied by a prop (say a rock, tree or wall) or if there is another player/character occupying that space and you don’t want to support multiple characters on a single node.  Implementing this in a class should be very easy.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/pathfinder.jpg" alt="Nissan Pathfinder" /></p>
</div>
<p>Now we will build a class called Pathfinder.  We will create the class only using static methods.  Hopefully this will make it very flexible.  It will not take much to turn this into a standard class but for our purposes static methods will do very well.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pathfinder
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> heuristic:<span style="color: #000000; font-weight: bold;">Function</span> = Pathfinder.<span style="color: #006600;">diagonalHeuristic</span>;</pre></div></div>

<p>Here, we define the heuristic function we will use to estimate the cost of traveling from a node to the end.  In this example we will use the diagonal heuristic.  At the end of this article, we will discuss some other heuristics and how to implement them.</p>
<p>Let’s move on to our actual path finding function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findPath<span style="color: #66cc66;">&#40;</span>firstNode:INode, destinationNode:INode, connectedNodeFunction:<span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> 	<span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>Let’s examine the parameters:</p>
<ul>
<li>firstNode: INode – this is the first node in our path</li>
<li>destinationNode: INode – The is the node that we are going to be traveling to</li>
<li>connectedNodeFunction:Function – This is a function that will return an array of nodes that are connected to a given node.</li>
</ul>
<p>Since each implementation might be different we abstract it out this way.  After we write our class I will use a tile- based example to illustrate how this works, but your method might be different.</p>
<p>Lastly, the function returns an Array. This will contain all the nodes along the path.</p>
<h2>Variable Setup</h2>
<p>So now that is out of the way let’s do some initial work:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> openNodes:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">var</span> closedNodes:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">var</span> currentNode:INode = firstNode;
<span style="color: #000000; font-weight: bold;">var</span> testNode:INode;
<span style="color: #000000; font-weight: bold;">var</span> connectedNodes:<span style="color: #0066CC;">Array</span>;
<span style="color: #000000; font-weight: bold;">var</span> travelCost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span>;
<span style="color: #000000; font-weight: bold;">var</span> g:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> h:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> f:<span style="color: #0066CC;">Number</span>;
currentNode.<span style="color: #006600;">g</span> = 0;
currentNode.<span style="color: #006600;">h</span> = Pathfinder.<span style="color: #006600;">heuristic</span><span style="color: #66cc66;">&#40;</span>currentNode, destinationNode, travelCost<span style="color: #66cc66;">&#41;</span>;
currentNode.<span style="color: #006600;">f</span> = currentNode.<span style="color: #006600;">g</span> + currentNode.<span style="color: #006600;">h</span>;
<span style="color: #000000; font-weight: bold;">var</span> l:<span style="color: #0066CC;">int</span> = nodes.<span style="color: #0066CC;">length</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;</pre></div></div>

<p>The first two arrays will hold our open nodes and our closed nodes.  Then we have a variable <strong>currentNode</strong> which will keep track of the node we are currently on.  We set this to be our firstNode since that is where we will start.</p>
<p>Next we keep a variable <strong>testNode:INode</strong>, which will be the node that will be a connected node of our current node.</p>
<p>Next we have <strong>g</strong>,<strong>h</strong> and <strong>f</strong> values we will keep track of for each node.</p>
<p>We then set the initial <strong>g</strong> value of the currentNode (which is also our first node) to 0 since it does not cost anything to get there since it is already our current location.</p>
<p>Then, we calculate the estimated cost to reach the end using our heuristic, and finally the final cost which is calculated by adding <strong>g</strong> + <strong>h</strong> (the starting cost plus the final cost).</p>
<p>Lastly, we have some variables that we will use inside of loops in our A* algorithm.</p>
<h2>The Loop</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/loop.jpg" alt="Roller coaster loop" /></p>
</div>
<p>Let’s move on to the actual loop.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>currentNode <span style="color: #66cc66;">!</span>= destinationNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	connectedNodes = connectedNodeFunction<span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
	l = connectedNodes.<span style="color: #0066CC;">length</span>;
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = 0; i <span style="color: #66cc66;">&lt;</span> l; ++i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		testNode = connectedNodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode == currentNode || testNode.<span style="color: #006600;">travesable</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;
		g = currentNode.<span style="color: #006600;">g</span>  + travelCost;
		h = heuristic<span style="color: #66cc66;">&#40;</span> testNode, destinationNode, travelCost<span style="color: #66cc66;">&#41;</span>;
		f = g + h;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> Pathfinder.<span style="color: #006600;">isOpen</span><span style="color: #66cc66;">&#40;</span>testNode, openNodes<span style="color: #66cc66;">&#41;</span> || Pathfinder.<span style="color: #006600;">isClosed</span><span style="color: #66cc66;">&#40;</span> testNode, closedNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">f</span> <span style="color: #66cc66;">&gt;</span> f<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
&nbsp;
				testNode.<span style="color: #006600;">f</span> = f;
				testNode.<span style="color: #006600;">g</span> = g;
				testNode.<span style="color: #006600;">h</span> = h;
				testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			testNode.<span style="color: #006600;">f</span> = f;
			testNode.<span style="color: #006600;">g</span> = g;
			testNode.<span style="color: #006600;">h</span> = h;
			testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
			openNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>testNode<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	closedNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openNodes.<span style="color: #0066CC;">length</span> == 0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
	openNodes.<span style="color: #0066CC;">sortOn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'f'</span>, <span style="color: #0066CC;">Array</span>.<span style="color: #006600;">NUMERIC</span><span style="color: #66cc66;">&#41;</span>;
	currentNode = openNodes.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as INode;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Whoa! There’s a lot going on there, so let’s try to break it down piece by piece.</p>

<div class="wp_syntax"><div class="code"><pre class="actioncript" style="font-family:monospace;">while (currentNode != destinationNode) {</pre></div></div>

<p>Start our loop.  If our current node is the same as our destination then we are done and we can build our path!</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">connectedNodes = connectedNodeFunction<span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
l = connectedNodes.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = 0; i <span style="color: #66cc66;">&lt;</span> l; ++i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>So fetch an array of all of the connected nodes of our current node.  Store the length and then loop through all of them.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">testNode = connectedNodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode == currentNode || testNode.<span style="color: #006600;">travesable</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;</pre></div></div>

<p>Test each node out of the connected nodes.  If the node is the same as our current node or it is not traversable, move on to the next connected node.</p>
<p><strong>Note:</strong> Your connectedNodeFunction might omit non traversable nodes which would make the above line redundant and unnecessary.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">g = currentNode.<span style="color: #006600;">g</span>  + travelCost;
h = heuristic<span style="color: #66cc66;">&#40;</span> testNode, destinationNode, travelCost<span style="color: #66cc66;">&#41;</span>;
f = g + h;</pre></div></div>

<p>Ok, so now we are calculating the actual cost of our node.  First,  <strong>g</strong>:</p>
<h2>g</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/aliG.jpg" alt="Ali G" /></p>
</div>
<p>In a tile based world, we simply add the cost of the <strong>currentNode</strong> to the <strong>travelCost</strong> to determine the cost of the tile.  In some tile based environments, a diagonal move sometimes cost more than other moves.  In this case you would need to determine if the <strong>testNode</strong> is diagonal to the <strong>currentNode</strong> and adjust the <strong>travelCost</strong> value accordingly.</p>
<p>In our case here at Untold, we had a situation where the cost of each connected node was not the same because the nodes could be spread out across variable distances. So in our case we changed the <strong>g</strong> calculation to use our heuristic between the <strong>currentNode</strong> and the <strong>testNode</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">g  = currentNode.<span style="color: #006600;">g</span> + Pathfinder.<span style="color: #006600;">heuristic</span><span style="color: #66cc66;">&#40;</span> currentNode, testNode, travelCost<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, h:</p>
<h2>h</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/heroin.jpg" alt="Horse" /></p>
</div>
<p>This is where we use our heuristic.  Our heuristic takes in our <strong>testNode</strong> and our destination node. We also supply the cost to the heuristic which will return a number.  Again, we will go over the heuristic later.</p>
<h2>f</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/f.jpg" alt="f" /></p>
</div>
<p>Finally, we are at <strong>f</strong>.  This is a straightforward calculation to determine the total cost of the node.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> Pathfinder.<span style="color: #006600;">isOpen</span><span style="color: #66cc66;">&#40;</span>testNode, openNodes<span style="color: #66cc66;">&#41;</span> || Pathfinder.<span style="color: #006600;">isClosed</span><span style="color: #66cc66;">&#40;</span> testNode, closedNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>Here, we use some simple utility functions to determine if the node is in either our open list or our closed list.  If it is, then we should only adjust its <strong>f</strong>,<strong>g</strong> and <strong>h</strong> value if its current cost (<strong>f</strong>) is greater than the cost we just calculated.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">f</span> <span style="color: #66cc66;">&gt;</span> f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		testNode.<span style="color: #006600;">f</span> = f;
		testNode.<span style="color: #006600;">g</span> = g;
		testNode.<span style="color: #006600;">h</span> = h;
		testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
	<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		testNode.<span style="color: #006600;">f</span> = f;
		testNode.<span style="color: #006600;">g</span> = g;
		testNode.<span style="color: #006600;">h</span> = h;
		testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
		openNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>testNode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If the node is not in either the open list or in the closed list, then we definitely want to assign it the values we just calculated.  We also want to add it to the list of open nodes and make sure its parent is set to our <strong>currentNode</strong>.</p>
<p>We are now done our loop and can add our <strong>currentNode</strong> to our list of closed nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">closedNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openNodes.<span style="color: #0066CC;">length</span> == 0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
openNodes.<span style="color: #0066CC;">sortOn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'f'</span>, <span style="color: #0066CC;">Array</span>.<span style="color: #006600;">NUMERIC</span><span style="color: #66cc66;">&#41;</span>;
currentNode = openNodes.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as INode;</pre></div></div>

<p>If there are no more open nodes, then no path is available between the provided nodes, so we return NULL.</p>
<p>Otherwise, sort our open nodes based on their cost and use the one with the lowest cost as our new <strong>currentNode</strong>.</p>
<p>If a path is available, the <strong>currentNode</strong> will eventually be the same as our destination node, and the <em>while</em> loop will exit.  At this point, we will be ready to build our path and end our function which we do with a single line:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #b1b100;">return</span> Pathfinder.<span style="color: #006600;">buildPath</span><span style="color: #66cc66;">&#40;</span>destinationNode, firstNode<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Of course this means we need a function called <strong>buildPath()</strong>.  Luckily, it is quite simple and looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> buildPath<span style="color: #66cc66;">&#40;</span>destinationNode:INode, startNode:INode<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> path:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> node:INode = destinationNode;
	path.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>node <span style="color: #66cc66;">!</span>= startNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		node = node.<span style="color: #0066CC;">parentNode</span>;
		path.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span> node <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> path;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In this function, we simply keep moving through all the parent nodes of the provided nodes and put them at the front of a new array we return to the caller.</p>
<h2>The Heuristic</h2>
<p>We are almost ready to test out our class with an example but as promised we will first discuss our heuristic.</p>
<p>There is a wide range of heuristics used to calculate an optimum path.  The methodology is based on a variety of criteria.  Different heuristics return different results and you will need to find the one that is best for your case.  Right now, we’ll examine three simple heuristics, but don’t be afraid to go out and find your own.</p>
<p>Now, I was familiar with the basics behind A* but had never actually implemented it.  Thankfully though I had a copy of Keith Peter’s <a href="http://www.amazon.com/AdvancED-ActionScript-Animation-Keith-Peters/dp/1430216085/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1280088548&amp;sr=8-1">Advanced ActionScript 3.0 Animation</a> which has a very nice chapter all about path finding and A*.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/manhattan.jpg" alt="Manhattan A* Heuristic" /></p>
</div>
<p><strong>Manhattan:</strong> This heuristic is the most simple.  It ignores any diagonal movement, so if your world does not allow for diagonal movement it might be a good fit. It simply adds the total number of rows and columns between the two provided nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> manhattan<span style="color: #66cc66;">&#40;</span>node:INode, destinationNode:INode, cost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">x</span> – destinationNode.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> cost + <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">y</span> + destinationNode.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> cost;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/euclid.jpg" alt="Euclidian A* Heuristic" /></p>
</div>
<p><strong>Euclidian:</strong> This heuristic simply draws a straight line between the two provided nodes and returns its length.  You might recall the Pythagorean Theorem from math class?  Well, here it is in action.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> euclidianHeuristic<span style="color: #66cc66;">&#40;</span>node:INode, destinationNode:INode, cost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> dx:<span style="color: #0066CC;">Number</span> = node.<span style="color: #006600;">x</span> - destinationNode.<span style="color: #006600;">x</span>;
	<span style="color: #000000; font-weight: bold;">var</span> dy:<span style="color: #0066CC;">Number</span> = node.<span style="color: #006600;">y</span> - destinationNode.<span style="color: #006600;">y</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span> dx <span style="color: #66cc66;">*</span> dx + dy <span style="color: #66cc66;">*</span> dy <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> cost;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/diagonAlley.jpg" alt="Diagonal A* Heuristic" /></p>
</div>
<p><strong>Diagonal:</strong> This heuristic is the most accurate of the three.  It is more complex than the previous two, but runs fewer times because of its accuracy.  It takes into account a diagonal cost, if it is more expensive than a regular movement.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> diagonalHeuristic<span style="color: #66cc66;">&#40;</span>node:INode, destinationNode:INode, cost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span>, diagonalCost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> dx:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">x</span> - destinationNode.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> dy:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">y</span> - destinationNode.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> diag:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">min</span><span style="color: #66cc66;">&#40;</span> dx, dy <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> straight:<span style="color: #0066CC;">Number</span> = dx + dy;
	<span style="color: #b1b100;">return</span> diagonalCost <span style="color: #66cc66;">*</span> diag + cost <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>straight - <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">*</span> diag<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Make it Go</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/rocket.jpg" alt="Rocket" /></p>
</div>
<p>Now that we are done with our pathfinding system, we can put it to work.   Take a look at the example below.  You can click on any two tiles in the grid and then we will then draw a path between the two tiles.  You can change the heuristic at any point to see the different results you might get.  Also highlighted are any tiles that the pathfinder tests.  Notice that the Manhattan heuristic tests far more nodes than the other two.</p>
<p>
<object width="640" height="480">
<param name="movie" value="http://www.untoldentertainment.com/blog/img/2010_08_13/pathfinding_example.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<embed type="application/x-shockwave-flash" width="640" height="480" src="http://www.untoldentertainment.com/blog/img/2010_08_13/pathfinding_example.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
</p>
<p>I implement the pathfinding simply by calling the <strong>Pathfinder.findPath()</strong> function and passing the start node, end node and the function that determines connected nodes.  I then pass the returned array to a function called <strong>drawPath</strong>. For your usage you probably want to do a lot more than draw a path, you probably want to have a character traverse along that path, but this is just meant to show you how to acquire which path to follow.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">drawPath<span style="color: #66cc66;">&#40;</span> Pathfinder.<span style="color: #006600;">findPath</span><span style="color: #66cc66;">&#40;</span>_startNode, _endNode, findConnectedNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The findConnectedNodes method is very straightforward.  It simply returns all nodes that are adjacent to a given node.  You can check it out here:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findConnectedNodes<span style="color: #66cc66;">&#40;</span> node:INode <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> n:Node = node as Node;
	<span style="color: #000000; font-weight: bold;">var</span> connectedNodes:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> testNode:Node;
	<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> = 0; i <span style="color: #66cc66;">&lt;</span> _nodes.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		testNode = _nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">row</span> <span style="color: #66cc66;">&lt;</span> n.<span style="color: #006600;">row</span> - <span style="color: #cc66cc;">1</span> || testNode.<span style="color: #006600;">row</span> <span style="color: #66cc66;">&gt;</span> n.<span style="color: #006600;">row</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">col</span> <span style="color: #66cc66;">&lt;</span> n.<span style="color: #006600;">col</span> - <span style="color: #cc66cc;">1</span> || testNode.<span style="color: #006600;">col</span> <span style="color: #66cc66;">&gt;</span> n.<span style="color: #006600;">col</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;
		connectedNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> testNode <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> connectedNodes;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, this implementation will not be sufficient if you want different tiles to have different costs based on terrain (quicksand might be more expensive than grass), but if you can grasp the above you should be able to make the adjustments fairly easily.</p>
<p>Hopefully you will now have a solid understanding of A* path finding and you can now use it in your own applications.</p>
<p>Click <a href="http://www.untoldentertainment.com/blog/labs/pathfinding/pathfinding_example.zip">here</a> to download the source code for this tutorial.</p>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/"><img src="http://www.untoldentertainment.com/blog/img/features/flash911/flash911.gif" alt="Flash and Actionscript 911" /></a></p>
<p>This article is part of our ongoing <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> Series, which is awesome.</p>
</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2780&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cooking With Flash: Basic Flash Project Setup</title>
		<link>http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/#comments</comments>
		<pubDate>Thu, 06 May 2010 18:36:44 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Cooking With Flash]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2504</guid>
		<description><![CDATA[Prerequisites:

none.

Begin
So you want to start creating games with Adobe Flash?  Here&#8217;s the very first step.
You&#8217;ll need two pieces of software: Adobe Flash, and a program to in which to write your code.  (Code is a list of instructions you give to the computer to make it bend to your steely will.)  You [...]]]></description>
			<content:encoded><![CDATA[<p><b>Prerequisites:</b></p>
<ul>
<li>none.
</ul>
<h2>Begin</h2>
<p>So you want to start creating games with Adobe Flash?  Here&#8217;s the very first step.</p>
<p>You&#8217;ll need two pieces of software: Adobe Flash, and a program to in which to write your code.  (Code is a list of instructions you give to the computer to make it bend to your steely will.)  You can write your code inside the Flash program, but Flash is always playing catch-up with the great features available in other programs that make coding easier.  And i like easy.</p>
<h2>1. Download the Software</h2>
<p>You can download a number of programs to help you code.  The one we recommend (and use here at Untold Entertainment to create all our games) is called FlashDevelop.  It&#8217;s free, but if you like it, consider tossing a few clams to the creators.</p>
<ul>
<li><a href="http://www.adobe.com/products/flash/">Download a free 30-day trial of Adobe Flash</a>
<li><a href="http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page">Download your free copy of FlashDevelop</a> (PC-only)
</ul>
<p>note: other popular programs that professionals use to create their code include <a href="http://www.fdt.powerflasher.com/developer-tools/fdt-3/">FDT</a>, <a href="http://www.adobe.com/products/flashbuilder/">Flash Builder</a> (formerly known as Flex Builder), and even a fancy text editor like <a href="http://notepad-plus.sourceforge.net/uk/download.php">Notepad++</a>.  (i&#8217;m told that if you use a Mac, you&#8217;re iShitOutOfLuck on this front, but perhaps the commenters can recommend something?)</p>
<p>Once you&#8217;re finished downloading the two pieces of software you need, install them into the default directories, and we&#8217;ll get started setting up a project.</p>
<h2>What <em>is</em> Flash?</h2>
<p>The term &#8220;Flash&#8221; is confusing.  You may have heard that some online video &#8220;<em>is</em> Flash&#8221;, that some games you play in the browser &#8220;<em>are</em> Flash&#8221;, that some websites &#8220;<em>use</em> Flash&#8221;, and more recently that Flash <em>runs</em> or <em>doesn&#8217;t run</em> on this device or that device.  The word &#8220;Flash&#8221; is used interchangeably for both the authoring tool, the content that you create with it, and the plugin that displays that content to the end user.</p>
<p>The Adobe Flash software is also called an authoring <em>tool</em>, like a paintbrush or a table saw.  With it, you produce Flash content (games, video players, websites, etc), which a person usually views in a web browser using a plugin called the Flash <em>Player</em>.  Most often, when someone says you need to &#8220;download Flash&#8221;, he means you need to download the Flash Player <em>plugin</em> so that you can view Flash <em>content</em> that was created with the Flash <em>tool</em>.   What you&#8217;re downloading in the link above is the tool, which is sometimes called the Flash IDE (Integrated Development Environment).  i prefer &#8220;tool&#8221;, because it&#8217;s only one syllable, and &#8220;eye dee yee&#8221; is not kind to the ear.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/manyUsesOfFlash.jpg" alt="The Many Uses of the Term 'Flash'"></p>
<p>The term &#8220;Flash&#8221; has multiple uses.
</p></div>
<h2>2. Create and Save a New Actionscript 3 .fla File</h2>
<p>When you open the Flash authoring tool for the first time, you&#8217;ll likely see a splash screen.  To create a new file, etiher click &#8220;Create New Flash File (Actionscript 3.0)&#8221;, or go to File > New and choose Flash File (Actionscript 3.0) from the list, and click OK.</p>
<p>Your Flash file opens up and is displayed in all its glory &#8211; tons of interesting buttons and panels all over the screen.  Don&#8217;t be intimidated. We&#8217;ll explore them in future lessons.</p>
<p>Click File>Save As in the menus.  Flash files have the extension .fla.  Change the name of your Flash working file to myFirstGame.fla (or whatever you like), and save it to a folder somewhere on your computer.  Remember where you saved it!  i recommend against saving it to the desktop &#8211; if you want it there, first create a folder on the desktop called MyFirstGame, and save your .fla file in there.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashIDE.jpg" alt="Adobe Flash IDE"></p>
<p>Look at all this great STUFF to mess around with!  (note: Flash supports all kinds of different layouts &#8211; you may see something different on your screen depending on the version and layout you&#8217;re using)
</p></div>
<h2>3. Create a New FlashDevelop Project</h2>
<ol>
<li>Open FlashDevelop.
<li>In the menus, go to Project > New Project.
<li>Choose a name for your project in the Name field. (You can call it MyFirstGame.  Seems logical.)
<li>Use the Browse button to point to the MyFirstGame folder you created, where saved your .fla file in the last step.
<li>Click OK.
<li>If you get a message saying &#8220;The directory is not empty &#8230;&#8221;, click OK to confirm.
<li>If FlashDevelop asks you the Author name, type your own name or a suitable pseudonym, like &#8220;Cap&#8217;n CrackyPants&#8221;.  All of the code you write will be signed with this name.
<li>Click OK.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/saveFlashDevelopProject.jpg" alt="Creating a new project in FlashDevelop"></p>
<p>Creating a new project in FlashDevelop
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashDevelopAuthor.jpg" alt="FlashDevelop author prompt"></p>
<p>Señor Taco-Taker is like the Mexican Hamburglar
</p></div>
<p>When you&#8217;re finished, check out the Project Panel, at the right side of the screen. (If you can&#8217;t see it, click View > Project Manager in the menus.)  You&#8217;ll see your Project name, MyFirstGame.  Then you&#8217;ll see the .fla file you created in Flash, called myFirstGame.fla.  All of the code files you write to make your game will be listed here, along with any other files you place in the MyFirstGame folder.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashDevelopProjectPanel.jpg" alt="FlashDevelop project panel"></p>
<p>FlashDevelop Project panel
</p></div>
<p>Behind the scenes, FlashDevelop creates an .as3proj file in the folder, which is a metadata file that keeps track of what goes into the folder.  Once you create your Project file, you never have to save it like you would most other files &#8211; you can just open and close different Project files, and they&#8217;ll list the stuff that&#8217;s in the folder with them.</p>
<h2>4. Link FlashDevelop to Flash</h2>
<p>The last thing to do is to make FlashDevelop aware of the Flash authoring tool so that the two can play nicely together.  </p>
<ol>
<li>In the menus, click Tools>Program Settings or press F10.
<li>Click ASCompletion in the sidebar list.
<li>Click in the field labeled &#8220;Path to Flash IDE&#8221;, and navigate to wherever you installed the Flash tool on your computer.  On my Windows system, it&#8217;s here:
<p>C:\Program Files\Adobe\Adobe Flash CS4</p>
<li>Click the Close button.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashDevelopSettings.jpg" alt="FlashDevelop Settings"></p>
<p>FlashDevelop Settings
</p></div>
<h2>Finish</h2>
<p>That&#8217;s it &#8211; you&#8217;re done!  Now you&#8217;re ready to create Flash content using the one-two punch combo of Flash and FlashDevelop.</p>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2504&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>5 Ways to Cheat at Actionscript</title>
		<link>http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 13:13:50 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2248</guid>
		<description><![CDATA[As i see it, there are two ways to approach programming in Flash with Actionscript: the right way, and the &#8220;this thing is due at the end of the week&#8221; way.  Throughout my career, i haven&#8217;t enjoyed the luxury of big budgets and generous timelines. The broadcasters are cheap, the teevee producers work mostly [...]]]></description>
			<content:encoded><![CDATA[<p>As i see it, there are two ways to approach programming in Flash with Actionscript: the right way, and the &#8220;this thing is due at the end of the week&#8221; way.  Throughout my career, i haven&#8217;t enjoyed the luxury of big budgets and generous timelines. The broadcasters are cheap, the teevee producers work mostly on a borrowed dime, and the ad agencies (and everyone else) want to pocket as much as possible for themselves.  The result is that a successful independent Flash vendor needs to turn things around FAST.</p>
<p>So while i&#8217;m getting to know and understand the best practices of Object-Oriented programming, there are still some tried-and-true methods of finishing work more quickly that don&#8217;t obey strict OOP protocol.  What follows is a list of pragmatic programming tips &#8211; ways to bend the rules, or to leverage the visual tools in the Flash IDE &#8211; to speed things up, and to make life easier.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/theCheat.jpg"></p>
</div>
<h2>Cheat #1: The Injection Method</h2>
<p>The fact that the programming eggheads, the <em>nerderati</em>, have coined a term for this strategy means it can&#8217;t be all that bad.  The injection method breaks one of the pillars of OOP &#8211; encapsulation &#8211; but it&#8217;s a Hell of a time-saver.</p>
<p>Let&#8217;s say you&#8217;re building an adventure game.  You have an Inventory class and your MainGame class.  When the player combines the &#8220;dynamite&#8221; inventory with the &#8220;hammer&#8221; inventory item, that action might trigger a death animation.   The way you&#8217;ve built it, your MainGame class handles all death sequences. </p>
<p>According to the eggheads, the &#8220;right&#8221; way to do this is for MainGame to instantiate Inventory.  In order for Inventory to talk back to MainGame, it should dispatch a custom event, which MainGame listens for:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainGame <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">var</span> inventory:Inventory = <span style="color: #000000; font-weight: bold;">new</span> Inventory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          inventory.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Inventory.<span style="color: #006600;">DEADLY_ITEM_COMBINATION</span>, playDeathSequence<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> playDeathSequence<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
           <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You have died, because i'm a lousy game developer and i couldn't
                         think of a creative way to solve this situation without screeching
                          the game to a dead stop.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Here&#8217;s what the Inventory class looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Inventory
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const DEADLY_ITEM_COMBINATION:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;deadlyItemCombination&quot;</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Inventory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// Setup stuff goes here - etc etc.</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> combineItems<span style="color: #66cc66;">&#40;</span>item1:Item, item2:Item<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
           <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>item1.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;hammer&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> item2.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;dynaimte&quot;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
                 dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>DEADLY_ITEM_COMBINATION<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>i find it a very time-consuming process to set this up, especially if you need a lot of communication between the two classes.  The other asspain is that you can&#8217;t specify <em>which</em> deadly item combination triggered the death sequence. What if there is a special death sequence for any dynamite-related inventory screw-up, and a different sequence for any misstep involving the bagOSnakes item?  Sure, you can set up some kind of custom event class, but that takes even MORE time, and you&#8217;re on a deadline.</p>
<p>With the injection method, you pass a reference to the MainGame class into the Inventory class.  Then Inventory can invoke any public methods and properties on MainGame.  It breaks encapsulation, sure, but like i said, you&#8217;ve got until Friday to make this work, and you probably won&#8217;t end up reusing your Inventory class anyway.  (Certainly not NOW, you won&#8217;t ;)</p>
<p>Here&#8217;s how it looks:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainGame <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">var</span> inventory:Inventory = <span style="color: #000000; font-weight: bold;">new</span> Inventory<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// pass , or &quot;inject&quot;, a reference to the </span>
                                                                <span style="color: #808080; font-style: italic;">// MainGame class to inventory</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playDeathSequence<span style="color: #66cc66;">&#40;</span>typeOfDeath:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// inventory calls this public method on MainGame when the time is right </span>
           <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>typeOfDeath == <span style="color: #ff0000;">&quot;dynamiteRelated&quot;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #808080; font-style: italic;">// Play the dynamite death animation</span>
          <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>typeOfDeath == <span style="color: #ff0000;">&quot;bagOSnakesRelated&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #808080; font-style: italic;">// Play the bag o' snakes death animation</span>
          <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Inventory
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> main:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// A reference to the MainGame class</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Inventory<span style="color: #66cc66;">&#40;</span>main:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
         <span style="color: #0066CC;">this</span>.<span style="color: #006600;">main</span> = main; <span style="color: #808080; font-style: italic;">// Set the instance variable main to whatever's passed into the constructor</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> combineItems<span style="color: #66cc66;">&#40;</span>item1:Item, item2:Item<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
           <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>item1.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;hammer&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> item2.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;dynaimte&quot;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
                 main.<span style="color: #006600;">playDeathSequence</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;dynamiteRelated&quot;</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>item1.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;bagOSnakes&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> item2.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;tickleFeather&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                 main.<span style="color: #006600;">playDeathSequence</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bagOSnakesRelated&quot;</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, with anything in programming, there are many ways to skin a cat.  If you only ever have one Inventory in your game, you might even make it a static class.  The eggheads are going to crucify me for my abuse of the injection method, but that&#8217;s alright &#8211; i&#8217;ve got six dirt-cheap preschool games to build by Friday.  i welcome crucifixion with open arms, as it were.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/dynamite.jpg"></p>
<p>And BOOM! goes the trinitrotoluene
</p></div>
<h2>Cheat #2: Dispatching Events from the Timeline</h2>
<p>The architects of Actionscript 3 pulled kind of a dick move by rendering the timeline incredibly difficult to deal with.  One of the best things about Flash was the amount of complicated graphics and animation stuff it did for you right out of the box, but AS3 robbed us of productivity by making us write a lot of code when we shouldn&#8217;t have to.</p>
<p>This disadvantage rears its ugly head when you need to wait for a MovieClip to finish playing its animation.  One way to deal with this is to listen to the clip&#8217;s ENTER_FRAME or EXIT_FRAME events, and wait until it hits its last frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> groovyMovie:GroovyMovie = <span style="color: #000000; font-weight: bold;">new</span> GroovyMovie<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>groovyMovie<span style="color: #66cc66;">&#41;</span>;
groovyMovie.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">EXIT_FRAME</span>, checkCompletion<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> checkCompletion<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	 <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">currentFrame</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">totalFrames</span><span style="color: #66cc66;">&#41;</span>
	 <span style="color: #66cc66;">&#123;</span>
		 <span style="color: #808080; font-style: italic;">// MovieClip has reached the end of its timeline.</span>
	 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>That&#8217;s fine.  But what happens if the clip contains a number of separate animation sequences?  You can write a conditional statement inside the checkCompletion() method that looks at the currentLabel property, but that makes your code even thicker:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> groovyMovie:GroovyMovie = <span style="color: #000000; font-weight: bold;">new</span> GroovyMovie<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>groovyMovie<span style="color: #66cc66;">&#41;</span>;
groovyMovie.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">EXIT_FRAME</span>, checkCompletion<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> checkCompletion<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">var</span> clip:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	 <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>clip.<span style="color: #006600;">currentLabel</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;finishedJumping&quot;</span>:
                     doSomethingAfterFinishingJumping<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                     <span style="color: #b1b100;">break</span>;
                <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;finishedGyrating&quot;</span>:
                     doSomethingAfterFinishingGyrating<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                     <span style="color: #b1b100;">break</span>;
                <span style="color: #000000; font-weight: bold;">default</span>:
                     <span style="color: #b1b100;">break</span>;
         <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Putting code on the timeline is dirty and bad.  Occasionally, i&#8217;ll throw in some timeline events (gotoAndPlay(), stop(), etc) but that&#8217;s the extent of it.  Except in this situation.  In this sitaution, if it helps speed things up, i&#8217;ll advocate slipping in a dispatchEvent() action on the timeline.</p>
<p>On the salient frame of GroovyMovie:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;finishedGyrating&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And here&#8217;s our modified code from elsewhere:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> groovyMovie:GroovyMovie = <span style="color: #000000; font-weight: bold;">new</span> GroovyMovie<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>groovyMovie<span style="color: #66cc66;">&#41;</span>;
groovyMovie.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;finishedGyrating&quot;</span>, doSomethingAfterFinishingGyrating<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>No more messy switch statement and &#8211; maybe the eggheads can confirm or deny this &#8211; i think the code may speed up, because you&#8217;re no longer checking that conditional statement every frame.  (i&#8217;ve never cottoned on to the inner workings of the virtual machine, so if i&#8217;m wrong about that, feel free to nerdspank me.)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/spanking.jpg"></p>
<p>i&#8217;ve been a baaaaad programmer, and i deserve to be punished.
</p></div>
<h2>Cheat #3: Positioning Markers on the Stage</h2>
<p>One of the most difficult things about AS3 for me, a visual person, is that i suddenly have to lay visual elements like buttons and text fields out in code, instead of on the stage.  You get a lot more control over your elements when you manipulate them with code only, and some stuff just downright doesn&#8217;t work <em>unless</em> you work solely with code.</p>
<p>What i can&#8217;t stand is not knowing where things are going to end up on the stage.  i start by positioning something:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">widget.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">173</span>;
widget.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">221</span>;</pre></div></div>

<p>Then i compile.  When the code places my element on the stage, i soon see it&#8217;s off by a longshot. So i go back to the drawing board:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">widget.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">192</span>;
widget.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">237</span>;</pre></div></div>

<p>Compile. Check.  Damn &#8211; this time, i&#8217;ve overshot the mark.  i repeat this process &#8211; set x/y, compile, check, close swf &#8211; until it&#8217;s in the right place.  And then i do the same with some other element.  It&#8217;s aggravating.</p>
<p>i&#8217;ve found one of the fastest ways to cut through this is to create an empty MovieClip in your library and call it &#8220;Empty&#8221;.  Drag it out to the stage where you want the code-created element to exist.  Give it the instance name &#8220;marker&#8221;, or whatever.  Then in your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">widget.<span style="color: #006600;">x</span> = marker.<span style="color: #006600;">x</span>;
widget.<span style="color: #006600;">y</span> = marker.<span style="color: #006600;">y</span>;</pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/empty.jpg"></p>
<p>Nothing up my sleeve &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/marker.jpg"></p>
<p>Give the clip the instance name &#8220;marker&#8221; and position your elements to it.
</p></div>
<p>Later, if you find out you got it wrong, don&#8217;t painstakingly tweak the numbers in code.  Just grab the marker instance and drag it somewhere else on the stage.  Step 4: profit.</p>
<h2>Cheat #4: Stealing Color Effect Data</h2>
<p>i hope i&#8217;m not the only one, but classes like ColorTransform confuse the heck out of me.  ColorTransform takes <em>eight</em> arguments:</p>
<p>ColorTransform(redMultiplier:Number = 1.0, greenMultiplier:Number = 1.0, blueMultiplier:Number = 1.0, alphaMultiplier:Number = 1.0, redOffset:Number =0, greenOffset:Number =0, blueOffset:Number = 0, alphaOffset:Number = 0)</p>
<p>Whenever i need a specific outcome from the ColorTransform class, i put a MovieClip on the stage and adjust the Color Effect properties in the Properties panel.  If you choose &#8220;Advanced&#8221; and play with the dials, you may notice that there are eight different parameters you can tweak.  These eight params map onto the eight arguments that the ColorTransform class expects.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/colorTransform.jpg"></p>
<p>The numbers in the left column are the multipliers. The numbers in the right column are the offsets.
</p></div>
<p>Here&#8217;s the corresponding code, taken from the numbers on the dials. The offset parameters are percentages, so remember to divide the first four offset numbers by 100 (just throw a decimal place in front of them):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ct:ColorTransform = <span style="color: #000000; font-weight: bold;">new</span> ColorTransform<span style="color: #66cc66;">&#40;</span>.29, .38, .33, .77, <span style="color: #cc66cc;">82</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">72</span>, <span style="color: #cc66cc;">51</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It&#8217;s not exactly a &#8220;cheat&#8221;, but it does get around this notion that we need to fight tooth and nail to imagine everything through code.</p>
<h2>Cheat #5: Guided Layers</h2>
<p>This last one is also less of a cheat, and more of a way to get stuff out of codespace and into the more visual Flash environment.</p>
<p>If you&#8217;ve ever had to open another programmer&#8217;s code and Flash file, you may have been faced with a blank screen.  All of the elements are linked in the library, but you don&#8217;t know where they&#8217;re supposed to go or how they&#8217;re supposed to appear without compiling the movie.  And if you&#8217;ve been hired to fix an incomplete project and the fla won&#8217;t compile, you <em>could</em> be in for a world of hurt.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/blank.jpg"></p>
<p>oh, GREAT.
</p></div>
<p>This is usually the path you follow to figure out how the file&#8217;s been built:</p>
<ol>
<li>Check for timeline code in the first frame.
<li>Check for a document class.
<li>Check the library for linkage names to see what gets added to the stage dynamically
</ol>
<p>In order to be kind to Future-Me, or to anyone who inherits my code if i get hit by a bus, i use guided layers.  If you put something on a layer, and then right-click the layer and hit &#8220;Guide&#8221;, that thing won&#8217;t show up when you compile.  It&#8217;s MUCH friendlier to open up a file and see elements on the stage, so you get <em>some</em> idea of what&#8217;s supposed to appear in the file, instead of seeing a blank screen.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/guide.jpg"></p>
<p>Don&#8217;t be cruel. Put dynamically-linked stuff on a guided layer.
</p></div>
<blockquote><p>Important note: If you put code on your timeline (and in nearly all cases, you shouldn&#8217;t), your code will still fire even if you Guide out the layer that it&#8217;s on.</p></blockquote>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/layers.jpg"></p>
<p>The stuff on the graphics layer won&#8217;t show up, but the actions on the code layer will still fire, even though the layer is guided.
</p></div>
<h2>Speed Kills</h2>
<p>So those are a few ways that i &#8220;cheat&#8221; by leveraging the Flash IDE or using &#8220;good-enough&#8221; practices instead of &#8220;best&#8221; practices to speed through tight timelines.  Do you have any less-than-perfect strategies to get around writing mountains of code, or to make things easier on yourself?  i&#8217;d love hear them!</p>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2248&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Tutorial: Clickify Your Twitter Feed in Flash</title>
		<link>http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 13:44:03 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2230</guid>
		<description><![CDATA[


&#8220;Fwitter&#8221;.  Deal with it.

Our Pull Twitter Updates Into Flash tutorials (Part 1 and Part 2) continue to be popular! A few folks were asking how i had taken my Twitter feed and rigged it so that people&#8217;s Twitter names and links were clickable.  i promised to write tutorials on demand, so here we [...]]]></description>
			<content:encoded><![CDATA[<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_04_02/fwitter.jpg" alt="Fwitter">
</p>
<p>&#8220;Fwitter&#8221;.  Deal with it.
</p></div>
<p>Our <b>Pull Twitter Updates Into Flash</b> tutorials (<a href="http://www.untoldentertainment.com/blog/2009/04/02/tutorial-pull-twitter-updates-into-flash/">Part 1</a> and <a href="http://www.untoldentertainment.com/blog/2009/05/14/tutorial-twitter-updates-in-flash-part-2/">Part 2</a>) continue to be popular! A few folks were asking how i had taken my Twitter feed and rigged it so that people&#8217;s Twitter names and links were clickable.  i promised to write tutorials on demand, so here we go.</p>
<h2>Clickify Your Twitter Feed in Flash</h2>
<p>Here&#8217;s the process in pseudo-code:</p>
<ol>
<li>Take the chunk of text and split it up into individual words
<li>With each word, figure out if it starts with &#8220;@&#8221; or &#8220;http:&#8221;
<li>If it does, wrap it in html tags
<li>Re-assemble the string
<li>Set the re-constituted string as your dynamic text field&#8217;s htmlText property
</ol>
<p>So, let&#8217;s start with a simple chunk of Tweet text:</p>
<p>&#8220;Hey everyone &#8211; @untoldent&#8217;s blog is INCREDIBLE!  http://bit.ly/cXX06r&#8221;</p>
<blockquote><p>
Note: this is just a tweet i pulled at random from the Twittersphere.  It could have been anything.  But my blog is usually a trending topic at any given time of the day.</p></blockquote>
<p>We&#8217;ve got two things we want to make clickable: <a href="http://www.twitter.com/untoldent">@untoldent</a>, which should link to untoldent&#8217;s Twitter user page, and <a href="http://bit.ly/cXX06r">http://bit.ly/cXX06r</a>, which should take us to that webpage.</p>
<h2>Splitting the String</h2>
<p>The String class has a built-in method called split.  It takes an argument that lets us carve a string up, and the resulting fragments are put into an array.</p>
<p>So, for example, if we have a comma-delimited string:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> fraggles:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Gobo,Mokey,Wimbley,Boober,Red&quot;</span>;</pre></div></div>

<p>we can split it using the comma as a separator.  The resulting fragments are returned as an array.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> fraggles:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Gobo,Mokey,Wimbley,Boober,Red&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> aFraggles:<span style="color: #0066CC;">Array</span> = fraggles.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// use the comma to split up the string</span>
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>aFraggles.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// 5</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>aFraggles<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Wimbley</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>aFraggles<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Red</span></pre></div></div>

<p>So in the case of our Twitter string, we want to split it up into individual words. The thing that separates one word from another is the space character. So let&#8217;s use space as an argument to split up that string:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> aWords:<span style="color: #0066CC;">Array</span> = twitterString.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// use the space character to split up the string</span></pre></div></div>

<blockquote><p>Note: the split method is slow, so use it sparingly.  There&#8217;s not a lot going on in our Twitter reader, so we can get away with it.</p></blockquote>
<p>Now let&#8217;s loop through each element in the aWords array &#8211; each word in the Twitter post &#8211; and determine if it&#8217;s a Twitter username (which starts with &#8220;@&#8221;), or a URL (which starts with &#8220;http:&#8221;).  We&#8217;ll create an empty string to hold our final output, and run each word through a separate method.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> finalString:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>; <span style="color: #808080; font-style: italic;">// Create a new string to hold our final output</span>
<span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">int</span> = aWords.<span style="color: #0066CC;">length</span>; <span style="color: #808080; font-style: italic;">// store the length of the aWords array to speed up our loop</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Loop through all of the words in our tweet:</span>
<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> = 0; i<span style="color: #66cc66;">&lt;</span>len; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> word:<span style="color: #0066CC;">String</span> = aWords<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// Store an individual word in a variable</span>
    finalString += <span style="color: #ff0000;">&quot; &quot;</span> + checkWord<span style="color: #66cc66;">&#40;</span>word<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Run the word through a checkWord method, and tack the </span>
    <span style="color: #808080; font-style: italic;">// return value onto the end of our finalString (along with a space character, to space the words out)</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<blockquote><p>Note: We&#8217;re storing the length of aWords in a variable, instead of saying i<aWords.length.  This is because the Flash Player has to do more work to look up the length property of aWords every single time we loop.  You can speed up your loops a tiny bit by using this trick. </p></blockquote>
<p>Obviously, what&#8217;s missing is the all-important checkWord() method.  Let&#8217;s build that next.</p>
<h2>The checkWord() Method</h2>
<p>Our checkWord() method is going to do this, in pseudo-code:</p>
<ol>
<li>Accept a word
<li>If the word starts with &#8220;@&#8221; or &#8220;http:&#8221;, wrap it in href tags and return the string
<li>Otherwise, just return exactly what was passed in
</ol>
<p>To check if a string contains a certain other string, use the String.indexOf() method.  indexOf() will return the place in the string where the other string first appears.</p>
<p>For example, let&#8217;s find the indexOf the capital letter &#8220;W&#8221; in the string &#8220;NSFW&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;NSFW&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;W&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 3</span></pre></div></div>

<p>The answer is 3, because &#8220;W&#8221; is at index 3 in the string.  (Remember that strings and arrays are 0-based &#8211; the first element is at index 0, the second element is at index 1, and so on.)</p>
<p>If the thing you&#8217;re looking for can&#8217;t be found anywhere in the string, the indexOf() method will return an index of -1:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;NSFW&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;pictures of adorable kittens&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// -1</span></pre></div></div>

<h2>Substring</h2>
<p>The last thing you need to know how to do is to grab just a piece of a string.  This is because the twitter name comes through with an @ symbol at the front, for example @untoldent.  But we need to link to http://www.twitter.com/untoldent.  Notice there&#8217;s no &#8220;@&#8221; in that URL.</p>
<p>We need to extract just the username from the string, without the &#8220;@&#8221;.  We can use the String.substr (substring) method to do that.</p>
<p>The way String.substr() works is that you pass it a start point and an end point, and it rips that section out of the string and returns it.</p>
<p>So, check it out:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;manboobs&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boobs</span></pre></div></div>

<p>The letter at index 3 of &#8220;manboobs&#8221; is &#8220;b&#8221;.  (Remember &#8211; we start counting at zero.)  We rip the section out up to index 7, which is that last letter, &#8220;s&#8221;.  So what&#8217;s returned are the letters from index 3 to index 7 &#8211; &#8220;boobs&#8221;.</p>
<p>If you want to rip out a chunk of your string from a certain index point to the last letter in the string, you can use the String.length property.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;manboobs&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,str.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boobs</span></pre></div></div>

<p>If you&#8217;re on high alert and paying really close attention, you&#8217;ll notice that str.length takes us one index point past the end of our string.  (&#8220;manboobs&#8221; is 8 characters long, and there IS no letter at index 8.)  Flash just ignores us if we try to grab more of the string than actually exists.  So technically, this is also valid:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;manboobs&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">5000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boobs</span></pre></div></div>

<p>Armed with this knowledge, we can build our checkWord() method.</p>
<h2>Let&#8217;s Do This Thing</h2>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkWord<span style="color: #66cc66;">&#40;</span>word:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>word.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http:&quot;</span><span style="color: #66cc66;">&#41;</span> == 0<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
         <span style="color: #808080; font-style: italic;">// This word starts with &quot;http:&quot; at index 0 (the beginning of the word)</span>
&nbsp;
         <span style="color: #808080; font-style: italic;">// Wrap it in html href (anchor) tags:</span>
         <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;a href='&quot;</span> + word + <span style="color: #ff0000;">&quot;'&gt;&quot;</span> + word + <span style="color: #ff0000;">&quot;&lt;/a&gt;&quot;</span>;
   <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>word.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">&#41;</span> == 0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #808080; font-style: italic;">// This word starts with &quot;@&quot; at index 0 (the beginning of the word)</span>
&nbsp;
         <span style="color: #000000; font-weight: bold;">var</span> justTheName:<span style="color: #0066CC;">String</span> = word.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, word.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Remove the &quot;@&quot; from the username</span>
         <span style="color: #808080; font-style: italic;">// We do this because we need to link to http://www.twitter.com/untoldent (for example),</span>
         <span style="color: #808080; font-style: italic;">// not http://www.twitter.com/@untoldent</span>
&nbsp;
         <span style="color: #808080; font-style: italic;">// Wrap it in html href (anchor) tags:</span>
         <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;a href='http://www.twitter.com/&quot;</span> + justTheName + <span style="color: #ff0000;">&quot;'&gt;&quot;</span> + word + <span style="color: #ff0000;">&quot;&lt;/a&gt;&quot;</span>;
   <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;">// There's nothing special about this word.  Return it as-is.</span>
        <span style="color: #b1b100;">return</span> word;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<blockquote><p>Note: Did you notice the use of single quotation marks and double quotation marks?  You need to wrap you url in quotes, but since we&#8217;re already using double-quotation marks around our string, we need to use single-quotation marks to wrap the url, or they&#8217;ll interrupt our string definition and screw things up.</p></blockquote>
<h2>The Money Shot</h2>
<p>So far, we&#8217;ve turned this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Hey everyone - @untoldent's blog is INCREDIBLE!  http://bit.ly/cXX06r&quot;</span></pre></div></div>

<p>into this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Hey everyone - &lt;a href='http://www.twitter.com/untoldent'&gt;@untoldent's&lt;/a&gt; blog is INCREDIBLE!  &lt;a href=''&gt;http://bit.ly/cXX06r&lt;/a&gt;&quot;</span></pre></div></div>

<p>The very last step is to use the htmlText property of your dynamic text box and stuff it with that fancy new string.  Assuming you have a dynamic text box called twitterTxt, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;">twitterTxt.<span style="color: #0066CC;">htmlText</span> = finalString;</pre></div></div>

<p>You&#8217;ll now have clickable links inside your text field.</p>
<h2>Extra Credit</h2>
<p>If you&#8217;re super-astute, you&#8217;ve noticed that this code won&#8217;t actually work!  That&#8217;s because the Twitter string says &#8220;@untoldent&#8217;s&#8221;, not &#8220;@untoldent&#8221;.  The apostrophe-s is going to screw your code up, because you&#8217;ll be linking to http://www.twitter.com/untoldent&#8217;s, which is not a valid URL.</p>
<p>Using what you&#8217;ve learned in this tutorial, can you augment your checkWord() method to detect a &#8220;&#8217;s&#8221; at the end of a Twitter user name and rip it out?</p>
<h2>Further Reading</h2>
<p>i hope this tutorial was informative and helpful!  For more Actionscript and Flash tutorials, thrill to our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript  911</a> book.</p>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2230&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>AS3 Pitfalls &#8211; SOUND_COMPLETE Event is Not Firing</title>
		<link>http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 00:25:22 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Pitfalls]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1956</guid>
		<description><![CDATA[PROBLEM:
The SOUND_COMPLETE event is not triggered when your sound finishes playing.
SOLUTION:
There are a few different reasons why this problem may occur, but here&#8217;s how it played out in my project:
When you&#8217;re listening for a SOUND_COMPLETE event, you work with two Classes: Sound and SoundChannel.  The event listener goes on the SoundChannel instance:

var sound:Sound = [...]]]></description>
			<content:encoded><![CDATA[<p><b>PROBLEM:</b></p>
<p>The SOUND_COMPLETE event is not triggered when your sound finishes playing.</p>
<p><b>SOLUTION:</b></p>
<p>There are a few different reasons why this problem may occur, but here&#8217;s how it played out in my project:</p>
<p>When you&#8217;re listening for a SOUND_COMPLETE event, you work with two Classes: Sound and SoundChannel.  The event listener goes on the SoundChannel instance:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">sound</span>:<span style="color: #0066CC;">Sound</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;allYouNeedIsLove.mp3&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> soundChannel:SoundChannel = <span style="color: #0066CC;">sound</span>.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
soundChannel.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SOUND_COMPLETE</span>, soundIsFinished<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>In my project, i rigged up a &#8220;Pause&#8221; button.  When the player clicks &#8220;Pause&#8221;, i stop the sound and take note of the Sound&#8217;s position.  When the player clicks &#8220;Pause&#8221; again, i unpause the game and tell the Sound to start playing where we left off.</p>
<p>But whenever you paused and unpaused the game, the SOUND_COMPLETE Event wouldn&#8217;t fire any more.</p>
<p>That&#8217;s because whenever you tell a Sound object to play, <em>it returns a new SoundChannel instance</em>.  The old SoundChannel instance &#8211; the one with the event listener on it &#8211; is no longer relevant, so the event doesn&#8217;t fire.  So to get this to work, when the player unpauses the game, you have to add the SOUND_COMPLETE event listener to the <em>new</em> SoundChannel instance that&#8217;s returned when you call Sound.play();</p>
<p><b>THANKS FOR BAILING ME OUT!</b></p>
<p>BIG thanks to user WearDark on the Kirupa forums for clearing this up, and for saving me from hours and hours of bug testing trying to figure out what was going on!</p>
<p>http://www.kirupa.com/forum/archive/index.php/t-263824.html</p>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1956&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
