<?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>Flash Game Tuts &#187; AS3</title>
	<atom:link href="http://www.flashgametuts.com/tutorials/category/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flashgametuts.com</link>
	<description>Free Flash Game Tutorials in AS2 and AS3</description>
	<lastBuildDate>Wed, 29 Jul 2009 14:43:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 7</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-7/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-7/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:07:28 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></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[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, [...]]]></description>
			<content:encoded><![CDATA[<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="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> rangeCircle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
rangeCircle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x006600,.5<span style="color: #000000;">&#41;</span>;
rangeCircle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">12.5</span>,<span style="color: #000000; font-weight:bold;">12.5</span>,<span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>;
rangeCircle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;">_root.rangeCircle.<span style="color: #004993;">x</span> = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span>;
_root.rangeCircle.<span style="color: #004993;">y</span> = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span>;
_root.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>_root.rangeCircle<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;">_root.<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>_root.rangeCircle<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_OVER</span>, thisMouseOver<span style="color: #000000;">&#41;</span>;
<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_OUT</span>, thisMouseOut<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> thisMouseOver<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	_root.rangeCircle.<span style="color: #004993;">x</span> = <span style="color: #0033ff; font-weight: bold;">this</span>.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">12.5</span>;
	_root.rangeCircle.<span style="color: #004993;">y</span> = <span style="color: #0033ff; font-weight: bold;">this</span>.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">12.5</span>;
	_root.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>_root.rangeCircle<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> thisMouseOut<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	_root.<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>_root.rangeCircle<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 6</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-6/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-6/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:06:41 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></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[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? The first thing we&#8217;re going to do before creating stronger enemies is to make the enemies [...]]]></description>
			<content:encoded><![CDATA[<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="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//destroy this if health is equal to or below 0</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>health <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;">_root.money <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">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="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> makeEnemies<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//this function will add enemies to the field</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>enemyTime <span style="color: #000000; font-weight: bold;">&lt;</span> enemyLimit<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it isn't time to make them yet</span>
		enemyTime <span style="color: #000000; font-weight: bold;">++</span>;<span style="color: #009900;">//then keep on waiting</span>
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//otherwise</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> theCode<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = enemyArray<span style="color: #000000;">&#91;</span>currentLvl<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>currentEnemy<span style="color: #000000;">&#93;</span>;<span style="color: #009900;">//get the code from the array</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>theCode <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it isn't an empty space</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> newEnemy<span style="color: #000000; font-weight: bold;">:</span>Enemy = <span style="color: #0033ff; font-weight: bold;">new</span> Enemy<span style="color: #000000;">&#40;</span>theCode<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//then create a new enemy and pass in the code</span>
			enemyHolder.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>newEnemy<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//and add it to the enemyholder</span>
		<span style="color: #000000;">&#125;</span>
		currentEnemy <span style="color: #000000; font-weight: bold;">++</span>;<span style="color: #009900;">//move on to the next enemy</span>
		enemyTime = <span style="color: #000000; font-weight:bold;">0</span>;<span style="color: #009900;">//and reset the time</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> startGame<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//we'll run this function every time a new level begins</span>
	<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>;i<span style="color: #000000; font-weight: bold;">&lt;</span>enemyArray<span style="color: #000000;">&#91;</span>currentLvl<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #004993;">length</span>;i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>enemyArray<span style="color: #000000;">&#91;</span>currentLvl<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			enemiesLeft <span style="color: #000000; font-weight: bold;">++</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _root<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> xSpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//how fast it's going horizontally</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> ySpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//how fast it's going vertically</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> maxSpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">3</span>;<span style="color: #009900;">//how fast it can possibly go</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> health<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">level</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//this will be set to the number passed in</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Enemy<span style="color: #000000;">&#40;</span><span style="color: #004993;">code</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED</span>, beginClass<span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, eFrameEvents<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #004993;">level</span> = <span style="color: #004993;">code</span>;<span style="color: #009900;">//set the level to the value passed in for use in other functions</span>
<span style="color: #000000;">&#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="actionscript3" style="font-family:monospace;">health = <span style="color: #004993;">level</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">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="actionscript3" style="font-family:monospace;">_root.money <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">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="actionscript3" style="font-family:monospace;">enemyArray = <span style="color: #000000;">&#91;</span><span style="color: #009900;">//defining the array</span>
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>,<span style="color: #009900;">//1's will just represent an enemy to be created</span>
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>,<span style="color: #009900;">//another row means another level</span>
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span>,<span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span>,
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#93;</span>,
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">7</span>,<span style="color: #000000; font-weight:bold;">6</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span>,
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000;">&#93;</span>
			  <span style="color: #000000;">&#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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 5</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-5/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-5/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:05:30 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1447</guid>
		<description><![CDATA[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 main source file. Find where this code is: enemyArray = &#91;//defining the array &#91;1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1&#93;,//1's will just represent an [...]]]></description>
			<content:encoded><![CDATA[<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="actionscript3" style="font-family:monospace;">enemyArray = <span style="color: #000000;">&#91;</span><span style="color: #009900;">//defining the array</span>
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>,<span style="color: #009900;">//1's will just represent an enemy to be created</span>
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>,<span style="color: #009900;">//another row means another level</span>
			<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>
			  <span style="color: #000000;">&#93;</span>;</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">_root.enemiesLeft <span style="color: #000000; font-weight: bold;">--</span>;</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>enemiesLeft==<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if there are no more enemies left</span>
	currentLvl <span style="color: #000000; font-weight: bold;">++</span>;<span style="color: #009900;">//continue to the next level</span>
	currentEnemy = <span style="color: #000000; font-weight:bold;">0</span>;<span style="color: #009900;">//reset the amount of enemies there are</span>
	startGame<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//restart the game</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> money<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">100</span>;<span style="color: #009900;">//how much money the player has to spend on turrets</span>
<span style="color: #6699cc; font-weight: bold;">var</span> lives<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">20</span>;<span style="color: #009900;">//how many lives the player has</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//checking what direction it goes when finishing the path</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'UP'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it finishes at the top</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if the y value is too high</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//then remove this guy from the field</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'RIGHT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//and so on for other directions</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'DOWN'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'LEFT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//checking what direction it goes when finishing the path</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'UP'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it finishes at the top</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if the y value is too high</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//then remove this guy from the field</span>
		_root.lives <span style="color: #000000; font-weight: bold;">--</span>;<span style="color: #009900;">//take away a life</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'RIGHT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//and so on for other directions</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		_root.lives <span style="color: #000000; font-weight: bold;">--</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'DOWN'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		_root.lives <span style="color: #000000; font-weight: bold;">--</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'LEFT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		_root.lives <span style="color: #000000; font-weight: bold;">--</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> thisClick<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.money <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if the player has enough money</span>
		_root.makeTurret<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span>,<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//make the turret</span>
		<span style="color: #009900;">//remove all the listeners so it can't be clicked on again</span>
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x333333<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">25</span>,<span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_OVER</span>, thisMouseOver<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_OUT</span>, thisMouseOut<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, thisClick<span style="color: #000000;">&#41;</span>;
&nbsp;
		_root.money <span style="color: #000000; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">20</span>; <span style="color: #009900;">//spend the money</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Updating the text fields</span>
txtLevel.<span style="color: #004993;">text</span> = <span style="color: #990000;">'Level '</span><span style="color: #000000; font-weight: bold;">+</span>currentLvl;
txtMoney.<span style="color: #004993;">text</span> = <span style="color: #990000;">'$'</span><span style="color: #000000; font-weight: bold;">+</span>money;
txtLives.<span style="color: #004993;">text</span> = <span style="color: #990000;">'Lives: '</span><span style="color: #000000; font-weight: bold;">+</span>lives;
txtEnemiesLeft.<span style="color: #004993;">text</span> = <span style="color: #990000;">'Enemies Left:  '</span><span style="color: #000000; font-weight: bold;">+</span>enemiesLeft;</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, restartGame<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//adding a mouse event listener for clicking the stage</span>
<span style="color: #339966; font-weight: bold;">function</span> restartGame<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//go to the first frame</span>
	<span style="color: #004993;">stage</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, restartGame<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//remove the listener</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, restartGame<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//adding a mouse event listener for clicking the stage</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//if there aren't any levels left</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>currentLvl <span style="color: #000000; font-weight: bold;">&gt;</span> enemyArray.<span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	gameOver=<span style="color: #0033ff; font-weight: bold;">true</span>;<span style="color: #009900;">//set the game to be over</span>
&nbsp;
	<span style="color: #009900;">//reset all the stats</span>
	currentLvl = <span style="color: #000000; font-weight:bold;">1</span>;
	currentEnemy = <span style="color: #000000; font-weight:bold;">0</span>;
	enemyTime = <span style="color: #000000; font-weight:bold;">0</span>;
	enemyLimit = <span style="color: #000000; font-weight:bold;">12</span>;
	enemiesLeft = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
	<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, eFrame<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//remove this listener</span>
	<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>roadHolder<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//remove the pieces of road</span>
	<span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'win'</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//go to the win frame</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>lives<span style="color: #000000; font-weight: bold;">&lt;</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if the user runs out of lives</span>
	gameOver=<span style="color: #0033ff; font-weight: bold;">true</span>;<span style="color: #009900;">//set the game to be over</span>
&nbsp;
	<span style="color: #009900;">//reset all the stats</span>
	currentLvl = <span style="color: #000000; font-weight:bold;">1</span>;
	currentEnemy = <span style="color: #000000; font-weight:bold;">0</span>;
	enemyTime = <span style="color: #000000; font-weight:bold;">0</span>;
	enemyLimit = <span style="color: #000000; font-weight:bold;">12</span>;
	enemiesLeft = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
	<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, eFrame<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//remove this listener</span>
	<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>roadHolder<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//remove the pieces of road</span>
	<span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'lose'</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//go to the lose frame</span>
<span style="color: #000000;">&#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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 4</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-4/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-4/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:04:23 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1439</guid>
		<description><![CDATA[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. Just add this code to &#8220;Enemy.as&#8221; where we define all of the variables: public var health:int = 5; Next, we&#8217;re going to have to create a new [...]]]></description>
			<content:encoded><![CDATA[<p>Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we?</p>
<p>First, we&#8217;ll define a single <tt>health</tt> variable for the Enemy. Just add this code to &#8220;Enemy.as&#8221; where we define all of the variables:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> health<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">5</span>;</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Bullet extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _root<span style="color: #000000; font-weight: bold;">:*</span>;
		<span style="color: #009900;">//these two variables below must be set to public so that we can edit them outside the class</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">target</span>;<span style="color: #009900;">//the target that this guy is moving towards</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> damage<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//how much damage this guy inflicts on the enemy</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> xSpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//how fast it's moving horizontally</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> ySpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//how fast it's moving vertically</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> maxSpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span>;<span style="color: #009900;">//how fast it can go</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Bullet<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED</span>,beginClass<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//this will run every time this guy is made</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>,eFrame<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//this will run every frame</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> beginClass<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			_root = <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//setting the root</span>
&nbsp;
			<span style="color: #009900;">//drawing this guy (it'll be a small white circle)</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0xFFFFFF<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> eFrame<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> yDist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">target</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">12.5</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span>;<span style="color: #009900;">//how far this guy is from the enemy (x)</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> xDist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">target</span>.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">12.5</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span>;<span style="color: #009900;">//how far it is from the enemy (y)</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>yDist,xDist<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//the angle that it must move</span>
			ySpeed=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> maxSpeed;<span style="color: #009900;">//calculate how much it should move the enemy vertically</span>
			xSpeed=<span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> maxSpeed;<span style="color: #009900;">//calculate how much it should move the enemy horizontally</span>
			<span style="color: #009900;">//move the bullet towards the enemy</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>= xSpeed;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span>= ySpeed;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">hitTestObject</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it touches the enemy</span>
				<span style="color: #004993;">target</span>.health <span style="color: #000000; font-weight: bold;">-</span>= damage;<span style="color: #009900;">//make it lose some health</span>
				destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//and destroy this guy</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span> == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000; font-weight: bold;">||</span> _root.gameOver == <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//destroy it if game is over</span>
				destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//this function will just remove this guy from the stage</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, eFrame<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The next thing we need to do is define some variables for the Turret. Open up &#8220;Turret.as&#8221; and add in the following code where we define the variables:</p>

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

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

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

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//remove this from stage when game is over</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.gameOver<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 3</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-3/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-3/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:03:57 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1394</guid>
		<description><![CDATA[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 us begin by first creating an Enemy class. Do this by creating a new external ActionScript file saved as &#8220;Enemy.as&#8221; and adding the following code to [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome back. In this part of the tutorial, we are going to add enemies to the field and we&#8217;re going to program them to move through the paths.</p>
<p>Let us begin by first creating an <tt>Enemy</tt> class. Do this by creating a new external ActionScript file saved as &#8220;Enemy.as&#8221; and adding the following code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span><span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">//imports</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #009900;">//defining the class</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Enemy extends <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _root<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> xSpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//how fast it's going horizontally</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> ySpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//how fast it's going vertically</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> maxSpeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">3</span>;<span style="color: #009900;">//how fast it can possibly go</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Enemy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED</span>, beginClass<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, eFrameEvents<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> beginClass<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			_root = <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//defining the root</span>
&nbsp;
			<span style="color: #009900;">//checking what the start direction is</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'UP'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it's starting up</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">300</span>;<span style="color: #009900;">//set the y value off the field</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> = _root.startCoord;<span style="color: #009900;">//make the x value where it should be</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.xSpeed = <span style="color: #000000; font-weight:bold;">0</span>;<span style="color: #009900;">//make it not move horizontally</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.ySpeed = <span style="color: #000000; font-weight: bold;">-</span>maxSpeed;<span style="color: #009900;">//make it move upwards</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'RIGHT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//and so on for other directions</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">25</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> = _root.startCoord;
				<span style="color: #0033ff; font-weight: bold;">this</span>.xSpeed = maxSpeed;
				<span style="color: #0033ff; font-weight: bold;">this</span>.ySpeed = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'DOWN'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">25</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> = _root.startCoord;
				<span style="color: #0033ff; font-weight: bold;">this</span>.xSpeed = <span style="color: #000000; font-weight:bold;">0</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span>.ySpeed = maxSpeed;
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'LEFT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">550</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> = _root.startCoord;
				<span style="color: #0033ff; font-weight: bold;">this</span>.xSpeed = <span style="color: #000000; font-weight: bold;">-</span>maxSpeed;
				<span style="color: #0033ff; font-weight: bold;">this</span>.ySpeed = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">//draw the actual enemy, it's just a red ball</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0xFF0000<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">12.5</span>,<span style="color: #000000; font-weight:bold;">12.5</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> eFrameEvents<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//move it based on x and y value</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span>= xSpeed;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span>= ySpeed;
&nbsp;
			<span style="color: #009900;">//checking what direction it goes when finishing the path</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'UP'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if it finishes at the top</span>
				<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if the y value is too high</span>
					destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//then remove this guy from the field</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'RIGHT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//and so on for other directions</span>
				<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
					destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.finDir == <span style="color: #990000;">'DOWN'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
					destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.startDir == <span style="color: #990000;">'LEFT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
					destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">//remove this from stage when game is over</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_root.gameOver<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> destroyThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//this function will make it easier to remove this from stage</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, eFrameEvents<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span>.<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now that&#8217;s a lot of code. The next thing we have to do is to add a function in the main &#8220;source.fla&#8221; that will add these guys to the field. But, let&#8217;s first create a bunch of variables that&#8217;ll help with the process. It&#8217;ll be simple. Just define them variable where all others are defined:</p>

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

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

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

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> enemyHolder<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>enemyHolder<span style="color: #000000;">&#41;</span>;</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>;i<span style="color: #000000; font-weight: bold;">&lt;</span>enemyArray<span style="color: #000000;">&#91;</span>currentLvl<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #004993;">length</span>;i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>enemyArray<span style="color: #000000;">&#91;</span>currentLvl<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> == <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		enemiesLeft <span style="color: #000000; font-weight: bold;">++</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'START'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'START'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if this is a start block</span>
		<span style="color: #009900;">//then define the startDir and StartCoord based on it's coordinates</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.startDir = <span style="color: #990000;">'RIGHT'</span>;
			_root.startCoord = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.startDir = <span style="color: #990000;">'DOWN'</span>;
			_root.startCoord = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> == <span style="color: #000000; font-weight:bold;">525</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.startDir = <span style="color: #990000;">'LEFT'</span>;
			_root.startCoord = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> == <span style="color: #000000; font-weight:bold;">275</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.startDir = <span style="color: #990000;">'UP'</span>;
			_root.startCoord = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//this level won't work if not any of these values</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'FINISH'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if this is a finish block</span>
		<span style="color: #009900;">//then define the finDir based on it's coordinates</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.finDir = <span style="color: #990000;">'LEFT'</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.finDir = <span style="color: #990000;">'UP'</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> == <span style="color: #000000; font-weight:bold;">525</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.finDir = <span style="color: #990000;">'RIGHT'</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> == <span style="color: #000000; font-weight:bold;">275</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			_root.finDir = <span style="color: #990000;">'DOWN'</span>;
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//this level won't work if not any of these values</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #990000;">'START'</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> directType <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #990000;">'FINISH'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//if this isn't a start of finish block</span>
	<span style="color: #009900;">//then it'll act as a directioning block</span>
	<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>;i<span style="color: #000000; font-weight: bold;">&lt;</span>_root.enemyHolder.<span style="color: #004993;">numChildren</span>;i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//create a loop</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> enTarget = _root.enemyHolder.<span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//this will hold a certain enemy</span>
		<span style="color: #009900;">//if the enTarget's coordinates are too close to this block</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= enTarget.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> enTarget.<span style="color: #004993;">width</span><span style="color: #000000; font-weight: bold;">*</span>.5 <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= enTarget.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> enTarget.<span style="color: #004993;">width</span><span style="color: #000000; font-weight: bold;">*</span>.5
		<span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= enTarget.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> enTarget.<span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">*</span>.5 <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&lt;</span>= enTarget.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> enTarget.<span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">*</span>.5<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//then move the enemy's direction based on what direction this block points to</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'UP'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				enTarget.xSpeed = <span style="color: #000000; font-weight:bold;">0</span>;
				enTarget.ySpeed = <span style="color: #000000; font-weight: bold;">-</span>enTarget.maxSpeed;
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'RIGHT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				enTarget.xSpeed = enTarget.maxSpeed;
				enTarget.ySpeed = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'DOWN'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				enTarget.xSpeed = <span style="color: #000000; font-weight:bold;">0</span>;
				enTarget.ySpeed = enTarget.maxSpeed;
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>directType == <span style="color: #990000;">'LEFT'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				enTarget.xSpeed = enTarget.maxSpeed;
				enTarget.ySpeed = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Phew! That was a lot of code, wasn&#8217;t it? Well, what these snippets of code do is simply direct the enemies to where they should start and where they should go.</p>
<p>That&#8217;s all for this part of the tutorial. Next time, we&#8217;ll make the turret detect and attack the enemies!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 2</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-2/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-2/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:02:42 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1384</guid>
		<description><![CDATA[Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is created. The first step to take in to create a Turret class. You know the drill, create a new ActionScript File, save it as &#8220;Turret.as&#8221;, and type [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is created. The first step to take in to create a <tt>Turret</tt> class. You know the drill, create a new ActionScript File, save it as &#8220;Turret.as&#8221;, and type in the following code:</p>

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

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

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> makeTurret<span style="color: #000000;">&#40;</span>xValue<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>,yValue<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//this will need to be told the x and y values</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> turret<span style="color: #000000; font-weight: bold;">:</span>Turret = <span style="color: #0033ff; font-weight: bold;">new</span> Turret<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//creating a variable to hold the Turret</span>
	<span style="color: #009900;">//changing the coordinates</span>
	turret.<span style="color: #004993;">x</span> = xValue<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">12.5</span>;
	turret.<span style="color: #004993;">y</span> = yValue<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">12.5</span>;
	<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>turret<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//add it to the stage</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

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

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

<p>Now, if you test out the game, a turret should appear whenever you click on any empty block!</p>
<p>Well, that&#8217;s it for this tutorial. Next time, we&#8217;ll add enemies and program them!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Tower Defense Game in AS3 &#8211; Part 1</title>
		<link>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-1/</link>
		<comments>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-1/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 12:01:36 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1373</guid>
		<description><![CDATA[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? In this section of the tutorial, [...]]]></description>
			<content:encoded><![CDATA[<p>The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always very fun to play. I am here to walk you through the creation of one of these games. Let us begin, shall we?</p>
<p>In this section of the tutorial, we&#8217;re going to set up the roads and stuff onto the stage. However, the first thing we need to do is create a blank flash document with a black background with the frames per second set at 24.</p>
<p>Now, we have to create two classes. In order to do this, simply create two external ActionScript files by selecting <strong>File -> New -> ActionScript File</strong>, twice. The first class we&#8217;ll create is one for an empty block that towers can be placed in. Add the following code to it:</p>

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

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

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

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

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

<p>Now, if you test out your game, you&#8217;ll see nice little road set up for you. However, we need to keep that empty space in the bottom so we can add stats and other cool stuff.</p>
<p>Well, that concludes this first part of the tutorial. Next time we&#8217;ll make it so you can add turrets!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/advanced/how-to-create-a-tower-defense-game-in-as3-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 6</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-6/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-6/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:06:48 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=357</guid>
		<description><![CDATA[Well, we&#8217;re almost done with our game, now we just have to add some finishing touches. I won&#8217;t make a menu system like you usually should in a game before releasing it. But, this is just a tutorial, and hopefully you&#8217;ve learned something. You probably already know how to make a menu anyway. Also, I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>Well, we&#8217;re almost done with our game, now we just have to add some finishing touches. I won&#8217;t make a menu system like you usually should in a game before releasing it. But, this is just a tutorial, and hopefully you&#8217;ve learned something. You probably already know how to make a menu anyway. Also, I&#8217;m not going to teach you how to do some of the things that we need because hopefully you can do it yourself. If you can&#8217;t , there&#8217;s always the source file at the bottom.</p>
<p>Now, where were we? Oh yes. I realized that the last lesson, we were supposed to make more levels. I taught a lot of stuff, but I forgot to make more levels for you! I apologize. Anyway, here&#8217;s an example of a 5 level game (Put it on the first frame).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//The array code for lvl 1</span>
<span style="color: #808080; font-style: italic;">//All of the later levels add one more row of bricks</span>
<span style="color: #000000; font-weight: bold;">var</span> lvl1Code:<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: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #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;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl2Code:<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: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #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;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl3Code:<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: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #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;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl4Code:<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: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #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;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl5Code:<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: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #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;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array that contains all of the level codes</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>lvl1Code, lvl2Code, lvl3Code, lvl4Code, lvl5Code<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It&#8217;ll be a pretty straightforward game. Next, I think the gamer needs to know that he has to click the screen to start, and not think the the game is frozen or something. So, just add a dynamic textfield to the middle of the stage. Give it an instance game of <tt>txtStart</tt>, and make sure it isn&#8217;t selectable.<br />
<img src="http://www.flashgametuts.com/obj/tuts/brick-breaker-as3/pt6/text-properties.gif" alt="Make sure that these properties are what you have" /></p>
<p>Next, add this code at the end of the frame.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//setting the text's word</span>
txtStart.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Click To Begin&quot;</span>;</pre></div></div>

<p>You can make the text different, but don&#8217;t be too mean. Next, we have to remove the text, which we can do in the <tt>beginCode()</tt> function. We aren&#8217;t going to remove the textfield itself because we need it again for every level.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//removing the &quot;Click to Start&quot; Text</span>
txtStart.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">''</span>;</pre></div></div>

<p>Now, we have to reset it every level. I&#8217;m not going to tell you how to do it. I hope you&#8217;ve learned enough to be able to.</p>
<p>The next thing we have to do is display the current level, and how many lives the user has. This will be pretty simple as well, so I won&#8217;t really go into details about how to do it. But, I will give you some code to use below all the other.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//creating a function to update the text fields</span>
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, updateTextFields<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>You can create your own text fields with and update them with that function. But, be sure to remove that listener if the gamer loses, or something bad will happen, something very bad&#8230;</p>
<p>And don&#8217;t think I forgot about scoring. Just define a variable at the top called <tt>score</tt> and have it update along with the other stats. Also, you have to make it increase in the <tt>Brick</tt> class every time the brick is hit. You might want to show the score when the player loses too. But, that&#8217;s just optional.</p>
<p>Well, those are all the finishing touches that I&#8217;m going to add on to my game at least. Here is the final product of all our work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 5</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-5/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-5/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:05:38 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=281</guid>
		<description><![CDATA[Now that we&#8217;ve got the basic gameplay down, we can create some levels. Because we&#8217;re making only a simple game, we aren&#8217;t going to make that many. But before we even start making multiple levels, we have to make it possible to win or lose a level. This will be pretty easy. We&#8217;re first going [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we&#8217;ve got the basic gameplay down, we can create some levels. Because we&#8217;re making only a simple game, we aren&#8217;t going to make that many. But before we even start making multiple levels, we have to make it possible to win or lose a level. This will be pretty easy.</p>
<p>We&#8217;re first going make it possible to beat the level. In order for this to happen, we have to track how many bricks are on the stage. Just define the following variable 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> brickAmt:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Now, we have to increment this number every time a brick is placed onto the stage. Type in this code in the Brick.as after defining <tt>_root</tt> in the <tt>beginClass()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//incrementing how many bricks are on the stage</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">brickAmt</span> ++;</pre></div></div>

<p>Next, we have to decrement the number every time a brick is destroyed. Just type in this code in the <tt>hitTestObject</tt> if statement.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//decrementing the amount of bricks on stage</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">brickAmt</span> --;</pre></div></div>

<p>That was pretty easy, right? Now, we have to add a listener that will check if the value of bricks is 0.<br />
You can do so in the <tt>beginCode()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//adding a listener to check if the level is done</span>
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, checkLevel<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Next, we have to define this function. Place this at the end of the code, but before we run <tt>beginCode()</tt></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> checkLevel<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//checking if the bricks are all gone</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>brickAmt == <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;">//reset the level by increasing the level</span>
		currentLvl ++;
		<span style="color: #808080; font-style: italic;">//and re-running makeLvl</span>
		makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When this code runs, nothing will happen when you break all of the bricks because we haven&#8217;t defined more levels. But, there is one thing I want to fix before doing that. The game starts automatically, even if the player isn&#8217;t ready. So, we want to start the level only when the user first clicks on the screen. This is actually easier than you might think.</p>
<p>Remember at the bottom of the code when we ran <tt>beginCode()</tt>? Well, we can just have it run after listening to a mouse click. Here&#8217;s the code.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//if the mouse clicks, then begin the game</span>
<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, beginCode<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Just replace <tt>beginCode();</tt> with that. We also have to change the <tt>beginCode()</tt> function itself just slightly so it will accept a mouse event. When we define the function, just change it to:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//removes the listener for a click</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, beginCode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#91;</span>..<span style="color: #006600;">Code</span>..<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you test the movie, however, it looks a bit weird. The bricks appear only after clicking. This can be easily fixed. Just take the <tt>makeLvl()</tt> out of the <tt>beginCode()</tt> function and put it at the bottom of the code.</p>
<p>The next problem we have to fix is that the level is immediately made after you break all of the bricks, without resetting the ball&#8217;s position or anything (it may not be like that on yours, but trust me, I&#8217;ve tested it). Just place this code in the <tt>CheckLevel()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> checkLevel<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//checking if the bricks are all gone</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>brickAmt == <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;">//reset the level by increasing the level</span>
		currentLvl ++;
		<span style="color: #808080; font-style: italic;">//and re-running makeLvl</span>
		makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//then resetting the ball's and paddle's position</span>
		mcBall.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
		mcBall.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">265</span>;
		mcPaddle.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">230</span>;
		<span style="color: #808080; font-style: italic;">//then removing all of the listeners</span>
		mcPaddle.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
		mcBall.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, checkLevel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//then listening for a mouse click to start the game again</span>
		<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, beginCode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now that we can beat a level, we now have to lose a level. We&#8217;re going to have to add a lives variable at the top first. We&#8217;re also going add a variable that defines if the game is over.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//how many lives you got</span>
<span style="color: #000000; font-weight: bold;">var</span> lives:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">3</span>;
<span style="color: #808080; font-style: italic;">//if the game is over</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>;</pre></div></div>

<p>Then, we have to subtract a life every time the ball hits the floor and do other stuff when the lives are all gone.</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>mcBall.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span>-mcBall.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if the ball hits the bottom</span>
	<span style="color: #808080; font-style: italic;">//then bounce up and lose a life</span>
	ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	lives --;
	<span style="color: #808080; font-style: italic;">//if there aren't any lives left</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;">//the game is over now</span>
		gameOver = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #808080; font-style: italic;">//go to a lose frame</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: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, now we have to create a frame called &#8220;lose&#8221;. I&#8217;m just going to make a frame that that has the text, &#8220;YOU LOSE&#8221;. Make sure to give your frame a label of &#8220;lose&#8221;, or the code won&#8217;t work.</p>
<p>Also, we have to remove the bricks from the stage, because they were added dynamically and won&#8217;t go away if you just change a frame. So, type the following code into the <tt>enterFrameEvents</tt> function in Brick.as.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//checking if the player has lost</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//destroy this brick</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//stop running this code</span>
	removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>For some reason, this code outputs an error. Don&#8217;t worry though, nothing is really wrong.</p>
<p>Now, we have to make the player be able to restart the game after losing. This will be easy. Just add a listener to the stage that will reset the game if the stage is clicked. This code should be in the &#8220;lose&#8221; frame.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//The lose frame</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//resetting the game if the mouse is clicked</span>
<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, resetGame<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> resetGame<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//removing this listener</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, resetGame<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//resetting the game</span>
	<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all for this lesson. The next one will be the final one, where we add some finishing touches, and fix some bugs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 4</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-4/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-4/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:04:17 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=260</guid>
		<description><![CDATA[Now that we&#8217;ve actually made the bricks, we can now break them with our ball&#8230; Anyway, this will be pretty easy to accomplish. But, if you are new to ActionScript 3.0 classes, it&#8217;ll be a pretty cool learning experience. First of all, we&#8217;re going to have to create a class file for the bricks. This [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we&#8217;ve actually made the bricks, we can now break them with our ball&#8230; Anyway, this will be pretty easy to accomplish. But, if you are new to ActionScript 3.0 classes, it&#8217;ll be a pretty cool learning experience.</p>
<p>First of all, we&#8217;re going to have to create a class file for the bricks. This is easy, just go to <strong>File->New</strong> and select &#8220;ActionScript File&#8221;.<br />
<img src="http://www.flashgametuts.com/obj/tuts/brick-breaker-as3/pt4/new-actionscript.gif" alt="Create a new ActionScript file" /></p>
<p>Once you&#8217;ve created the file, immediately save it as &#8220;Brick.as&#8221; in the same folder as your .fla file. Then, type the following code in:</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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Classes must always be wrapped in a package</span>
package <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//importing display elements that we can use in this class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #808080; font-style: italic;">//importing flash events that we can use (like ENTER_FRAME)</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//defining the class name and saying that it</span>
	<span style="color: #808080; font-style: italic;">//extends the MovieClip class, meaning that it has the same</span>
	<span style="color: #808080; font-style: italic;">//properties as a movieclip</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Brick <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//all classes must have a function that runs every time</span>
		<span style="color: #808080; font-style: italic;">//an instance of the class is put on stage</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Brick<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is a skeleton of what most ActionScript classes will look like. Although making a class is not necessary for this small game, it will definitely help if you want to expand your game. In order the use the class file in your game, you must import it first, so add this code to the first line of the first frame of your main flash file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//IMPORTS</span>
<span style="color: #0066CC;">import</span> Brick;</pre></td></tr></table></div>

<p>We also have to change our previous brick MovieClip, <tt>mcBrick</tt>, to the class, Brick. This way, all of the code we place into Brick.as will be used in the Brick MovieClip.</p>
<p>So, right click the mcBrick MovieClip in your library and click on linkage. This window will pop up.<br />
<img src="http://www.flashgametuts.com/obj/tuts/brick-breaker-as3/pt4/linkage-window.gif" alt="The Linkage Window" /></p>
<p>Now, find where it says &#8220;Class&#8221; (it&#8217;s going to be highlighted), and set it from &#8220;mcBrick&#8221; to &#8220;Brick&#8221;. Now, we have a class. Of course, now we&#8217;re going to have to change the <tt>makeLvl()</tt> function slightly. Now we don&#8217;t add &#8220;mcBrick&#8221; to the stage, but just &#8220;Brick&#8221;. Just change the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> brick:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> mcBrick<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>to:</p>

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

<p>If you test it out, it will work out exactly the same as before. Now, we add code to &#8220;Brick.as&#8221;. Let&#8217;s go.</p>
<p>We can&#8217;t access the root level of the document through the <tt>Brick()</tt> function, so we&#8217;re going to add two listeners to it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Brick<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;">//Code that will be run when the brick is added to the stage</span>
	addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//Enter frame code</span>
	addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, we define those two functions.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//private function are just functions that you can't access</span>
<span style="color: #808080; font-style: italic;">//from the main timeline, but only within the class itself</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameEvents<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In order to access the main timeline, we have to type in <tt>MovieClip(root)</tt>. But, this is quite a bit of typing, so we can shorten it by putting it into a variable. Because I&#8217;m guessing that most of you have used ActionScript 2.0 before, I&#8217;m going to call this variable <tt>_root</tt>. Because this variable is non-existent in AS3, we can define it as anything. In this case, it actually will be the root of the document. Place this code after the class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Brick <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//The main timeline!</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
<span style="color: #808080; font-style: italic;">//etc etc etc</span></pre></div></div>

<p>Then, we can define the variable within the <tt>beginClass()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//defining _root as the document root</span>
<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, we can easily access the main timeline within the class. Go on, test it out. Make a trace statement or two.</p>
<p>Now, let&#8217;s make some hit testing, eh?</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> enterFrameEvents<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//hit testing with the ball</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcBall</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;">//making the ball bounce off vertically</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">ballYSpeed</span> <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
		<span style="color: #808080; font-style: italic;">//destroying this brick</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//stop running this code</span>
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This will make the ball bounce off and destroy the brick every time it touches it. Pretty cool, right? Well, that&#8217;s all for this part of the tutorial. Next, we&#8217;re going to make more levels and add a win/lose system!</p>
<h4>Final Code for Main Flash File Frame 1:</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//IMPORTS</span>
<span style="color: #0066CC;">import</span> Brick;
<span style="color: #808080; font-style: italic;">//Current level player is on</span>
<span style="color: #000000; font-weight: bold;">var</span> currentLvl:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #808080; font-style: italic;">//The array code for lvl 1</span>
<span style="color: #000000; font-weight: bold;">var</span> lvl1Code:<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: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #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;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array that contains all of the level codes</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>lvl1Code<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h4>Final Code for Main Flash File Frame 2:</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//VARIABLES</span>
<span style="color: #808080; font-style: italic;">//These variables are needed for moving the ball</span>
<span style="color: #000000; font-weight: bold;">var</span> ballXSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">8</span>; <span style="color: #808080; font-style: italic;">//X Speed of the Ball</span>
<span style="color: #000000; font-weight: bold;">var</span> ballYSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">8</span>; <span style="color: #808080; font-style: italic;">//Y Speed of the Ball</span>
<span style="color: #808080; font-style: italic;">//First I defined a function where all of</span>
<span style="color: #808080; font-style: italic;">//the code needed to start the game is placed</span>
<span style="color: #808080; font-style: italic;">//This includes listeners, variable definitions, and other stuff</span>
<span style="color: #000000; font-weight: bold;">function</span> beginCode<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;">//Adds a listener to the paddle which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcPaddle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//Adds a listener to the ball which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcBall.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//making the level</span>
	makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> movePaddle<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #006600;">x</span> = mouseX - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
	<span style="color: #808080; font-style: italic;">//Keeping the paddle in the stage</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the left</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mouseX <span style="color: #66cc66;">&lt;</span> mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Keep the paddle on stage</span>
		mcPaddle.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the right</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mouseX <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Keep the paddle on stage</span>
		mcPaddle.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mcPaddle.<span style="color: #0066CC;">width</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> moveBall<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Code for moving ball goes here</span>
	mcBall.<span style="color: #006600;">x</span> += ballXSpeed; <span style="color: #808080; font-style: italic;">//Move the ball horizontally</span>
	mcBall.<span style="color: #006600;">y</span> += ballYSpeed; <span style="color: #808080; font-style: italic;">//Move the ball vertically</span>
	<span style="color: #808080; font-style: italic;">//Bouncing the ball off of the walls</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span>-mcBall.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the ball hits the right side</span>
		<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
		ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the ball hits the left side</span>
		<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
		ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span>-mcBall.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the ball hits the bottom</span>
		<span style="color: #808080; font-style: italic;">//then bounce up</span>
		ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">y</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: #808080; font-style: italic;">//if the ball hits the top</span>
		<span style="color: #808080; font-style: italic;">//then bounce down</span>
		ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">//Hitting the paddle</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span>mcPaddle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		calcBallAngle<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: #000000; font-weight: bold;">function</span> calcBallAngle<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;">//ballPosition is the position of the ball is on the paddle</span>
	<span style="color: #000000; font-weight: bold;">var</span> ballPosition:<span style="color: #0066CC;">Number</span> = mcBall.<span style="color: #006600;">x</span> - mcPaddle.<span style="color: #006600;">x</span>;
	<span style="color: #808080; font-style: italic;">//hitPercent converts ballPosition into a percent</span>
	<span style="color: #808080; font-style: italic;">//All the way to the left is -.5</span>
	<span style="color: #808080; font-style: italic;">//All the way to the right is .5</span>
	<span style="color: #808080; font-style: italic;">//The center is 0</span>
	<span style="color: #000000; font-weight: bold;">var</span> hitPercent:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>ballPosition <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span>mcPaddle.<span style="color: #0066CC;">width</span> - mcBall.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> - .5;
	<span style="color: #808080; font-style: italic;">//Gets the hitPercent and makes it a larger number so the</span>
	<span style="color: #808080; font-style: italic;">//ball actually bounces</span>
	ballXSpeed = hitPercent <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">10</span>;
	<span style="color: #808080; font-style: italic;">//Making the ball bounce back up</span>
	ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> makeLvl<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;">//Places bricks onto Level</span>
	<span style="color: #808080; font-style: italic;">//finding the array length of the lvl code</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//The index has to be currentLvl-1 because:</span>
	<span style="color: #808080; font-style: italic;">//array indexes start on 0 and our lvl starts at 1</span>
	<span style="color: #808080; font-style: italic;">//our level will always be 1 higher than the actual index of the array</span>
	<span style="color: #000000; font-weight: bold;">var</span> arrayLength:<span style="color: #0066CC;">int</span> = lvlArray<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>;
	<span style="color: #808080; font-style: italic;">//the current row of bricks we are creating</span>
	<span style="color: #000000; font-weight: bold;">var</span> brickRow:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #808080; font-style: italic;">//Now, creating a loop which places the bricks onto the stage</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>arrayLength;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//checking if it should place a brick there</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//creating a variable which holds the brick instance</span>
			<span style="color: #000000; font-weight: bold;">var</span> brick:Brick = <span style="color: #000000; font-weight: bold;">new</span> Brick<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//setting the brick's coordinates via the i variable and brickRow</span>
			brick.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">15</span>+<span style="color: #66cc66;">&#40;</span>i-brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">75</span>;
			brick.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>+brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">20</span>;
			<span style="color: #808080; font-style: italic;">//checks if the current brick needs a new row</span>
			<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>;c<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">10</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;">7</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					brickRow ++;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">//finally, add the brick to stage</span>
			addChild<span style="color: #66cc66;">&#40;</span>brick<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h4>Final Code for Brick.as</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Classes must always be wrapped in a package</span>
package <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//importing display elements that we can use in this class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//defining the class name and saying that it</span>
	<span style="color: #808080; font-style: italic;">//extends the MovieClip class, meaning that it has the same</span>
	<span style="color: #808080; font-style: italic;">//properties as a movieclip</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Brick <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//The main timeline!</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
		<span style="color: #808080; font-style: italic;">//all classes must have a function that runs every time</span>
		<span style="color: #808080; font-style: italic;">//an instance of the class is put on stage</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Brick<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;">//Code that will be run when the brick is added to the stage</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Enter frame code</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//private function are just functions that you can't access</span>
		<span style="color: #808080; font-style: italic;">//from the main timeline, but only within the class itself</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//defining _root as the document root</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameEvents<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//hit testing with the ball</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcBall</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;">//making the ball bounce off vertically</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">ballYSpeed</span> <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
				<span style="color: #808080; font-style: italic;">//destroying this brick</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">//stop running this code</span>
				removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
