<?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>Mr Sun Studios &#187; AS3</title>
	<atom:link href="http://www.mrsunstudios.com/category/flash/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mrsunstudios.com/blog</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 28 Jul 2009 15:03:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 7</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-7/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-7/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:07:28 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1464</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 7: Finishing Touches Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 7: Finishing Touches</h3>
<p>Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I won&#8217;t comment any of the code I give you, as the knowledge you have learned from this tutorial should tell you what&#8217;s going on. Luckily for you, there aren&#8217;t too many finishing touches for us to make, so your brain won&#8217;t hurt too much.</p>
<p>One of the things I want to do is show the player the range of the turret. If you&#8217;ve ever played a tower defense game, you you should know what I&#8217;m talking about. A translucent circle will appear to show the player how far the turret can shoot. We&#8217;re going to show this both when somebody hovers over an empty block and when somebody hovers over a turret. Let&#8217;s start with hovering over an empty block.</p>
<p>Lets first start by opening up &#8220;source.fla&#8221;. Add this code to the top. Don&#8217;t ask any questions&#8230; or else&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> rangeCircle:Shape = <span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
rangeCircle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x006600,.5<span style="color: #66cc66;">&#41;</span>;
rangeCircle.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
rangeCircle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Open up &#8220;EmptyBlock.as&#8221;, would you? Find the <tt>thisMouseOver()</tt> function and add the following code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span>.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>;
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span>.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span>;
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Find the <tt>thisMouseOut()</tt> function and add this code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Add the same thing to the <tt>thisClick()</tt> function and the work will be done for empty blocks. Now, let&#8217;s do the same thing for Turrets.</p>
<p>Open up &#8220;Turret.as&#8221;. Add this code to the <tt>Turret()</tt> function (the main function):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, thisMouseOver<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, thisMouseOut<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Next, add these two functions to the end of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> thisMouseOver<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span>.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>-<span style="color: #cc66cc;">12.5</span>;
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span>.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span>-<span style="color: #cc66cc;">12.5</span>;
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> thisMouseOut<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">rangeCircle</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Good stuff. Now, it should work if you hover over the turrets.</p>
<p>Now, what else can we add to our little game? The answer is: it&#8217;s up to you to decide what to add. It&#8217;s also up to you to use what you&#8217;ve learned to do it right. Thank you and good night.</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt7/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt7/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt7/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-7/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-6/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-6/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:06:41 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1459</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 6: Expanding on the Game Welcome to the 6th part of the tutorial, Expanding on the Game! Well, what do I mean by &#8220;Expanding&#8221;? Well, by expanding, I mean that we&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 6: Expanding on the Game</h3>
<p>Welcome to the 6th part of the tutorial, Expanding on the Game! Well, what do I mean by &#8220;Expanding&#8221;? Well, by expanding, I mean that we&#8217;re going to create more enemies and more levels. Sounds pretty cool, doesn&#8217;t it?</p>
<p>The first thing we&#8217;re going to do before creating stronger enemies is to make the enemies give you money when you kill them. This will be easy. Just open up &#8220;Enemy.as&#8221; and find this code in <tt>eFrameEvents()</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//destroy this if health is equal to or below 0</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>health <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Just add the following code to the if statement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> += <span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>Now, we can continue on to making better enemies. Open up &#8220;source.fla&#8221; and find the <tt>makeEnemies</tt> function. We&#8217;re going to have some major renovations to this functions. Just replace the function with this new code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> makeEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//this function will add enemies to the field</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>enemyTime <span style="color: #66cc66;">&lt;</span> enemyLimit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it isn't time to make them yet</span>
		enemyTime ++;<span style="color: #808080; font-style: italic;">//then keep on waiting</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;">//otherwise</span>
		<span style="color: #000000; font-weight: bold;">var</span> theCode:<span style="color: #0066CC;">int</span> = enemyArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>currentEnemy<span style="color: #66cc66;">&#93;</span>;<span style="color: #808080; font-style: italic;">//get the code from the array</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>theCode <span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it isn't an empty space</span>
			<span style="color: #000000; font-weight: bold;">var</span> newEnemy:Enemy = <span style="color: #000000; font-weight: bold;">new</span> Enemy<span style="color: #66cc66;">&#40;</span>theCode<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then create a new enemy and pass in the code</span>
			enemyHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>newEnemy<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//and add it to the enemyholder</span>
		<span style="color: #66cc66;">&#125;</span>
		currentEnemy ++;<span style="color: #808080; font-style: italic;">//move on to the next enemy</span>
		enemyTime = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//and reset the time</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Okay, so the renovations weren&#8217;t too major. The only thing we changed was that instead of checking for the code of &#8220;1&#8243;, we check for any code that isn&#8217;t equal to &#8220;0&#8243;. Then, we pass that value into the Enemy class. </p>
<p>We&#8217;ll also have to make some changes in the <tt>startGame()</tt> functions. Don&#8217;t worry, they&#8217;ll be just as minor as the ones we just made. Replace the code inside of the function with this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> startGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//we'll run this function every time a new level begins</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>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>enemyArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>enemyArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			enemiesLeft ++;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The only thing we changed here was that now we check for all numbers not equal to 0, instead of just counting those set as 1.</p>
<p>Of course, now we&#8217;re going to have to make some changes to the Enemy class. Open up &#8220;Enemy.as&#8221; and find the topmost code where we define the variables and define the <tt>Enemy()</tt> function. Just replace that with this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> xSpeed:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//how fast it's going horizontally</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ySpeed:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//how fast it's going vertically</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> maxSpeed:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">3</span>;<span style="color: #808080; font-style: italic;">//how fast it can possibly go</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> health:<span style="color: #0066CC;">int</span>;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">level</span>:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//this will be set to the number passed in</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Enemy<span style="color: #66cc66;">&#40;</span>code:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0066CC;">level</span> = code;<span style="color: #808080; font-style: italic;">//set the level to the value passed in for use in other functions</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Not too many changes have been made. Now, we&#8217;re using that variable that was passed into the <tt>Enemy()</tt> function and making it usable. We&#8217;re also making the health undefined so we can change it based on the level. In fact, let&#8217;s change them now. Add this to the top of the <tt>beginCode()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">health = <span style="color: #0066CC;">level</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>This will set the health based on the level of the enemy. Next, let&#8217;s make him worth a bit more points, shall we? Find the code that we added in the beginning of the tutorial. Simply replace it with this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> += <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">level</span>;</pre></div></div>

<p>Now, we can make more levels with better enemies! You can customize your own levels, or use the ones I created by setting these values in the <tt>enemyArray</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">enemyArray = <span style="color: #66cc66;">&#91;</span><span style="color: #808080; font-style: italic;">//defining the array</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #808080; font-style: italic;">//1's will just represent an enemy to be created</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #808080; font-style: italic;">//another row means another level</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>,
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#93;</span>,
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span>,
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">250</span>,<span style="color: #cc66cc;">250</span>,<span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#93;</span>
			  <span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p>Of course, I&#8217;d suggest creating your own levels, as mine aren&#8217;t what you would call the best. Anyways, this wraps up the second to last part of this tutorial. Join us next time when finish up this little game!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt6/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt6/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 5</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-5/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-5/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:05:30 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1447</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 5: Winning/Losing the Game Welcome back to the 5th installment of this tutorial series. In this lesson, we&#8217;ll make some playable levels, along with a winning and losing situation. Let&#8217;s dig [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 5: Winning/Losing the Game</h3>
<p>Welcome back to the 5th installment of this tutorial series. In this lesson, we&#8217;ll make some playable levels, along with a winning and losing situation. Let&#8217;s dig in, shall we?</p>
<p>Lets start off by opening up the main source file. Find where this code is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">enemyArray = <span style="color: #66cc66;">&#91;</span><span style="color: #808080; font-style: italic;">//defining the array</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #808080; font-style: italic;">//1's will just represent an enemy to be created</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #808080; font-style: italic;">//another row means another level</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>
			  <span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p>Let&#8217;s review this code for a bit. Each of those arrays within that <tt>enemyArray</tt> represents a level. So, right now, we&#8217;re set up for 3 different levels, with an increasing amount of enemies in each level. Now that we&#8217;ve covered this, we can continue on to making it possible to advance levels.</p>
<p>In order to do this, we must open up &#8220;Enemy.as&#8221;. Find the <tt>destroyThis()</tt> function. Add this code to the bottom.</p>

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

<p>Next, we&#8217;ll have to go back to &#8220;source.fla&#8221;. Find the <tt>eFrame()</tt> function. There should only be one line of code in there right now. We&#8217;re going to add some. Add the following code to the bottom of the <tt>eFrame()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>enemiesLeft==<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if there are no more enemies left</span>
	currentLvl ++;<span style="color: #808080; font-style: italic;">//continue to the next level</span>
	currentEnemy = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//reset the amount of enemies there are</span>
	startGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//restart the game</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, try testing out the game. Functionally, it&#8217;s working 100%. Practically, it&#8217;s not a great game. In order to make this game better, we&#8217;re going to have to show some information to the user while they&#8217;re playing, like the current level, the score, etc. This is exactly what we&#8217;re going to do now.</p>
<p>Create four dynamic text boxes at the bottom left portion of the stage, each at 12 point font. Here&#8217;s an example of what yours should look like, with an example of what information we&#8217;re going to put into them later:</p>
<p><center><img src="http://www.mrsunstudios.com/wp-content/uploads/2009/02/textboxes.gif" alt="The Text Fields" title="The Text Fields" width="146" height="88" class="size-full wp-image-1450" /></center></p>
<p>Got that? Now, we just have to give each of these dynamic text fields an instance name. Label them accordingly:</p>
<ul>
<li><tt>txtLevel</tt></li>
<li><tt>txtMoney</tt></li>
<li><tt>txtLives</tt></li>
<li><tt>txtEnemiesLeft</tt></li>
</ul>
<p>Now, let&#8217;s dive into some code, shall we?</p>
<p>The first thing we need to do is actually define the <tt>money</tt> and the <tt>lives</tt> variables. Just add this code to the top:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> money:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">100</span>;<span style="color: #808080; font-style: italic;">//how much money the player has to spend on turrets</span>
<span style="color: #000000; font-weight: bold;">var</span> lives:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">20</span>;<span style="color: #808080; font-style: italic;">//how many lives the player has</span></pre></div></div>

<p>Okay, good stuff. Let&#8217;s first make these two variables mean something before we update the text. We first have to make it so the player loses lives so he/she can lose the game. Open up &#8220;Enemy.as&#8221; and find this code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//checking what direction it goes when finishing the path</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'UP'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it finishes at the top</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= -<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the y value is too high</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then remove this guy from the field</span>
	<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'RIGHT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//and so on for other directions</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">550</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;					
	<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'DOWN'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'LEFT'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>All we have to do is make the player lose a life after all of these conditionals. It should look like this now:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//checking what direction it goes when finishing the path</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'UP'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it finishes at the top</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= -<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the y value is too high</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then remove this guy from the field</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;<span style="color: #808080; font-style: italic;">//take away a life</span>
	<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'RIGHT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//and so on for other directions</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">550</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;					
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
	<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'DOWN'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
	<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'LEFT'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, we&#8217;ll make each of the turrets cost a certain amount of money. I&#8217;m thinking $20 is a good price. Open up &#8220;EmptyBlock.as&#8221; and find the <tt>thisClick</tt> function. Change it to this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> thisClick<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<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;">_root</span>.<span style="color: #006600;">money</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the player has enough money</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">makeTurret</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>,<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//make the turret</span>
		<span style="color: #808080; font-style: italic;">//remove all the listeners so it can't be clicked on again</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, thisMouseOver<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, thisMouseOut<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, thisClick<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> -= <span style="color: #cc66cc;">20</span>; <span style="color: #808080; font-style: italic;">//spend the money</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, we can update these text fields. Once again, find the <tt>eFrame()</tt> function in &#8220;source.fla&#8221;. Add the following code which will update all the text fields with the needed information:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Updating the text fields</span>
txtLevel.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'Level '</span>+currentLvl;
txtMoney.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'$'</span>+money;
txtLives.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'Lives: '</span>+lives;
txtEnemiesLeft.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'Enemies Left:  '</span>+enemiesLeft;</pre></div></div>

<p>Now, let&#8217;s create some winning and losing scenarios for the player. When the player has beaten all of the levels, then a win screen should show up. The same thing should happen with a lose screen when the player loses all of his/her lives. Let&#8217;s create these two frames, shall we?</p>
<p>Before, we create the frames, we have to add a layer called &#8220;labels&#8221;. This will just let us easily navigate to the required frames. Next, create a frame labeled &#8220;win&#8221; and add whatever message you want to inform the player that they&#8217;ve won. Do the same with a &#8220;lose&#8221; frame.</p>
<p>Now, we have to make it possible for the player to restart the game. We&#8217;ll let them do it by clicking anywhere on the screen. Copy and paste this code to the &#8220;win&#8221;  frame.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, restartGame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//adding a mouse event listener for clicking the stage</span>
<span style="color: #000000; font-weight: bold;">function</span> restartGame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//go to the first frame</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, restartGame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the listener</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We don&#8217;t need to define the function again in the &#8220;lose&#8221; frame, so you can just add this code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, restartGame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//adding a mouse event listener for clicking the stage</span></pre></div></div>

<p>All right, the next thing we have to do is navigate to the desired frame when the player wins or loses. Go back to &#8220;source.fla&#8221; and find the <tt>eFrame()</tt> function. Add the following code to the <strong>very beginning</strong> of it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//if there aren't any levels left</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentLvl <span style="color: #66cc66;">&gt;</span> enemyArray.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	gameOver=<span style="color: #000000; font-weight: bold;">true</span>;<span style="color: #808080; font-style: italic;">//set the game to be over</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//reset all the stats</span>
	currentLvl = <span style="color: #cc66cc;">1</span>;
	currentEnemy = <span style="color: #cc66cc;">0</span>;
	enemyTime = <span style="color: #cc66cc;">0</span>;
	enemyLimit = <span style="color: #cc66cc;">12</span>;
	enemiesLeft = <span style="color: #cc66cc;">0</span>;
&nbsp;
	removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove this listener</span>
	removeChild<span style="color: #66cc66;">&#40;</span>roadHolder<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the pieces of road</span>
	<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'win'</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//go to the win frame</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lives<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the user runs out of lives</span>
	gameOver=<span style="color: #000000; font-weight: bold;">true</span>;<span style="color: #808080; font-style: italic;">//set the game to be over</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//reset all the stats</span>
	currentLvl = <span style="color: #cc66cc;">1</span>;
	currentEnemy = <span style="color: #cc66cc;">0</span>;
	enemyTime = <span style="color: #cc66cc;">0</span>;
	enemyLimit = <span style="color: #cc66cc;">12</span>;
	enemiesLeft = <span style="color: #cc66cc;">0</span>;
&nbsp;
	removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove this listener</span>
	removeChild<span style="color: #66cc66;">&#40;</span>roadHolder<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the pieces of road</span>
	<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'lose'</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//go to the lose frame</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Sweet. This concludes this installment of the tutorial series. Join us next time when we expand on the game!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt5/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt5/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt5/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-5/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 4</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-4/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-4/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:04:23 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1439</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 4: Making Turrets Attack Enemies Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we? First, we&#8217;ll define a single health variable for [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 4:  Making Turrets Attack Enemies</h3>
<p>Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we?</p>
<p>First, we&#8217;ll define a single <tt>health</tt> variable for the Enemy. Just add this code to &#8220;Enemy.as&#8221; where we define all of the variables:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> health:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>Next, we&#8217;re going to have to create a new class. We&#8217;ve done this many times before, so hopefully I don&#8217;t need to remind you how to do it. Just create a new ActionScript file and save it as &#8220;Bullet.as&#8221;. Now, we&#8217;re just going to have to place a bit of code into it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Bullet</span> <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #66cc66;">*</span>;
		<span style="color: #808080; font-style: italic;">//these two variables below must be set to public so that we can edit them outside the class</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">target</span>;<span style="color: #808080; font-style: italic;">//the target that this guy is moving towards</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> damage:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//how much damage this guy inflicts on the enemy</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xSpeed:<span style="color: #0066CC;">Number</span>;<span style="color: #808080; font-style: italic;">//how fast it's moving horizontally</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ySpeed:<span style="color: #0066CC;">Number</span>;<span style="color: #808080; font-style: italic;">//how fast it's moving vertically</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> maxSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">5</span>;<span style="color: #808080; font-style: italic;">//how fast it can go</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Bullet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>,beginClass<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this will run every time this guy is made</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>,eFrame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this will run every frame</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//setting the root</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//drawing this guy (it'll be a small white circle)</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<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> yDist:<span style="color: #0066CC;">Number</span>=<span style="color: #0066CC;">target</span>.<span style="color: #006600;">y</span>+<span style="color: #cc66cc;">12.5</span> - <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span>;<span style="color: #808080; font-style: italic;">//how far this guy is from the enemy (x)</span>
			<span style="color: #000000; font-weight: bold;">var</span> xDist:<span style="color: #0066CC;">Number</span>=<span style="color: #0066CC;">target</span>.<span style="color: #006600;">x</span>+<span style="color: #cc66cc;">12.5</span> - <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>;<span style="color: #808080; font-style: italic;">//how far it is from the enemy (y)</span>
			<span style="color: #000000; font-weight: bold;">var</span> angle:<span style="color: #0066CC;">Number</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span>yDist,xDist<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//the angle that it must move</span>
			ySpeed=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> maxSpeed;<span style="color: #808080; font-style: italic;">//calculate how much it should move the enemy vertically</span>
			xSpeed=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>angle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> maxSpeed;<span style="color: #808080; font-style: italic;">//calculate how much it should move the enemy horizontally</span>
			<span style="color: #808080; font-style: italic;">//move the bullet towards the enemy</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>+= xSpeed;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span>+= ySpeed;
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it touches the enemy</span>
				<span style="color: #0066CC;">target</span>.<span style="color: #006600;">health</span> -= damage;<span style="color: #808080; font-style: italic;">//make it lose some health</span>
				destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//and destroy this guy</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span> == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span> == <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//destroy it if game is over</span>
				destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//this function will just remove this guy from the stage</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</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>The next thing we need to do is define some variables for the Turret. Open up &#8220;Turret.as&#8221; and add in the following code where we define the variables:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> angle:<span style="color: #0066CC;">Number</span>; <span style="color: #808080; font-style: italic;">//the angle that the turret is currently rotated at</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> radiansToDegrees:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">180</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span>;<span style="color: #808080; font-style: italic;">//this is needed for the rotation</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> damage:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">3</span>;<span style="color: #808080; font-style: italic;">//how much damage this little baby can inflict</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> range:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">100</span>;<span style="color: #808080; font-style: italic;">//how far away (in pixels) it can hit a target</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> enTarget;<span style="color: #808080; font-style: italic;">//the current target that it's rotating towards</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cTime:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//how much time since a shot was fired by this turret</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> reloadTime:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">12</span>;<span style="color: #808080; font-style: italic;">//how long it takes to fire another shot</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">loaded</span>:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;<span style="color: #808080; font-style: italic;">//whether or not this turret can shoot</span></pre></div></div>

<p>Those are a lot of variables, aren&#8217;t they? Well, this is a pretty complex task, so we&#8217;re going to need all of them. So, brace yourself, for we are now going to jump into some code. Add this code to the <tt>eFrameEvents</tt> function in &#8220;Turret.as&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//FINDING THE NEAREST ENEMY WITHIN RANGE</span>
<span style="color: #000000; font-weight: bold;">var</span> distance:<span style="color: #0066CC;">Number</span> = range;<span style="color: #808080; font-style: italic;">//let's define a variable which will be how far the nearest enemy is</span>
enTarget = <span style="color: #000000; font-weight: bold;">null</span>;<span style="color: #808080; font-style: italic;">//right now, we don't have a target to shoot at</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>=<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span>.<span style="color: #006600;">numChildren</span>-<span style="color: #cc66cc;">1</span>;i<span style="color: #66cc66;">&gt;</span>=<span style="color: #cc66cc;">0</span>;i--<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//loop through the children in enemyHolder</span>
	<span style="color: #000000; font-weight: bold;">var</span> cEnemy = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span>.<span style="color: #006600;">getChildAt</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//define a movieclip that will hold the current child</span>
	<span style="color: #808080; font-style: italic;">//this simple formula with get us the distance of the current enemy</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span>cEnemy.<span style="color: #006600;">y</span> - y, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span>cEnemy.<span style="color: #006600;">x</span> - x, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span> distance<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the selected enemy is close enough, then set it as the target</span>
		enTarget = cEnemy;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//ROTATING TOWARDS TARGET</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>enTarget <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if we have a defined target</span>
	<span style="color: #808080; font-style: italic;">//turn this baby towards it</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">rotation</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>enTarget.<span style="color: #006600;">y</span>-y<span style="color: #66cc66;">&#41;</span>, enTarget.<span style="color: #006600;">x</span>-x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">180</span> - <span style="color: #cc66cc;">90</span>;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">loaded</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the turret is able to shoot</span>
		<span style="color: #0066CC;">loaded</span> = <span style="color: #000000; font-weight: bold;">false</span>;<span style="color: #808080; font-style: italic;">//then make in unable to do it for a bit</span>
		<span style="color: #000000; font-weight: bold;">var</span> newBullet:<span style="color: #0066CC;">Bullet</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Bullet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create a bullet</span>
		<span style="color: #808080; font-style: italic;">//set the bullet's coordinates</span>
		newBullet.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>;
		newBullet.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span>;
		<span style="color: #808080; font-style: italic;">//set the bullet's target and damage</span>
		newBullet.<span style="color: #0066CC;">target</span> = enTarget;
		newBullet.<span style="color: #006600;">damage</span> = damage;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>newBullet<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add it to the stage</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//LOADING THE TURRET</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #0066CC;">loaded</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it isn't loaded</span>
	cTime ++;<span style="color: #808080; font-style: italic;">//then continue the time</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>cTime == reloadTime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if time has elapsed for long enough</span>
		<span style="color: #0066CC;">loaded</span> = <span style="color: #000000; font-weight: bold;">true</span>;<span style="color: #808080; font-style: italic;">//load the turret</span>
		cTime = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//and reset the time</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, if you test the movie out, the turrets should be shooting at those darn red dots. Now, we have to make those dots die! In the <tt>eFrameEvents()</tt> function in &#8220;Enemy.as&#8221;, add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//remove this from stage when game is over</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Pretty hot stuff, ain&#8217;t it? Well, this concludes the fourth installment of this tutorial. Join us next time when we make levels and have winning and losing scenarios!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt4/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt4/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt4/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-3/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:03:57 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1394</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 3: Adding Enemies Welcome back. In this part of the tutorial, we are going to add enemies to the field and we&#8217;re going to program them to move through the paths. [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 3: Adding Enemies</h3>
<p>Welcome back. In this part of the tutorial, we are going to add enemies to the field and we&#8217;re going to program them to move through the paths.</p>
<p>Let us begin by first creating an <tt>Enemy</tt> class. Do this by creating a new external ActionScript file saved as &#8220;Enemy.as&#8221; and adding the following code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//imports</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #808080; font-style: italic;">//defining the class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Enemy <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> xSpeed:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//how fast it's going horizontally</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ySpeed:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//how fast it's going vertically</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> maxSpeed:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">3</span>;<span style="color: #808080; font-style: italic;">//how fast it can possibly go</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Enemy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//defining the root</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//checking what the start direction is</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'UP'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it's starting up</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">300</span>;<span style="color: #808080; font-style: italic;">//set the y value off the field</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;<span style="color: #808080; font-style: italic;">//make the x value where it should be</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//make it not move horizontally</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span> = -maxSpeed;<span style="color: #808080; font-style: italic;">//make it move upwards</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'RIGHT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//and so on for other directions</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> = -<span style="color: #cc66cc;">25</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span> = maxSpeed;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'DOWN'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> = -<span style="color: #cc66cc;">25</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span> = maxSpeed;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'LEFT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">550</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span> = -maxSpeed;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//draw the actual enemy, it's just a red ball</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF0000<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eFrameEvents<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<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;">//move it based on x and y value</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> += xSpeed;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> += ySpeed;
&nbsp;
			<span style="color: #808080; font-style: italic;">//checking what direction it goes when finishing the path</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'UP'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it finishes at the top</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= -<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the y value is too high</span>
					destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then remove this guy from the field</span>
				<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'RIGHT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//and so on for other directions</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">550</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> == <span style="color: #ff0000;">'DOWN'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</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><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> == <span style="color: #ff0000;">'LEFT'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//remove this from stage when game is over</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//this function will make it easier to remove this from stage</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</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>Now that&#8217;s a lot of code. The next thing we have to do is to add a function in the main &#8220;source.fla&#8221; that will add these guys to the field. But, let&#8217;s first create a bunch of variables that&#8217;ll help with the process. It&#8217;ll be simple. Just define them variable where all others are defined:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> currentEnemy:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//the current enemy that we're creating from the array</span>
<span style="color: #000000; font-weight: bold;">var</span> enemyTime:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//how many frames have elapsed since the last enemy was created</span>
<span style="color: #000000; font-weight: bold;">var</span> enemyLimit:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">12</span>;<span style="color: #808080; font-style: italic;">//how many frames are allowed before another enemy is created</span>
<span style="color: #000000; font-weight: bold;">var</span> enemyArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this array will tell the function when to create an enemy</span>
<span style="color: #000000; font-weight: bold;">var</span> enemiesLeft:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//how many enemies are left on the field</span>
enemyArray = <span style="color: #66cc66;">&#91;</span><span style="color: #808080; font-style: italic;">//defining the array</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #808080; font-style: italic;">//1's will just represent an enemy to be created</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #808080; font-style: italic;">//another row means another level</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>
			  <span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p>Sweet. Now we can add an <tt>enterFrame</tt> function that&#8217;ll create the enemies for us. Add this to the bottom of your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//adding an eFrame function</span>
<span style="color: #000000; font-weight: bold;">function</span> eFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	makeEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//we'll just make some enemies</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> makeEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//this function will add enemies to the field</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>enemyTime <span style="color: #66cc66;">&lt;</span> enemyLimit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it isn't time to make them yet</span>
		enemyTime ++;<span style="color: #808080; font-style: italic;">//then keep on waiting</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;">//otherwise</span>
		<span style="color: #000000; font-weight: bold;">var</span> theCode:<span style="color: #0066CC;">int</span> = enemyArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>currentEnemy<span style="color: #66cc66;">&#93;</span>;<span style="color: #808080; font-style: italic;">//get the code from the array</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>theCode == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it's set as 1</span>
			<span style="color: #000000; font-weight: bold;">var</span> newEnemy:Enemy = <span style="color: #000000; font-weight: bold;">new</span> Enemy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then create a new enemy</span>
			enemyHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>newEnemy<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//and add it to the enemyholder</span>
		<span style="color: #66cc66;">&#125;</span>
		currentEnemy ++;<span style="color: #808080; font-style: italic;">//move on to the next enemy</span>
		enemyTime = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//and reset the time</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Also, we have to create an <tt>enemyHolder</tt>. We should do this after the road is put down, so all the enemies will appear on top of it. Add this code right after we run the <tt>makeRoad()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> enemyHolder:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>enemyHolder<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Next, we have to count the amount of enemies that are going to be created. Add this code to the <tt>startGame()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>enemyArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>enemyArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		enemiesLeft ++;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, there is one final thing to do in order to make these enemies work. Find the &#8220;DirectBlock.as&#8221; file for me, would you? Add this code to the <tt>beginClass()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>directType == <span style="color: #ff0000;">'START'</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>directType == <span style="color: #ff0000;">'START'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if this is a start block</span>
		<span style="color: #808080; font-style: italic;">//then define the startDir and StartCoord based on it's coordinates</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> = <span style="color: #ff0000;">'RIGHT'</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> = <span style="color: #ff0000;">'DOWN'</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> == <span style="color: #cc66cc;">525</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> = <span style="color: #ff0000;">'LEFT'</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> == <span style="color: #cc66cc;">275</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startDir</span> = <span style="color: #ff0000;">'UP'</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</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;">//this level won't work if not any of these values</span>
		<span style="color: #66cc66;">&#125;</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>directType == <span style="color: #ff0000;">'FINISH'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if this is a finish block</span>
		<span style="color: #808080; font-style: italic;">//then define the finDir based on it's coordinates</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> = <span style="color: #ff0000;">'LEFT'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> = <span style="color: #ff0000;">'UP'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> == <span style="color: #cc66cc;">525</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> = <span style="color: #ff0000;">'RIGHT'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> == <span style="color: #cc66cc;">275</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">finDir</span> = <span style="color: #ff0000;">'DOWN'</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;">//this level won't work if not any of these values</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, add this code to the <tt>eFrame()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>directType <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'START'</span> <span style="color: #66cc66;">&amp;&amp;</span> directType <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'FINISH'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if this isn't a start of finish block</span>
	<span style="color: #808080; font-style: italic;">//then it'll act as a directioning block</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> = <span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>_root.<span style="color: #006600;">enemyHolder</span>.<span style="color: #006600;">numChildren</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//create a loop</span>
		<span style="color: #000000; font-weight: bold;">var</span> enTarget = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span>.<span style="color: #006600;">getChildAt</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this will hold a certain enemy</span>
		<span style="color: #808080; font-style: italic;">//if the enTarget's coordinates are too close to this block</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= enTarget.<span style="color: #006600;">x</span> - enTarget.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">*</span>.5 <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&lt;</span>= enTarget.<span style="color: #006600;">x</span> + enTarget.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">*</span>.5
		<span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= enTarget.<span style="color: #006600;">y</span> - enTarget.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">*</span>.5 <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= enTarget.<span style="color: #006600;">y</span> + enTarget.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">*</span>.5<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//then move the enemy's direction based on what direction this block points to</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>directType == <span style="color: #ff0000;">'UP'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				enTarget.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
				enTarget.<span style="color: #006600;">ySpeed</span> = -enTarget.<span style="color: #006600;">maxSpeed</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>directType == <span style="color: #ff0000;">'RIGHT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				enTarget.<span style="color: #006600;">xSpeed</span> = enTarget.<span style="color: #006600;">maxSpeed</span>;
				enTarget.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</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>directType == <span style="color: #ff0000;">'DOWN'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				enTarget.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
				enTarget.<span style="color: #006600;">ySpeed</span> = enTarget.<span style="color: #006600;">maxSpeed</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>directType == <span style="color: #ff0000;">'LEFT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				enTarget.<span style="color: #006600;">xSpeed</span> = enTarget.<span style="color: #006600;">maxSpeed</span>;
				enTarget.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Phew! That was a lot of code, wasn&#8217;t it? Well, what these snippets of code do is simply direct the enemies to where they should start and where they should go.</p>
<p>That&#8217;s all for this part of the tutorial. Next time, we&#8217;ll make the turret detect and attack the enemies!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt3/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt3/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt3/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 2</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-2/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-2/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:02:42 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1384</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 2: Adding Turrets Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 2: Adding Turrets</h3>
<p>Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is created. The first step to take in to create a <tt>Turret</tt> class. You know the drill, create a new ActionScript File, save it as &#8220;Turret.as&#8221;, and type in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package<span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//creating the basic skeleton</span>
	imporProxy-Connection: keep-alive
Cache-<span style="color: #0066CC;">Control</span>: max-age=<span style="color: #cc66cc;">0</span>
&nbsp;
flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Turret <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Turret<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;">//adding the required listeners</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//drawing the turret, it will have a gray, circular, base with a white gun</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x999999<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">12.5</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2.5</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eFrameEvents<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//destroy this if game is over</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</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>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This will be only the beginning of what we program into the <tt>Turret</tt>. Next, we have to define a function in the <tt>_root</tt> of the document that will create the turrets. Add this code to the bottom in your source .fla file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> makeTurret<span style="color: #66cc66;">&#40;</span>xValue:<span style="color: #0066CC;">int</span>,yValue:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//this will need to be told the x and y values</span>
	<span style="color: #000000; font-weight: bold;">var</span> turret:Turret = <span style="color: #000000; font-weight: bold;">new</span> Turret<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//creating a variable to hold the Turret</span>
	<span style="color: #808080; font-style: italic;">//changing the coordinates</span>
	turret.<span style="color: #006600;">x</span> = xValue+<span style="color: #cc66cc;">12.5</span>;
	turret.<span style="color: #006600;">y</span> = yValue+<span style="color: #cc66cc;">12.5</span>;
	addChild<span style="color: #66cc66;">&#40;</span>turret<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add it to the stage</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, we can finally make it so the turret is created when the user clicks on an empty block. Find the function <tt>thisClick()</tt> in &#8220;EmptyBlock.as&#8221;. Add the following code to that:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">makeTurret</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span>,<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//make the turret</span>
<span style="color: #808080; font-style: italic;">//remove all the listeners so it can't be clicked on again</span>
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, thisMouseOver<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, thisMouseOut<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, thisClick<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, if you test out the game, a turret should appear whenever you click on any empty block!</p>
<p>Well, that&#8217;s it for this tutorial. Next time, we&#8217;ll add enemies and program them!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt2/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt2/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt2/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:01:36 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1373</guid>
		<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 1: Setting Up a Level The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 1: Setting Up a Level</h3>
<p>The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always very fun to play. I am here to walk you through the creation of one of these games. Let us begin, shall we?</p>
<p>In this section of the tutorial, we&#8217;re going to set up the roads and stuff onto the stage. However, the first thing we need to do is create a blank flash document with a black background with the frames per second set at 24.</p>
<p>Now, we have to create two classes. In order to do this, simply create two external ActionScript files by selecting <strong>File -> New -> ActionScript File</strong>, twice. The first class we&#8217;ll create is one for an empty block that towers can be placed in. Add the following code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//importing required classes for this to work</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EmptyBlock <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//defining the class as EmptyBlock</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;<span style="color: #808080; font-style: italic;">//creating a _root variable to access root easily</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> EmptyBlock<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;">//this function will always run once EmptyBlock is called</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create a function that will run once</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create a enterFrame function</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//setting the _root as the root level</span>
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">true</span>;<span style="color: #808080; font-style: italic;">//make this act like a button</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, thisMouseOver<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//adding function for mouseOver</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, thisMouseOut<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//adding function for mouseOut</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, thisClick<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//adding function for clicking</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eFrameEvents<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<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;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//remove this and listeners if game is over</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrameEvents<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, thisMouseOver<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, thisMouseOut<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, thisClick<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> thisMouseOver<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<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;">//changing the background so the user know's it's clickable</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> thisMouseOut<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<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;">//changing the background back</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> thisClick<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<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;">//we'll add code that'll make a turret be made later</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I&#8217;ve commented the code extensively if you need any explanation. Now, save this file as &#8220;EmptyBlock.as&#8221; in the same folder as your source .fla file. Now, take the second external ActionScript file. We will turn this into a special block which will act as a marker for the road. There are going to be 6 types of this special block, the four different directional notifiers, and the start and finish block. They won&#8217;t look any different than any other block, but they will be very important later. Add this code to that second class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//imports</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DirectBlock <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//we'll call it a DirectBlock</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;<span style="color: #808080; font-style: italic;">//again, defining a _root</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> directType:<span style="color: #0066CC;">String</span>;<span style="color: #808080; font-style: italic;">//what kind of special block is this</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//this time, we have to accept some values to make it easier to place, like the type and coordinates</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DirectBlock<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>,xVal:<span style="color: #0066CC;">int</span>,yVal:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			directType = <span style="color: #0066CC;">type</span>;<span style="color: #808080; font-style: italic;">//set the directType so that all other functions can use it</span>
			<span style="color: #808080; font-style: italic;">//add the required event listeners</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//setting the coordinates</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> = xVal;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> = yVal;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//setting the _root again</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//making this into a 25x25 square</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x111111<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<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;">_root</span>.<span style="color: #006600;">gameOver</span> == <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//destroy this if the game's over</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">//we'll add more code to this later</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This will just set up the blocks. Now, we must return back to the main .fla file. Create a new layer to place actions in, and add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//setting vars to step in for turns and special blocks</span>
<span style="color: #000000; font-weight: bold;">var</span> S:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'START'</span>;
<span style="color: #000000; font-weight: bold;">var</span> F:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'FINISH'</span>;
<span style="color: #000000; font-weight: bold;">var</span> U:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'UP'</span>;
<span style="color: #000000; font-weight: bold;">var</span> R:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'RIGHT'</span>;
<span style="color: #000000; font-weight: bold;">var</span> D:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'DOWN'</span>;
<span style="color: #000000; font-weight: bold;">var</span> L:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'LEFT'</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> startDir:<span style="color: #0066CC;">String</span>;<span style="color: #808080; font-style: italic;">//the direction the enemies go when they enter</span>
<span style="color: #000000; font-weight: bold;">var</span> finDir:<span style="color: #0066CC;">String</span>;<span style="color: #808080; font-style: italic;">//the direction the enemies go when they exit</span>
<span style="color: #000000; font-weight: bold;">var</span> startCoord:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//the coordinates of the beginning of the road</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this array will hold the formatting of the roads</span>
&nbsp;
lvlArray = <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,D,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,D,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,D,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			S,D,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,F,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,U,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,U,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,R,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,U,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,
			<span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//the names of these variables explain what they do</span>
<span style="color: #000000; font-weight: bold;">var</span> currentLvl:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #000000; font-weight: bold;">var</span> gameOver:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> startGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//we'll run this function every time a new level begins</span>
	<span style="color: #808080; font-style: italic;">//right now we don't have any code</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> roadHolder:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create an object that will hold all parts of the road</span>
addChild<span style="color: #66cc66;">&#40;</span>roadHolder<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add it to the stage</span>
<span style="color: #000000; font-weight: bold;">function</span> makeRoad<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: #000000; font-weight: bold;">var</span> row:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//the current row we're working on</span>
	<span style="color: #000000; font-weight: bold;">var</span> block;<span style="color: #808080; font-style: italic;">//this will act as the block that we're placing down</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>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>lvlArray.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//creating a loop that'll go through the level array</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the current index is set to 0</span>
			block = <span style="color: #000000; font-weight: bold;">new</span> EmptyBlock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create a gray empty block</span>
			block.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
			block.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			block.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>block<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//and set the coordinates to be relative to the place in the array</span>
			block.<span style="color: #006600;">x</span>= <span style="color: #66cc66;">&#40;</span>i-row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span>;
			block.<span style="color: #006600;">y</span> = row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</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>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if there is supposed to be a row</span>
			<span style="color: #808080; font-style: italic;">//just add a box that will be a darker color and won't have any actions</span>
			block = <span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			block.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x111111<span style="color: #66cc66;">&#41;</span>;
			block.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			block.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;		
			block.<span style="color: #006600;">x</span>= <span style="color: #66cc66;">&#40;</span>i-row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span>;
			block.<span style="color: #006600;">y</span> = row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span>;	
			roadHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>block<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add it to the roadHolder</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>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> is <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it's a string, meaning a special block</span>
			<span style="color: #808080; font-style: italic;">//then create a special block</span>
			block = <span style="color: #000000; font-weight: bold;">new</span> DirectBlock<span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>,<span style="color: #66cc66;">&#40;</span>i-row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span>,row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>block<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>;c<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">16</span>;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i == c<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">22</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//if 22 columns have gone by, then we move onto the next row</span>
				row++;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//run these functions at the start</span>
makeRoad<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
startGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, if you test out your game, you&#8217;ll see nice little road set up for you. However, we need to keep that empty space in the bottom so we can add stats and other cool stuff.</p>
<p>Well, that concludes this first part of the tutorial. Next time we&#8217;ll make it so you can add turrets!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt1/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt1/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt1/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>So You Wanna Make a Game? A Step-by-Step Guide to Becoming a Flash Game Developer</title>
		<link>http://www.mrsunstudios.com/blog/flash/so-you-wanna-make-a-game-a-step-by-step-guide-to-becoming-a-flash-game-developer/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/so-you-wanna-make-a-game-a-step-by-step-guide-to-becoming-a-flash-game-developer/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 12:00:18 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[develop]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[steps]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1416</guid>
		<description><![CDATA[So you want to be a flash game developer, eh?  It's something that's easy to get into, but hard to get good at.  I'm going to guide you through the process that a total newbie must get into in order to get into the business.]]></description>
			<content:encoded><![CDATA[<p>So you want to be a flash game developer, eh?  It&#8217;s something that&#8217;s easy to get into, but hard to get good at.  I&#8217;m going to guide you through the process that a total newbie must get into in order to get into the business.</p>
<h3>Step 1: Get Flash</h3>
<p>The first obvious step in becoming a flash game developer is to actually get the program.  There are two ways to get flash, expensively or cheaply.  </p>
<p>If you want to get it expensively, you can just go to <a href="http://www.adobe.com">Adobe&#8217;s Site</a> and buy it for a couple hundred dollars.</p>
<p>If you want it for only a fraction of the price, you&#8217;ll have to meet a few requirements.  You either have to be a student, a teacher, a parent of a student, part of a non-profit organization, or anybody that has to do with education.  If you are any of these things, you can get an academic discount!  Just go to any of the following discount online academic stores:</p>
<ul>
<li><a href="http://www.academicsuperstore.com/">Academic Superstore</a></li>
<li><a href="http://www.campustech.com/">CampusTech</a></li>
<li><a href="http://www.gradware.com/">Gradware</a></li>
<li><a href="http://www.ccvsoftware.com/">CCV Software</a></li>
</ul>
<p>You can either get Flash for a few bucks ($248.95 compared to $699.99 from Adobe), or even a suite of programs for even less than Flash costs on Adobe itself ($398.95 &#8211; $598.95). It&#8217;s a pretty sweet deal, ain&#8217;t it?</p>
<h3>Step 2:  Get used to the Program (Optional)</h3>
<p>This step is optional, but still very highly recommended.  I suggest looking at some <a href="http://www.tutorialized.com/tutorials/Flash/1">online tutorials</a> or even buying some <a href="http://www.lynda.com/home/ViewCourses.aspx?lpk0=392">video lessons</a>.  A combination of both is what got me to be comfortable with Flash.  I&#8217;ll just give you a brief description of some panels you&#8217;ll need to learn in order to be efficient with Flash.</p>
<h4>The Toolbar</h4>
<p><img src="http://www.mrsunstudios.com/wp-content/uploads/2009/02/picture-1.gif" alt="Toolbar" title="Toolbar" width="251" height="145" class="alignleft size-full wp-image-1420" /> This is the toolbar.  This is your utility belt.  You&#8217;re going to need this in your journeys as a game developer very much.  I also suggest learning some of the hot keys.</p>
<h4>The Properties Panel</h4>
<p><img src="http://www.mrsunstudios.com/wp-content/uploads/2009/02/picture-2.gif" alt="Properties Panel" title="Properties Panel" width="281" height="153" class="alignright size-full wp-image-1423" /> This is the properties panel.  Depending on what version of flash you have, it&#8217;ll either be horizontal on the bottom or vertical on the sidebar. This is a pretty important panel, as it gives you all of the information you need about a selected object.</p>
<h4>Other Panels that You&#8217;ll Need to Be Familiar With</h4>
<p><strong>The Actions Panel</strong> &#8211; This is where you will type in all of your code.  If you&#8217;re going to be a game developer, you&#8217;ll want to make this as large as possible</p>
<p><strong>The Compiler Errors Panel</strong> &#8211; This is useful if you mess up with your code.  It&#8217;ll pop up whenever a problem occurs with your code and the game won&#8217;t work.</p>
<p><strong>The Output Panel</strong> &#8211; This panel is similar to the compiler errors.  This is where Flash will output warnings.  Your trace statements will also appear here.</p>
<h4>A Good Layout of Panels for a Game Developer</h4>
<p><a href="http://www.mrsunstudios.com/wp-content/uploads/2009/02/screen.gif"><img src="http://www.mrsunstudios.com/wp-content/uploads/2009/02/screensmall.gif" alt="A Screenshot of a Good Layout for Flash" title="A Screenshot of a Good Layout for Flash" width="610" height="356" class="alignnone size-full wp-image-1429" /></a></p>
<h4>Online Tutorials to Get Used to Flash</h4>
<ul>
<li><a href="http://www.w3schools.com/Flash/default.asp">W3 Schools</a></li>
<li><a href="http://www.entheosweb.com/Flash/default.asp">Entheos Web Flash Tutorials</a></li>
<li><a href="http://www.kirupa.com/developer/flash/index.htm">Kirupa</a></li>
<li><a href="http://www.flashdesignerzone.com/tutorials/">Flash Designer Zone Flash Tutorials</a></li>
</ul>
<h3>Step 3: Learn Some ActionScript</h3>
<p>Finally! It&#8217;s time for you to learn some code.  But, which version of ActionScript should you learn first? In my opinion, ActionScript 2.0 is probably the best to gain knowledge of first, as it isn&#8217;t as strict as ActionScript 3.0.  Then, later, once you&#8217;ve gotten the hang of it, you can learn AS3. But, it&#8217;s all up to you.  There are many tutorials online (including on <a href="http://www.mrsunstudios.com/tutorials">my site</a>). Here are some of the ones I suggest:</p>
<ul>
<li><a href="http://www.kirupa.com/developer/flash/index.htm#ActionScript_Basics">Kirupa</a></li>
<li><a href="http://www.flashkit.com/tutorials/Actionscripting/">Flash Kit Tutorials</a></li>
<li><a href="http://www.actionscript.org/resources/categories/Tutorials/Flash/">ActionScript.org Tutorials</a></li>
</ul>
<h3>Step 4: Make some Practice Games (Optional)</h3>
<p>Hopefully, the above tutorials have helped you learn to code.  If you&#8217;re still shaky, then I suggest creating some simple games as &#8220;practice&#8221;.  Tutorials are a must for this step.  I suggest finding some that will walk you through the entire process.  Don&#8217;t expect to make any money with these games, or maybe even to release them to the public.  These games are just for practice.  Games that are great to make as practice are: brick breakers, simple platformers, Pacman, vertical shooters, mouse mazes, and other classic games.  Here are some great tutorials:</p>
<h4>AS2</h4>
<ul>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2/">Brick Breaker Game</a></li>
<li>Avoiding Game</li>
<li><a href="http://www.emanueleferonato.com/2006/10/29/flash-game-creation-tutorial-part-1/">A Game Like Ball Revamped</a></li>
<li><a href="http://www.emanueleferonato.com/2007/04/28/create-a-flash-artillery-game-step-1/">Artillery Game</a></li>
</ul>
<h4>AS3</h4>
<ul>
<li><a href="http://www.mrsunstudios.com/2008/07/tutorial-create-a-brick-breaker-game-in-as3/">Brick Breaker Game</a></li>
<li><a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=6077">Setting Up a Pacman Game</a></li>
<li><a href="http://www.mrsunstudios.com/2008/08/tutorial-create-a-platform-game-in-as3/">Platform Game</a></li>
</ul>
<h3>Step 5: Develop Some Killer Flash Games!</h3>
<p>Now, assuming that you&#8217;ve done all of the previous steps, you can unleash your inner game developer.  The possibilities are now virtually endless for you.  You can create any game you want.  You can make some simple innovative new concept, or maybe even create an epic MMORPG.  Develop your own world.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/so-you-wanna-make-a-game-a-step-by-step-guide-to-becoming-a-flash-game-developer/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 7</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-7/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-7/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:07:04 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1215</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 7: Finishing Touches Well, now for the last step, adding the finishing touches. Just like in my previous tutorial, I won&#8217;t really go into much description in this section, in hopes that you can [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 7: Finishing Touches</h3>
<p>Well, now for the last step, adding the finishing touches. Just like in my previous tutorial, I won&#8217;t really go into much description in this section, in hopes that you can figure out how to do these without my help. As always, the source file is always available at the bottom.</p>
<p>We haven&#8217;t displayed the score to the user yet, so we&#8217;ll do that first. It&#8217;ll be mad easy. Just create a dynamic text field (make sure to embed the font) and set the &#8220;Instance Name&#8221; field to <tt>txtScore</tt>. Of course, when you do this, and error pops up because of the <tt>for</tt> loop that we have. To fix this, just create an entire MovieClip called <tt>mcText</tt> and place all of the dynamic textfields in there.  I&#8217;ll leave the rest of the coding to you for this.</p>
<p>We also have to show the <tt>scoreString</tt>. We can also do this with a dynamic text field. This time, however, the &#8220;Instance Name&#8221; field should be set to <tt>txtScoreString</tt>. Also, we&#8217;ve got to set the scoreString to &#8220;Bad&#8221; whenever the user misses the arrow or hits it at the wrong time. Hopefully you can figure out yourself how to do that yourself.</p>
<p>Now, to show the combo to the player. We first have to define a <tt>combo</tt> variable at the top and set it to 0. Then, we have to increment it every time the user hits the right key, and reset it every time the user doesn&#8217;t. Then, we can just place a dynamic text field that shows the combo where ever we want.</p>
<p>Next, we&#8217;re going to have a win screen and a lose screen. Just make two frames, one labeled &#8220;win&#8221; and the other labeled &#8220;lose&#8221;. Have the user navigate to &#8220;win&#8221; when the game is over, and to &#8220;lose&#8221; when the user loses. It&#8217;s pretty straightforward. If you want to, you can display the stats. Then, make a button that will navigate back to the menu.</p>
<p>Finally, there is one last thing to do. If you haven&#8217;t noticed, whenever we start a game, the arrows don&#8217;t work unless you click on the background first. Simply add this code to the top of the &#8220;game&#8221; frame to fix it:</p>

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

<p>Well, now we&#8217;re basically done. Of course, this is just the beginning of making a game like this. You must figure out everything else on your own if you want to become a real programmer.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt7/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt7/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt7/source.zip">Source Files (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-7/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-6/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-6/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:06:53 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1210</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 6: Scoring The next step in making our rhythm game is scoring. The user will get points depending on how far away the arrow is from the receptor when it hits it. We&#8217;re also [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 6: Scoring</h3>
<p>The next step in making our rhythm game is scoring. The user will get points depending on how far away the arrow is from the receptor when it hits it. We&#8217;re also going to make the health bar gain and lose parts when the user hits or misses the receptor. First of all, we need to define a score variable at the top of the game frame. We also have to define a String variable that will show how well the user hits the arrow.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//the score variable</span>
<span style="color: #000000; font-weight: bold;">var</span> score:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #808080; font-style: italic;">//either perfect, great, nice, or good</span>
<span style="color: #000000; font-weight: bold;">var</span> scoreString:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">''</span>;</pre></div></div>

<p>That was pretty easy. Next, we want to increase the score whenever the user hits the right key when it hits the score. We&#8217;re going to make it increase by 10 for a perfect hit, 8 for a great hit, 6 for an nice hit, and 4 for a good hit.  Add this code to the &#8220;Arrow.as&#8221; class in the <tt>checkKeys()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkKeys<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<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;">//checking if a certain key is down and it's touching the receptor</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == arrowCode <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>				
		<span style="color: #808080; font-style: italic;">//checking if the correct key is down</span>
		<span style="color: #808080; font-style: italic;">//if the user hits it about perfectly</span>
		<span style="color: #808080; font-style: italic;">//the receptors y coordinate is about 10 px away from the</span>
		<span style="color: #808080; font-style: italic;">//symbol's y coordinate.</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">15</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">10</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Perfect'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">25</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span>-<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">8</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Great'</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> -<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">6</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Nice'</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">4</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Good'</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove it from stage</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The next thing we have to do now is program the health bar. If the user misses or hits the arrow key at the wrong time, then we have to lose health. If the user hits it right though, the health will increase. First of all, give the health bar an instance name, <tt>mcHealth</tt>. Then, we&#8217;re going to make a function for this, so we don&#8217;t have to write all the if statements repeatedly. Define this function at the bottom of your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//this function will change the health depending on how much health change</span>
<span style="color: #808080; font-style: italic;">//it needs, positive or negative</span>
<span style="color: #000000; font-weight: bold;">function</span> changeHealth<span style="color: #66cc66;">&#40;</span>healthDiff:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	healthDiff <span style="color: #66cc66;">/</span>= <span style="color: #cc66cc;">100</span>;<span style="color: #808080; font-style: italic;">//the scaleX only changes in percentages</span>
	<span style="color: #808080; font-style: italic;">//checking if the health is already at it's full</span>
	<span style="color: #808080; font-style: italic;">//or will be full after this hit</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcHealth.<span style="color: #006600;">scaleX</span> + healthDiff <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		mcHealth.<span style="color: #006600;">scaleX</span> = <span style="color: #cc66cc;">1</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>mcHealth.<span style="color: #006600;">scaleX</span> + healthDiff <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//checking if the health will be equal or below 0</span>
		<span style="color: #808080; font-style: italic;">//lose function will go here</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if there are no problems</span>
		mcHealth.<span style="color: #006600;">scaleX</span> += healthDiff;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The next thing we&#8217;re going to do is run this function positively when the user hits the key correctly. In the arrow&#8217;s code where it hit tests with the receptor, put this code for perfect:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">changeHealth<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This will increase the health by 5. You can do the rest yourself, just keep on decrementing the health change by 1, and you&#8217;ll be good.</p>
<p>Next comes the hard part, making the user lose health. Again, we have to define a few variables at the top. They will be Booleans saying whether or not the receptor is touching a certain key.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Booleans checking if the arrows are touching the receptor</span>
<span style="color: #000000; font-weight: bold;">var</span> touchLeft:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #000000; font-weight: bold;">var</span> touchUp:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #000000; font-weight: bold;">var</span> touchDown:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #000000; font-weight: bold;">var</span> touchRight:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;</pre></div></div>

<p>Now, add this code to the <tt>makeLvl()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//checking if mcReceptor is touching any arrows</span>
	<span style="color: #808080; font-style: italic;">//first we reset the variables we got last time just in case they aren't true anymore</span>
	touchLeft = <span style="color: #000000; font-weight: bold;">false</span>;
	touchUp = <span style="color: #000000; font-weight: bold;">false</span>;
	touchDown = <span style="color: #000000; font-weight: bold;">false</span>;
	touchRight = <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #808080; font-style: italic;">//this for loop will be used for the hit testing</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> = <span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>numChildren;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span> = getChildAt<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">target</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span>mcReceptor<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the object is an arrow and is touching the receptor</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">37</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if left arrow</span>
				touchLeft = <span style="color: #000000; font-weight: bold;">true</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><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if up arrow</span>
				touchUp = <span style="color: #000000; font-weight: bold;">true</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><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if down arrow</span>
				touchDown = <span style="color: #000000; font-weight: bold;">true</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><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">39</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if right arrow</span>
				touchRight = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now that we have the variables checking which arrow is touching the receptor, now we need some key checkers. Add this to the bottom of your code in the &#8220;game&#8221; frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, checkKeys<span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">function</span> checkKeys<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if the left key is down and no left arrows are touching the receptor</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">37</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchLeft<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> 
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//make the health go down</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><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">38</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchUp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//and so on</span>
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</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><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchDown<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</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><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">39</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchRight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Phew, that took a while. Now, for the final part of losing health, when the person misses the arrow altogether. Place this code at the end of the arrow&#8217;s <tt>eFrame</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//if game is over or it's off the stage, destroy it</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if off the stage</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">changeHealth</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all for scoring. This probably has been the longest lesson in the series. If you survived through this without having any trouble, then you can do the next part without my help, the finishing touches.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt6/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt6/source.zip">Source Files (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

