<?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; Actionscript</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/actionscript/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>Flash Gaming Summit 2010</title>
		<link>http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 09:09:38 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2277</guid>
		<description><![CDATA[The Flash Gaming Summit, a (now) annual event sponsored by the usual suspects in the Flash gaming world, has come and gone. Scheduled strategically a day before the Game Developers Conference begins, the mini-con fills an auditorium with everyone who&#8217;s anyone in the Flash gaming scene, from solo hobbyist developers to extremely successful yet Satanic [...]]]></description>
			<content:encoded><![CDATA[<p>The Flash Gaming Summit, a (now) annual event sponsored by the usual suspects in the Flash gaming world, has come and gone. Scheduled strategically a day before the Game Developers Conference begins, the mini-con fills an auditorium with everyone who&#8217;s anyone in the Flash gaming scene, from solo hobbyist developers to extremely successful yet Satanic Facebook game developers. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/zynga.jpg" alt="zynga"></p>
<p>Zynga actually killed this dog, because no one would gift it to any of their Facebook friends.
</p></div>
<p>i spent the past month tweeting rotten poetry to the FGS Twitter account.  The entry fee for the conference wasn&#8217;t very expensive, and the OMDC (Ontario Mosquito Death Camp) is footing the bill for half of our activities in San Francisco this week through the Export Fund.  But if i see a chance to get something for free, whether it&#8217;s a conference pass or a Scientological stress test, i jump at the chance.  And lo and behold, at the eleventh hour, @FGS comped me a pass &#8230; but not before GamerSafe/Flash Game License comped me another one.  i felt like the <em>one</em> girl in grade eight who had breasts &#8230; so much unwarranted attention!  Thanks, everyone.  i was very happy to be there and to meet everyone in person.</p>
<p>If you couldn&#8217;t make it, you missed a pretty solid show.  i almost wish that GDC was one day only, and FGS dragged on for a month or more.  Here&#8217;s a run-down of who i saw and what i learned.</p>
<h2>Bidness</h2>
<p>The show was split into two tracks: business on the upper floor, and creative on the lower floor.  It was the conference equivalent of a mullet.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/mullet.jpg" alt="mullet"></p>
<p>Flash Gaming Summit: Business on the main floor, party in the basement.
</p></div>
<p>i stayed upstairs the entire time.  Adam did a great job with <b>Canabalt</b>, but i&#8217;d much rather learn how to make <em>myself</em> money than to hear about how he built a walk-in freezer made of money to store all his money, or whatever. </p>
<h2>Opening Keynote &#8211; Jameson Hsu</h2>
<p>Jameson&#8217;s a good guy, but a somewhat nervous presenter.  He announced a few new initiatives from his company, MochiMedia: the Mochi Social Platform, and the Mochi GAME Developer Fund.  i have NO IDEA why they capitalized &#8220;game&#8221; &#8230; maybe to differentiate it from their Mochi ACCOUNTING SOFTWARE Developer Fund?  Not sure.  The idea is that Mochi&#8217;s new supreme Chinese overlords, Shanda Games, who recently bought Mochi for $80 million, would put up $10 million to help developers make games.  i didn&#8217;t quite find out what the catch was &#8230; obviously, Mochi wants any games it funds to tie into as many of its own initiatives as possible &#8211; microtransactions, ads, and the new Mochi Social Platform.</p>
<p>The Platform didn&#8217;t really seem like a new or Earth-shattering announcement &#8211; you could see them building up to this at least two years out.  Mochi is creating an easy-to-implement solution where you can leverage social media &#8211; Facebook, MySpace, YourPants, etc &#8211; with a single, simple API.  It&#8217;s nice, but like many of the other services Mochi offers, you can roll your own and have a lot more control, but at a longer development time and greater financial risk.  With Mochi&#8217;s solution, you gain ease of use but forfeit control.</p>
<p>i was pretty happy to see the developer fund, because it was a little tiring to keep hearing Mochi and pals chanting the mantra &#8220;Make multiplayer games!  They rake in a lot more dough!&#8221;  They also <em>cost</em> a big wad more to develop.  Our first foray into a multiplayer game, <b><a href="http://www.interruptingcowtrivia.com">Interrupting Cow Trivia</a></b>, means that my daughters won&#8217;t be able to get braces and will look like snaggle-toothed freaks the rest of their lives.  The game was expensive to build, in other words.  Mochi&#8217;s fund, they say, aims to mitigate the risk of that more expensive development, and i think it&#8217;s a step in the right direction.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/badTeeth.jpg" alt="bad teeth"></p>
<p>Sorry, sweety &#8211; daddy had to build a multiplayer game.
</p></div>
<p>They&#8217;re still, however, giving away MochiCoins to players, which the players then use to buy virtual items in your microtransaction-enabled games, which is kind of like the government approving citizens to walk out of your shop with a free television or whatever, so Mochi remains on my handle-with-care list.</p>
<p>Mochi&#8217;s Ada Chen said she was afraid to open her mouth around me because she never knew whether i&#8217;d write something negative about the company.  Roll with the punches, Ada!  If you didn&#8217;t have critics, you&#8217;d go mad with power.  i&#8217;m just here to keep you honest.</p>
<h2>4 Keys to a Successful Social Game that All Game Developers Should Know</h2>
<p>On the panel:</p>
<ul>
<li>Dan Fiden &#8211; Playfish
<li>David Stewart &#8211; Playdom
<li>Gavin Barrett &#8211; Crowdstar
<li>Mark Skaggs &#8211; Zynga
</ul>
<p>This panel was stacked with Zynga, Playdom, Playfish and Crowdstar, the companies who &#8211; God love em &#8211; have actually turned a buck on Flash &#8230; and a BIG buck at that.  When the same companies spoke at Casual Connect in Seattle last year and dropped the bomb about how much they were raking in on Facebook, i could tell the whole conference was freaking out and trying to figure out how to get some of that action.  i also knew that by then, it was too late &#8211; these guys had sewn it up, a fact that they repeated often throughout this panel.</p>
<p>This panel, and nearly every other at the conference, was plagued with some uniformly terrible moderation.  Moderator Sana Choudary, and every panel moderator at the show, pulled the same rookie error of asking very broad questions, tiptoeing around controversy, and ending on the same ridiculously vague question &#8220;Where do you think the future of _____ is going?&#8221;  Oh <em>maaaaaaan</em> &#8230; if i have to hear that question one more time, i&#8217;m positively going to <em>bite my pillow</em>.</p>
<p>The moderator asked the audience to tweet questions.  The first one i came up with was &#8220;Are you making games or slot machines??&#8221;  Not very original, i know, but i have a hunch i wasn&#8217;t the only one thinking it.  i could only hear these companies talk about the fun, original and interesting <em>games</em> they were making for so long before i really had to give my head a shake.  At least one other person in the peanut gallery had tweeted the same question.  Unfortunately, the moderator censored the questions, and it was as if she&#8217;d been coached to do so &#8211; like when you interview a celebrity and you&#8217;re not allowed to ask about her recent divorce or that thing on her neck.  Zynga was here, and they were rich, and they were only going to deign to visit us from Mount Olympus if we were all on our best behaviour.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/quietPanel.jpg" alt="Quiet Panel"></p>
<p>Um &#8230; are any of you guys actually going to say anything?
</p></div>
<p>The end result was that the questions were vague, the answers were <em>more</em> vague, and at least two of the panelists were ignorant enough to clam up about numbers and say &#8220;it&#8217;s all on the Internet &#8211; go do some research and look it up.&#8221;  No, fellas &#8211; i didn&#8217;t fly from Toronto to California and haiku my way to a free pass to be told to go Google my questions about your companies.  You&#8217;re on the panel for a reason, and i&#8217;m in the audience for the same reason. Me: questions. You: answers. Telling people to &#8220;just Google it&#8221; is ignorant.</p>
<p>The conclusion the panel came to was that yes, you can make lots of money on Facebook &#8230; <em>if you&#8217;re Zynga or Playdom or Playfish or Crowdstar.</em>.  A few other points of interest:</p>
<ul>
<li>Games need to be designed with social hooks from the ground-up. Retrofitting doesn&#8217;t work.
<li>Mark &#8211; if your numbers are going down, it’s time for a sale!
<li>Mark &#8211; &#8220;Zynga collects 5 terabytes of data a day. (Ryan &#8211; holy SH*T!)  Don’t underestimate the data side of this business.&#8217;
<li>Gavin &#8211; &#8220;The support of your community and your interaction with them is the most important thing you can do.&#8221;
<li>Mark &#8211; (answering &#8220;what&#8217;s a minimum bar for success?&#8221;) &#8220;5 million daily active users.&#8221; (Ryan &#8211; DOUBLE holy sh*t!)
</ul>
<p>In conclusion: if you can collect and parse 5 terabytes of data a day, pull in and retain 5 million daily active users, and hook grandmas to your virtual slot machine like they&#8217;ve got a crack habit, you too could be the next Zynga.  It&#8217;s <em>that easy</em>.</p>
<h2>Money in Flash: Next Gen Monetization of Flash Games</h2>
<p>On the panel:</p>
<ul>
<li>Chris Hughes &#8211; flashgamelicense.com
<li>Jim Greer &#8211; Kongregate
<li>Justin Wong &#8211; Mochi Media
<li>Vikas Gupta &#8211; Social Gold
</ul>
<p>Initially, i was a bit annoyed during this panel.  The fellas weren&#8217;t talking about some space-aged monetization techniques <em>from the future</em> that i&#8217;d never heard of.  They were talking about microtransactions, mostly &#8211; <em>current gen</em> stuff, not next-gen stuff like the topic promised.  Still, it&#8217;s not fair for me to demand new monetization ideas when i (and many, many others i know) haven&#8217;t even made good on the current techniques.  </p>
<p>Here were a few nuggets from this panel:</p>
<ul>
<li>Jim says Kongregate&#8217;s best multiplayer game is raking in 5x more cash than their best single-player game
<li>Justin reiterated the thing that most successfully drives microtransactions: &#8220;Getting the player to the point of need.&#8221;  That&#8217;s where, for example, the next level is going to SLAUGHTER the player, and he knows it, but hey!  He can buy a bigger gun for two bucks.  It&#8217;s a lot like putting a condom machine in the bathroom of a cougar bar.  The point of need drives more sales.  (That&#8217;s my analogy, not Justin&#8217;s.)
<li>Jim says Kongregate is making about 1/3 of its revenue from virtual goods, and 2/3 from advertising.  He expects that to level out to about 50/50 in the next year.
<li>Jim added that Kongregate makes about half of its ad revenue in the 4th quarter, when its advertisers need to sell video games to people for Christmas
<li>Justin says Mochi makes about 15%-ish of its revenue from virtual goods sales
<li>Echoing the sentiments of the previous panel, the lads emphasized that games need to be built with microtransactions in mind from square one.  Retro-fitting an older game with new microtransactions is not as effective.
<li>Jim says analytics are important, and adds that you should &#8220;watch what [players] do, not what they say.&#8221;  i&#8217;ve heard this before &#8211; believe it or not, the two can be completely different.
<li>Jim says &#8220;The vast vast majority of revenue comes from credit cards and Pay Pal.&#8221; &#8220;$100 at a time is the most common.&#8221;  (Surprising! Higher than i expected.)
<li>Chris and Vikas agreed that once you get a player over the initial hurdle of paying the first time, it&#8217;s much easier to get that player to continue paying.  Chris added &#8220;If you can sell a dollar to a user, you can sell fifty dollars to a user.&#8221;
<li>Vikas offers that the best way to use subscriptions is in conjunction with virtual goods payments.  You offer virtual goods deals or bundles with subscriptions that end up saving the player money.  He says the two are a fantastic combination.
<li>Justin says games that used Facebook Connect saw a 30% jump in gameplays.
</ul>
<p>One of the most interesting insights i got out of this panel came about when one of the guys from Yummy Interactive took the Q&#038;A mic for the first of two somewhat obnoxious self-promoting speeches.  He pointed out that you could also sell games for a flat fee (which conveniently ties into Yummy&#8217;s model of selling a Flash wrapper for downloadables).  What ensued was a tense, almost exasperated exchange that gave me an Aha! moment.</p>
<p>Casual downloadable titles from sites like Big Fish Games started out at $20.  The price has dropped over the year, thanks to market interference by folks like Amazon, to the point where it&#8217;s now at about $7.  Flash games started at zero dollars, and have been struggling to increase.  So you have this race to the bottom in the casual downloadable space, and a race to the top in the Flash games space.  Is the point at which the two segments meet the perfect price to charge for an online video game?  Or will Flash games prove that microtransactions in multiplayer games suck way more money out of people&#8217;s pockets than a $20 price point ever did?</p>
<h2>Adobe Tools and Services for Flash Games</h2>
<p>It was more than a little embarrassing that Adobe, the people who <em>make</em> the software that all the conference attendees were meeting and speaking about, had such an inept presentation.  Technically, it was like giving a camcorder to a spider monkey and expecting it to take coherent pictures.  The presenters tried to show off Flash Player 10.1 running on a number of smartphones with a very dim presentation camera, and one monitor cable that they constantly had to pull out of the camera and plug into the laptop when switching between demos and slides.  The demos of games like Bloons on the various handheld devices ran disappointingly slowly.  One of the presenters swallowed her mic or something by the end of the talk, and had to lean into her fellow presenter&#8217;s chest and talk into his lavalier.  Just awful.</p>
<p>The whole time, i felt very bad for them, struggling like they did with bad tech, bad demos and no new information to share, like the release date for Flash CS5.  It was the <em>Flash</em> Gaming Summit, Adobe.  Throw us a frickin&#8217; bone.</p>
<p>One demo that did pique my curiosity showed a Flash game online where the player could log in using Facebook, MySpace or YourPants, and then another player could log into the same game on the iPhone, and the two could play the game on the same network.  This was a demonstration of a new initiative called Adobe Services.  i need to ask them about it at GDC, but i can&#8217;t imagine what they&#8217;d charge for that setup.  My guess is $lots.</p>
<h2>The Mochis Award Show, Sponsored by Kongregate</h2>
<p>This was a chance to see the usually serious-faced Jim Greer from Kongregate have fun and lampoon himself a bit, which was nice.  The award winners all look like they deserved their hardware, but i was dismayed to discover i hadn&#8217;t heard of most of the games in the running.  i knew <b>Canabalt</b> <b>Machinarium</b>, and had only heard of <b>Time Fcuk</b> because of a podcast i did with the developer, Edward McMullen.  It underscored the fact that this past year has been non-stop work for me, and that i need to lighten up and start playing more games.  i shouldn&#8217;t be such a Jim Greer all the time.</p>
<p>(i keed, Jim!  i keed!  Don&#8217;t hate.)</p>
<h2>Monetizing Your Game Outside of Sponsorship</h2>
<p>This panel consisted of:</p>
<ul>
<li>Colin Northway, Fantastic Contraption
<li>Daniel James, Three Rings
<li>Sian Yue Tan, Rocketbirds
<li>William Stallwood, Cipher Prime
</ul>
<p>This was a GREAT panel.  i absolutely loved that it inadvertently pitted three young bucks, whose recent successes had not yet been proven as deliberate business savvy or lucky strikes, against Daniel James, who&#8217;s had a chance to very publicly succeed and fail over the years he&#8217;s helmed Three Rings.</p>
<p>When asked how they chose a price point for their games, the answers were naive:</p>
<p>Colin &#8211; &#8220;Well, <b>World of Goo</b> cost $20, and i figured my game was half as good as <b>World of Goo</b>, so i charged $10.&#8221;</p>
<p>Sian Yue &#8211; &#8220;We looked at $10 and $20, and went with $15 cuz it was about halfway.&#8221;</p>
<p>Adorable.</p>
<p>i could see Daniel biting his tongue, but knowing him, it wasn&#8217;t going to last long.  Finally, he said in his refined British accent &#8220;So &#8230; you guys basically pulled your numbers out of your butts?&#8221;</p>
<p>Soon after, Sian Yue sagely added &#8220;We chose a price and we stuck with it.  Moving your price point around is never a good idea.&#8221;</p>
<p>i could see Daniel facepalm using <em>only his face</em>.  Having been to his monetization roundtables at GDC for the past few years, and knowing how much they monkey with their payment systems and pricing strategies, i knew he was about to assplode.  Finally, he couldn&#8217;t resist speaking up: &#8220;Actually, there&#8217;s a lot of scientific research that proves you <em>should</em> move your price point around.&#8221;</p>
<p>Oh <em>snappeth</em>!  Thou didst <em>not</em>!  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/daniel.jpg" alt="Daniel James and Friends"></p>
<p>Daniel James surreptitiously checks the walls for &#8220;Exit&#8221; signs.
</p></div>
<p>The old dog had schooled the young pup.  Here&#8217;s where Daniel&#8217;s wisdom and experience shone through.  He said it was alright to make that initial guess, but that you should &#8220;be diligent about testing your hypotheses.&#8221;  He went on to explain that his company had spent 5 million dollars on <b>Whirled</b>, only to see a $300k return.  He said they could have done the same learning in far less time with far less money by testing their hypothesis early and often.</p>
<p><center><br />
<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/IZLz04Hu0_U&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IZLz04Hu0_U&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object><br />
</center></p>
<p>i had read Daniel&#8217;s blog where he explained the lesson learned from <b>Whirled</b>.  He said that Three Rings was now testing Facebook games in small doses to see what would take off.  i had no idea what this meant.  Were they building portions of the games?  How could you know if a game would do well if you only built a fraction of it?</p>
<p>He clarified the strategy:  they were putting together highly-target Facebook banner ads for games that didn&#8217;t exist yet.  Each ad would lead to a landing page with a button where the potential player could sign up to be notified about the beta, which may not ever happen.  Daniel said that one of the theoretical games out-performed the others 5 to 1 in terms of interest, so that&#8217;s the one they&#8217;re going to build.</p>
<p>They even did granular testing on THAT game.  They did at least two ads with the same artwork and a different game name.  One name tested better than the other, so that&#8217;s the name they&#8217;re gonna use.</p>
<p><em>Brillant</em>, as any true Brit would say.</p>
<h2>Everything About Sponsorship &#038; Licensing</h2>
<p>What a panel!</p>
<ul>
<li>Greg McClanahan, Kongregate
<li>Joel Breton, AddictingGames.com
<li>Lars Jornow, King.com
<li>Robin Yang, Candystand
</ul>
<p>Of all the panels, this was probably both the worst and the most delightful.  Let me explain.</p>
<p>i&#8217;m told that AddictingGames.com were harangued by a developer last year for encouraging the Flash devs in the room to submit their games for free to that portal.  It was really nice to see how, after one year, the tide had turned, thanks in no small part to the effort of Chris Hughes and the Flash Game License team.  FGL clearly became the Great Equalizer in the war between the portals, who want as much content as possible for as little money as possible, and the developers, who deserve to be paid for their work.</p>
<p>You could tell that everyone on the panel, now matter how much they downplayed it, used FGL to find the developers they sponsored.  Robin, the most intolerable panelist, along with Joel, continually made the appeal for developers to forge a relationship with the portals, ostensibly so that devs would skip FGL and continue to deal directly with Candystand.  The advantage, of course, is that Candystand avoids a bidding war on FlashGameLicense.</p>
<p>Greg from Kongregate cottoned on to this real quick and said &#8220;what&#8217;s the advantage to the developer of going to you first?&#8221;  Robin and Joel each tried to spin a yarn about how they know their audience best and can give developers suggestions about how to build their games (uh &#8230; great?), and how they&#8217;ll put all kinds of money and promotion into a sequel if the first game does really well.  With a huge grin, Greg said &#8220;ANYONE  will give the dev all kinds of money and promotion for a sequel to a successful game!&#8221;  He put a fine point on it by saying that devs should go with the highest bidder, period.  Robin&#8217;s angle was so transparent that i could hardly believe her audacity.  The closest parallel that came to mind was when George Bush went on teevee right after the Shock and Awe bombing campaign in Iraq and said &#8220;Now, Iraqis, please stop setting your oil fields on fire.  Those are precious, and they&#8217;re the inheritance of the Iraqi people.&#8221;  </p>
<p>Really, Bush?  You don&#8217;t want the Iraqis to set their fields on fire because it&#8217;s for THEIR own good?  Aren&#8217;t you the least bit interested in maybe having a little bit of that oil for yourself?  Not even the <em>teensiest</em> amount?</p>
<p>It was justice, to see the reps from those two big portals twist and squirm before an audience that, in the course of a year, had begun to turn the tables on them.  The white masters were suddenly <em>so concerned</em> for the welfare of their newly-emancipated slaves.</p>
<p>Ok &#8211; i&#8217;m probably going overboard with these analogies.  The Iraq War?  Slavery?  i&#8217;m a step away from Hitler.</p>
<p>&#8230;.</p>
<p>Oh, what the Hell?  <em>Candystand and AddictingGames are Hitler</em>.  There.  i&#8217;m officially a hack.</p>
<h2>Turbulence Ahead: The Ups and Downs of Getting a Premium Flash Game to Success</h2>
<p>The last session of the day was a solo flight by Tim Flowers of Gabob LLC, who made six figures on <b>Now Boarding</b>.  i was initially worried that, like many developer talks, Tim&#8217;s would have a very narrow focus with very few take-homes and loads of inapplicable advice, like &#8220;then my auntie died and i got some money, so i was able to fund the next portion of my game.&#8221;</p>
<p>i found myself actually hanging on every word of what Tim said.  His whole plan for his game had been the exact plan i&#8217;d had for <b><a href="http://www.untoldentertainment.com/blog/kahoots-designer-diary">Kahoots</a></b> &#8211; push the game out to casual downloadable portals, who take an 80% cut.  His art style, liberally borrowed from <b>Catch Me If You Can</b>, reminded me of the pop-art style we adopted for <b><a href="http://www.interruptingcowtrivia.com">Interrupting Cow Trivia</a></b>.  i wrote down everything Tim said voraciously, and after asking him a few questions at the mic, returned to my seat and somehow lost my notebook in the process.  He said some absolutely smart things that are now lost to time &#8230; or to the two video cameras that taped the session.  Know this: daddy wants the video tape of Tim&#8217;s session.  i suggest that if it&#8217;s available, you watch it too!</p>
<h2>NICE TO MEEEET YOOOUUUU!!!!!  (WHAAAAT???)</h2>
<p>All in all, it was a fantastic day, and i didn&#8217;t want it to end &#8230; until the after-party started, and the organizers thought it would be a simply smashing idea to blast incredibly loud club music into everyone&#8217;s ears at a <em>networking event</em>.  i DO NOT understand why companies do that.  After i grew tired of yelling into people&#8217;s faces for hours, i left the party and started writing this postmortem back at my hotel.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/party.jpg" alt="party"></p>
<p>You can almost hear how loud it was from looking at this picture.
</p></div>
<p>GDC starts tomorrow, and i&#8217;ll do my level best to pussy out of Tuesday&#8217;s parties to write more for your enjoyment.</p>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2277&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/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>
	</channel>
</rss>
