<?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; actionscript</title>
	<atom:link href="http://www.mrsunstudios.com/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mrsunstudios.com</link>
	<description>All about Design, Programming and Game Development.</description>
	<lastBuildDate>Tue, 28 Jul 2009 15:03:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:07:48 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1534</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 won&#8217;t comment any of the code I give you, [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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>Now, what 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-as2/pt7/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt7/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt7/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:06:07 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1531</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 going to create more enemies and more levels. Sounds [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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>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;">Number</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;">&amp;&amp;</span> theCode <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 it's not set at 0</span>
			<span style="color: #808080; font-style: italic;">//then create a new enemy and add it to the enemy holder</span>
			enemyHolder.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy,enemyHolder.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//now we're going to draw the enemy. It'll just be a tiny red circle</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF0000<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//coloring them red gray</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2.5</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//move the entire shape a certain way</span>
			<span style="color: #808080; font-style: italic;">//create 4 curves so that it'll look like a circle</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//end the fill</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//add a few variables to the enemy</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">enLevel</span> = theCode;<span style="color: #808080; font-style: italic;">//setting its level to be what # it is</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span> = <span style="color: #cc66cc;">3</span>;<span style="color: #808080; font-style: italic;">//how fast it can possibly go</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">health</span> = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">*</span>theCode;
&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>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">300</span>;<span style="color: #808080; font-style: italic;">//set the y value off the field</span>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</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>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = -enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;<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>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = -<span style="color: #cc66cc;">25</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</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>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = -<span style="color: #cc66cc;">25</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<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><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>
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">550</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = -enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;
				enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> += <span style="color: #cc66cc;">5</span>;<span style="color: #808080; font-style: italic;">//fixing the x value</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> += <span style="color: #cc66cc;">5</span>;<span style="color: #808080; font-style: italic;">//fixing up the y value</span>
&nbsp;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//give it some functions</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> += <span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> += <span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span>;
&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: #0066CC;">_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>
						<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: #0066CC;">_root</span>.<span style="color: #006600;">money</span> -= <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">enLevel</span>;<span style="color: #808080; font-style: italic;">//don't let the player gain any money</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//take it away from the stage</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: #0066CC;">_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>
						<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
						<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;">this</span>.<span style="color: #006600;">enLevel</span>;
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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: #0066CC;">_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>
						<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
						<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;">this</span>.<span style="color: #006600;">enLevel</span>;
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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: #0066CC;">_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>
						<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
						<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;">this</span>.<span style="color: #006600;">enLevel</span>;
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">health</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>
					<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemiesLeft</span> --;
					<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;">this</span>.<span style="color: #006600;">enLevel</span>;
					<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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: #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>There aren&#8217;t very many renovations that we&#8217;ve made, but they will make our code much more flexible. It allows us to create different enemy levels by setting the <tt>enLevel</tt> to be equal to the code that is placed into the enemy array. The <tt>enLevel</tt> in turn lets us dynamically change the amount of health the enemy has and the amount of money that it gives you when you kill it. Right now the health and money it gives you is 5 times the enemy level.</p>
<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-as2/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt6/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt6/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:05:40 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1519</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 in, shall we?
Lets start off by opening up the [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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 first count the number of enemies we actually have on stage. This is actually very easy to do. Find the <tt>startGame()</tt> function (~line 45) that has no code in it, and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">enemiesLeft = 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>;</pre></div></div>

<p>Now, we have to decrease this number every time an enemy is destroyed. We can do this by adding one line of code to where we remove the Enemy from the stage in the <tt>makeEnemies()</tt> function. Add the following code (~line 346):</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>Now, find the main <tt>_root.onEnterFrame()</tt> function (~line 287). 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 it:</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>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;">Number</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;">Number</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. </p>
<p>In order to do this, we must add the following code to the Enemy&#8217;s <tt>onEnterFrame()</tt> function (~line 353):</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: #0066CC;">_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>
		<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: #0066CC;">_root</span>.<span style="color: #006600;">money</span> -= <span style="color: #cc66cc;">5</span>;<span style="color: #808080; font-style: italic;">//don't let the player gain any money</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//take it away from the stage</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: #0066CC;">_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>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> -= <span style="color: #cc66cc;">5</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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: #0066CC;">_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>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> -= <span style="color: #cc66cc;">5</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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: #0066CC;">_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>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lives</span> --;
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> -= <span style="color: #cc66cc;">5</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">health</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>
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemiesLeft</span> --;
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">money</span> += <span style="color: #cc66cc;">5</span>;
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Note that we also are giving the player money for killing the enemies. Next, we&#8217;ll make each of the turrets cost a certain amount of money. I&#8217;m thinking $20 is a good price. Find the block&#8217;s <tt>onRelease</tt> function (~line 80). We have to wrap all of its contents with <tt>if(_root.money >= 20){money-=20;......}</tt>. This way, the final <tt>onRelease</tt> function will look like 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: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onRelease</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</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;">_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 there's enough money</span>
		<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 it and make a turret</span>
		<span style="color: #808080; font-style: italic;">//this function will run when the empty block is clicked on</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//change this guy's color back</span>
		<span style="color: #000000; font-weight: bold;">var</span> newColor = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Color</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
		newColor.<span style="color: #0066CC;">setRGB</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//set all other mouse functions to null in order to keep it from being clicked again</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onRollOver</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onRollOut</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onRelease</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #808080; font-style: italic;">//create an empty turret movieclip that will be on the top root layer</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span>,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x999999<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//coloring the base light gray</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">12.5</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//move the entire shape a certain way</span>
		<span style="color: #808080; font-style: italic;">//create 4 curves so that it'll look like a circle</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">12.5</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//end the fill so we can make a new one</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//creating the gun</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'gun'</span>,<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2</span>,-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2</span>,-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//setting the gun to be on the center of the turret</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">12.5</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">12.5</span>;
		<span style="color: #808080; font-style: italic;">//set the turrets coordinates to be the blocks coordinates</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>;
&nbsp;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">angle</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #808080; font-style: italic;">//the angle that the turret is currently rotated at</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">radiansToDegrees</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">damage</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">range</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">enTarget</span> = <span style="color: #000000; font-weight: bold;">null</span>;<span style="color: #808080; font-style: italic;">//the current target that it's rotating towards</span>
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">cTime</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">reloadTime</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">loaded</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>
&nbsp;
		<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//FINDING THE NEAREST ENEMY WITHIN RANGE</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">distance</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">range</span>;<span style="color: #808080; font-style: italic;">//let's define a variable which will be how far the nearest enemy is</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span> = <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;">_root</span>.<span style="color: #006600;">currentEnemy</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: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</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: #0066CC;">_y</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>, <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: #0066CC;">_x</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">distance</span><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>
					<span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span> = 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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span> <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;">gun</span>.<span style="color: #0066CC;">_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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>._y-<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>._x-<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span><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;">this</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;">this</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: #808080; font-style: italic;">//create a bullet</span>
					<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span>,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #808080; font-style: italic;">//draw the bullet</span>
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #808080; font-style: italic;">//set the bullet's coordinates</span>
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">12.5</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>+<span style="color: #cc66cc;">12.5</span>;
					<span style="color: #808080; font-style: italic;">//set the bullet's target and damage</span>
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">target</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>;
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">damage</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">damage</span>;
&nbsp;
					<span style="color: #808080; font-style: italic;">//add some functions to this bullet</span>
					<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">maxSpeed</span>=<span style="color: #cc66cc;">8</span>;
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">yDist</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_y</span>+<span style="color: #cc66cc;">12.5</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>;<span style="color: #808080; font-style: italic;">//how far this guy is from the enemy (x)</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xDist</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">12.5</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>;<span style="color: #808080; font-style: italic;">//how far it is from the enemy (y)</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">angle</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">yDist</span>,<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xDist</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//the angle that it must move</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">angle</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span>;<span style="color: #808080; font-style: italic;">//calculate how much it should move the enemy vertically</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">angle</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span>;<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: #0066CC;">_x</span>+= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span>;
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>+= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span>;
						<span style="color: #808080; font-style: italic;">//check if it is close to the enemy</span>
						<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>._x-<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&lt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_x</span>
						<span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>+<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>._y-<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&lt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
							<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">health</span> -= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">damage</span>;<span style="color: #808080; font-style: italic;">//make the enemy lose health</span>
							<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove this sucker</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>
			<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;">this</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>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">cTime</span> ++;<span style="color: #808080; font-style: italic;">//then continue the time</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">cTime</span> == <span style="color: #0066CC;">this</span>.<span style="color: #006600;">reloadTime</span><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;">this</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>
					<span style="color: #0066CC;">this</span>.<span style="color: #006600;">cTime</span> = <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>
		<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>_root.onEnterFrame()</tt> function (~line 300). 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 both the &#8220;win&#8221;  frame and the &#8220;lose&#8221; frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">timeElapsed = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//how many frames have elapsed since this screen has been shown</span>
&nbsp;
<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	timeElapsed ++;<span style="color: #808080; font-style: italic;">//increase the amount of frames that have elapsed</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>timeElapsed == <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if 5 seconds (@24fps) have elapsed</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 back to restart the game</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</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 frame 1 and find the <tt>_root.onEnterFrame()</tt> function (~line 293). 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;
		roadHolder.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the pieces of road</span>
		enemyHolder.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the enemies</span>
		<span style="color: #808080; font-style: italic;">//remove all of the blocks</span>
		<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>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>
				<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//destroy the empty block</span>
				<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tblock'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//destroy the turret</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</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: #0066CC;">_root</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #808080; font-style: italic;">//remove this function</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;
		roadHolder.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the pieces of road</span>
		enemyHolder.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove the enemies</span>
		<span style="color: #808080; font-style: italic;">//remove all of the blocks</span>
		<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>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>
				<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//destroy the empty block</span>
				<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tblock'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//destroy the turret</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</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 win frame</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #808080; font-style: italic;">//remove this function</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-as2/pt5/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt5/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt5/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:04:22 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1509</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 the Enemy. Find this code in the makeEnemies() function (~ [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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. Find this code in the <tt>makeEnemies()</tt> function (~ Line 236):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//add a few variables to the enemy</span>
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span> = <span style="color: #cc66cc;">3</span>;<span style="color: #808080; font-style: italic;">//how fast it can possibly go</span>
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Just add this code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">health</span> = <span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>Now, we&#8217;re going to have to add some variables to the turrets. In the <tt>makeRoad()</tt> function, find the <tt>onRelease</tt> function of the Empty Block (~ Line 77). Add this code to the bottom of that function (~ Line 116).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">angle</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #808080; font-style: italic;">//the angle that the turret is currently rotated at</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">radiansToDegrees</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">damage</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">range</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">enTarget</span> = <span style="color: #000000; font-weight: bold;">null</span>;<span style="color: #808080; font-style: italic;">//the current target that it's rotating towards</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">cTime</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">reloadTime</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">loaded</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 the following after the code you just added above:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//FINDING THE NEAREST ENEMY WITHIN RANGE</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">distance</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">range</span>;<span style="color: #808080; font-style: italic;">//let's define a variable which will be how far the nearest enemy is</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span> = <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;">_root</span>.<span style="color: #006600;">currentEnemy</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: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</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: #0066CC;">_y</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>, <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: #0066CC;">_x</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">distance</span><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>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span> = 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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span> <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;">gun</span>.<span style="color: #0066CC;">_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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>._y-<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>._x-<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span><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;">this</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: #808080; font-style: italic;">//subtract the enemy's health</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>.<span style="color: #006600;">health</span> -= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">damage</span>;
			<span style="color: #0066CC;">this</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: #808080; font-style: italic;">//create a bullet</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span>,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//draw the bullet</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//set the bullet's coordinates</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">12.5</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>+<span style="color: #cc66cc;">12.5</span>;
			<span style="color: #808080; font-style: italic;">//set the bullet's target and damage</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">target</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">enTarget</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//add some functions to this bullet</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">maxSpeed</span>=<span style="color: #cc66cc;">4</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">yDist</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_y</span>+<span style="color: #cc66cc;">12.5</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>;<span style="color: #808080; font-style: italic;">//how far this guy is from the enemy (x)</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xDist</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">12.5</span> - <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>;<span style="color: #808080; font-style: italic;">//how far it is from the enemy (y)</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">angle</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">atan2</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">yDist</span>,<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xDist</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//the angle that it must move</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">angle</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span>;<span style="color: #808080; font-style: italic;">//calculate how much it should move the enemy vertically</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span>=<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">angle</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span>;<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: #0066CC;">_x</span>+= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>+= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span>;
				<span style="color: #808080; font-style: italic;">//check if it is close to the enemy</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>._x-<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&lt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_x</span>
				<span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>+<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>._y-<span style="color: #0066CC;">this</span>.<span style="color: #006600;">maxSpeed</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&lt;</span>=<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">health</span> -= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">damage</span>;<span style="color: #808080; font-style: italic;">//make the enemy lose health</span>
					<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove this sucker</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>
	<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;">this</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>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">cTime</span> ++;<span style="color: #808080; font-style: italic;">//then continue the time</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">cTime</span> == <span style="color: #0066CC;">this</span>.<span style="color: #006600;">reloadTime</span><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;">this</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>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">cTime</span> = <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>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, if you test out the game and create some turrets, they should start shooting at those darn enemies! Next, we have to make it so the enemy dies when shot. This will be pretty easy to do. Find the code where we added the <tt>onEnterFrame()</tt> code to the enemy and made it move (~line 342). Just add this little tidbit of code:</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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">health</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>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><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-as2/pt4/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt4/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt4/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:03:10 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1498</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.
Let&#8217;s begin by adding the enemy to the stage. Like [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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&#8217;s begin by adding the enemy to the stage. Like the other symbols, we&#8217;re going to do it by creating an empty MovieClip and drawing the shape into it. In order to do this, however, we must first define some variables. Do this at the top of the code:</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;">Number</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;">Number</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;">Number</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;">Number</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>I&#8217;ve extensively commented on what each variable does. Next, we need to create an <tt>onEnterFrame()</tt> function with a few actions, along with a few other stuff. Program this into the bottom of the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'enemyHolder'</span>,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create a movieclip that will hold the enemy</span>
&nbsp;
<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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;">Number</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: #808080; font-style: italic;">//then create a new enemy and add it to the enemy holder</span>
			enemyHolder.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy,enemyHolder.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//now we're going to draw the enemy. It'll just be a tiny red circle</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF0000<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//coloring them red gray</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2.5</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//move the entire shape a certain way</span>
			<span style="color: #808080; font-style: italic;">//create 4 curves so that it'll look like a circle</span>
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
			enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//end the fill</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>Now, if you test out the game, a red dot should appear on the top left corner of the screen. But, this isn&#8217;t what we want for our game, is it? In order to place the enemy so that it&#8217;s right next to the start point, we&#8217;re going to have to add some code. Remember the variables <tt>startDir</tt> and <tt>finDir</tt> that we created all that time ago? If you don&#8217;t here&#8217;s what it should look like at around line 11:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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;">Number</span>;<span style="color: #808080; font-style: italic;">//the coordinates of the beginning of the road</span></pre></div></div>

<p>Well, we&#8217;re going to use these variables. Find in the <tt>makeRoad()</tt> function where we create add in the <strong>SPECIAL DIRECTIONAL ROAD PIECE</strong> (around line 129). Add the following code to the bottom of that <tt>if</tt> statement:</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>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == <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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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> = roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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> = roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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> = roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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> = roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</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 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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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, we have to take the variables we just defined in this code and make the enemy use them. We also have to make the enemy move along the path. Go back to the <tt>makeEnemies()</tt> function and add the following code in the <tt>if(theCode == 1){</tt> statement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//add a few variables to the enemy</span>
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span> = <span style="color: #cc66cc;">3</span>;<span style="color: #808080; font-style: italic;">//how fast it can possibly go</span>
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;
<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>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">300</span>;<span style="color: #808080; font-style: italic;">//set the y value off the field</span>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</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>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = -enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;<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>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = -<span style="color: #cc66cc;">25</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</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>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = -<span style="color: #cc66cc;">25</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<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><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>
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">550</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">startCoord</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = -enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;
	enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> += <span style="color: #cc66cc;">5</span>;<span style="color: #808080; font-style: italic;">//fixing the x value</span>
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> += <span style="color: #cc66cc;">5</span>;<span style="color: #808080; font-style: italic;">//fixing up the y value</span>
&nbsp;
enemyHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+currentEnemy<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//give it some functions</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> += <span style="color: #0066CC;">this</span>.<span style="color: #006600;">xSpeed</span>;
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> += <span style="color: #0066CC;">this</span>.<span style="color: #006600;">ySpeed</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The final thing we have to do in this lesson is make the enemy turn when it should turn. To do this, we must go back to the <strong>Directional Block</strong>. We&#8217;re going to use the directional block to access all of the enemies coordinates. If the coordinates are close enough to the block, then it will make the enemy change direction. Find the <tt>if(String(lvlArray[i])){</tt> statement in the <tt>makeRoad()</tt> function. Add this code to the end of it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">directType</span> = lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<span style="color: #808080; font-style: italic;">//accessing the type of block it is</span>
roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//add some functions to this 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>i = <span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>_root.<span style="color: #006600;">enemyArray</span><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: #808080; font-style: italic;">//create a loop</span>
		<span style="color: #808080; font-style: italic;">//if the enemy'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: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> - <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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: #0066CC;">_x</span> +<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> + <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> - <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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: #0066CC;">_y</span> +<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> + <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">directType</span> == <span style="color: #ff0000;">'UP'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = -<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">directType</span> == <span style="color: #ff0000;">'RIGHT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</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;">this</span>.<span style="color: #006600;">directType</span> == <span style="color: #ff0000;">'DOWN'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #cc66cc;">0</span>;
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">ySpeed</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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><span style="color: #0066CC;">this</span>.<span style="color: #006600;">directType</span> == <span style="color: #ff0000;">'LEFT'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">xSpeed</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">maxSpeed</span>;
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">enemyHolder</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'enemy'</span>+i<span style="color: #66cc66;">&#93;</span>.<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>

<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-as2/pt3/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt3/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt3/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:02:57 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1491</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 created. Let&#8217;s begin, shall we?
Open up your main flash [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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. Let&#8217;s begin, shall we?</p>
<p>Open up your main flash file and find the code where we set the empty block&#8217;s <tt>onRollOver()</tt> and <tt>onRollOut</tt> functions. Well, now we&#8217;re going to create a <tt>onRelease()</tt> function, which just means clicking. Add this code to around <strong>Line 65</strong>, or after you set the <tt>onRollOver</tt> and <tt>onRollOut</tt> functions:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onRelease</span> = <span style="color: #000000; font-weight: bold;">function</span><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 run when the empty block is clicked on</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//change this guy's color back</span>
	<span style="color: #000000; font-weight: bold;">var</span> newColor = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Color</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	newColor.<span style="color: #0066CC;">setRGB</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//set all other mouse functions to null in order to keep it from being clicked again</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onRollOver</span> = <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onRollOut</span> = <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onRelease</span> = <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #808080; font-style: italic;">//create an empty turret movieclip that will be on the top root layer</span>
	<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span>,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x999999<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//coloring the base light gray</span>
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">12.5</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//move the entire shape a certain way</span>
	<span style="color: #808080; font-style: italic;">//create 4 curves so that it'll look like a circle</span>
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">12.5</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">12.5</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</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;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//end the fill so we can make a new one</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//creating the gun</span>
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'gun'</span>,<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2</span>,-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">2</span>,-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//setting the gun to be on the center of the turret</span>
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">12.5</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">gun</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">12.5</span>;
	<span style="color: #808080; font-style: italic;">//set the turrets coordinates to be the blocks coordinates</span>
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'t'</span>+<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_name</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, it&#8217;s a lot of code, but it gets the job done.</p>
<p>That&#8217;s it for this part of the tutorial. Next time, we&#8217;ll create and program some 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-as2/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-as2/pt2/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2</title>
		<link>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/</link>
		<comments>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:01:32 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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=1481</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 very fun to play. I am here to walk [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-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 can dive into some code. Let&#8217;s first create some code that will lay down the track for the enemies to go through. Create a new layer called &#8220;actions&#8221; and add the following code to it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
</pre></td><td 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;">Number</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;">Number</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: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'roadHolder'</span>,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add a MovieClip to hold the road</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;">Number</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>i=<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>
			<span style="color: #808080; font-style: italic;">/////*****EMPTY BLOCK*****/////</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'block'</span>+i,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//create a gray empty block</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><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>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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;">/////*****EMPTY ROAD*****/////</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>
			roadHolder.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'block'</span>+i,roadHolder.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x111111<span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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><span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</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's a string, meaning a special block</span>
			<span style="color: #808080; font-style: italic;">/////*****SPECIAL DIRECTIONAL ROAD PIECE*****/////</span>
			roadHolder.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'block'</span>+i,roadHolder.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x111111<span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_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>;
			roadHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = row<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</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;">Number</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></td></tr></table></div>

<p>That&#8217;s a lot of code, ain&#8217;t it? Hopefully, I&#8217;ve given you enough instructions in the comments. If you test out our game, a nice little path should be shown which the enemies will travel through, along with some blank gray boxes where we&#8217;ll be able to place some turrets.</p>
<p>Before I end this part of the tutorial, I want to give these empty boxes some <tt>rollOver</tt> and <tt>rollOut</tt> effects. Find where I added the comment saying <tt>/////*****EMPTY BLOCK*****/////</tt> (lines 44-54). Add this code to the bottom of that section of the <tt>if</tt> 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: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onRollOver</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//Change the color to green</span>
	<span style="color: #000000; font-weight: bold;">var</span> newColor = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Color</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	newColor.<span style="color: #0066CC;">setRGB</span><span style="color: #66cc66;">&#40;</span>0x006600<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'block'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onRollOut</span> = <span style="color: #000000; font-weight: bold;">function</span><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;">//Change this color back to gray</span>
	<span style="color: #000000; font-weight: bold;">var</span> newColor = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Color</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	newColor.<span style="color: #0066CC;">setRGB</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<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-as2/pt1/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt1/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt1/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/" title="Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>4 Reasons Why All Flash Game Developers Should Learn PHP</title>
		<link>http://www.mrsunstudios.com/2009/03/4-reasons-why-all-flash-game-developers-should-learn-php/</link>
		<comments>http://www.mrsunstudios.com/2009/03/4-reasons-why-all-flash-game-developers-should-learn-php/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 17:59:12 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1477</guid>
		<description><![CDATA[As a person, it is always to important to explore all areas of a subject.  That is why, as a flash game developer, you must extend your knowledge to all corners of code.  The most relevant programming language to ActionScript, as you may have already guessed, is PHP.  Here are some reasons [...]]]></description>
			<content:encoded><![CDATA[<p>As a person, it is always to important to explore all areas of a subject.  That is why, as a flash game developer, you must extend your knowledge to all corners of code.  The most relevant programming language to ActionScript, as you may have already guessed, is PHP.  Here are some reasons why you must educate yourself in this language.</p>
<h3>1. The Syntax is Very Similar to ActionScript</h3>
<p>PHP has very similar syntax rules to ActionScript.  This will help you both in learning PHP, and in improving your knowledge in AS2 or AS3. I actually learned both languages at around the same time, and almost everything I learned in one subject could be applied to the other.  For example, when I learned that you could shorthand additive actions in PHP with a &#8220;++&#8221; or &#8220;+=&#8221;, I instantly figured out that you could do the same with ActionScript.  As a beginner, it&#8217;ll help you very much.</p>
<p>But, of course, not everybody is a beginner at coding.  There are still some many other reasons to learn PHP, like&#8230;</p>
<h3>2. PHP is Used Everywhere</h3>
<p>Let&#8217;s face it, almost every website out there runs on PHP.  We cannot change this, so we must change our knowledge to be a part of the many people who use PHP. Some examples of popular uses of PHP are:</p>
<ul>
<li>Content Manage Systems</li>
<li>Browser-Based Games</li>
<li>Online Applications</li>
<li>Blogs</li>
</ul>
<p>Most likely, one of these is going to applicable to what you&#8217;re going to do in flash.</p>
<h3>3. PHP Can be Used Hand-in-Hand with Flash</h3>
<p>Out of all the programming and scripting languages in the world, the best match for flash is PHP.  They are meant to be together, like husband and wife.  There are many ways that Flash and PHP can be used together.  Here&#8217;s a list of ways they can be used:</p>
<ul>
<li>High Scores</li>
<li>Account Systems</li>
<li>Dynamic Flash Sites</li>
<li>Image Galleries</li>
</ul>
<h3>4. It&#8217;s Very Easy to Learn PHP</h3>
<p>Due to PHP&#8217;s popularity, thousands of tutorials have popped up all over the web to teach you how to code it.  It doesn&#8217;t matter if you&#8217;re not the best at learning code. Because there are so many tutorials out there, you&#8217;ll definitely find one that works for you. Here are a bunch of tutorials that I suggest:</p>
<ul>
<li><a href="http://us2.php.net/tut.php">A Simple PHP Tutorial</a></li>
<li><a href="http://www.w3schools.com/PHP/DEfaULT.asP">W3 Schools PHP Tutorials</a></li>
<li><a href="http://www.tizag.com/phpT/">Tizag PHP Tutorials</a></li>
<li><a href="http://www.phpvideotutorials.com/">PHP Video Tutorials</a> (For You Visual Learners Out There</li>
<li><a href="http://www.lynda.com/home/DisplayCourse.aspx?lpk2=435">Lynda.com: PHP with MySQL Essential Training</a> (A Great Tutorial but Costs Money)</li>
</ul>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-6/" title="Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 6">Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-5/" title="Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 5">Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-4/" title="Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 4">Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-3/" title="Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 3">Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-2/" title="Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 2">Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/03/4-reasons-why-all-flash-game-developers-should-learn-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 7</title>
		<link>http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/</link>
		<comments>http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:07:28 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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 won&#8217;t comment any of the code I give you, [...]]]></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>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 6">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 6</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/</link>
		<comments>http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:06:41 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></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 going to create more enemies and more levels. Sounds [...]]]></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>
<h3  class="related_post_title">Similar Stuff</h3><ul class="related_post"><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 7">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 7</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 5">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 5</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 4">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 4</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 3">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 3</a></li><li><a href="http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/" title="Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 2">Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
