<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mr Sun Studios</title>
	<atom:link href="http://www.mrsunstudios.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mrsunstudios.com/blog</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sun, 29 May 2022 12:23:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0</generator>
	<item>
		<title>New Site for Tutorials!</title>
		<link>http://www.mrsunstudios.com/blog/flash/new-site-for-tutorials/</link>
					<comments>http://www.mrsunstudios.com/blog/flash/new-site-for-tutorials/#comments</comments>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Tue, 28 Jul 2009 15:03:18 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1552</guid>

					<description><![CDATA[Hey guys. I&#8217;m sorry for my lack of posts for the past few months. I&#8217;ve been extremely lazy busy all year. But, I have some news that might cheer you up. I am happy to announce that I&#8217;ve created a new site solely for the purpose of posting my flash tutorials. The site is called [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Hey guys. I&#8217;m sorry for my lack of posts for the past few months. I&#8217;ve been extremely <strike>lazy</strike> busy all year. But, I have some news that might cheer you up. I am happy to announce that I&#8217;ve created a new site solely for the purpose of posting my flash tutorials. The site is called <a href="http://www.flashgametuts.com">Flash Game Tuts</a>. I&#8217;ve posted all of the tutorials from this site over there so you can check them out! I&#8217;ll keep all of the tutorials on Mr Sun Studios just in case you still need them, however. But, I&#8217;m planning to release a few more tutorials only on Flash Game Tuts, so stay tuned.</p>
<p>Also, I need some ideas for my next set of tutorials! Feel free to post here some suggestions of types of games you want to make. I&#8217;d greatly appreciate it!</p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/new-site-for-tutorials/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 7</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-7/</link>
					<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-7/#comments</comments>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:07:48 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1534</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 7: Finishing Touches Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 7: Finishing Touches</h3>
<p>Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I won&#8217;t comment any of the code I give you, as the knowledge you have learned from this tutorial should tell you what&#8217;s going on. Luckily for you, there aren&#8217;t too many finishing touches for us to make, so your brain won&#8217;t hurt too much.</p>
<p>Now, what can we add to our little game? The answer is: it&#8217;s up to you to decide what to add. It&#8217;s also up to you to use what you&#8217;ve learned to do it right. Thank you and good night.</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt7/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt7/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt7/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-7/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-6/</link>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:06:07 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1531</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 6: Expanding on the Game Welcome to the 6th part of the tutorial, Expanding on the Game! Well, what do I mean by &#8220;Expanding&#8221;? Well, by expanding, I mean that we&#8217;re [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 6: Expanding on the Game</h3>
<p>Welcome to the 6th part of the tutorial, Expanding on the Game! Well, what do I mean by &#8220;Expanding&#8221;? Well, by expanding, I mean that we&#8217;re going to create more enemies and more levels. Sounds pretty cool, doesn&#8217;t it?</p>
<p>Open up &#8220;source.fla&#8221; and find the <tt>makeEnemies</tt> function. We&#8217;re going to have some major renovations to this functions. Just replace the function with this new code:</p>
<pre lang="actionscript">
function makeEnemies():Void{//this function will add enemies to the field
	if(enemyTime < enemyLimit){//if it isn't time to make them yet
		enemyTime ++;//then keep on waiting
	} else {//otherwise
		var theCode:Number = enemyArray[currentLvl-1][currentEnemy];//get the code from the array
		if(theCode != 0 &#038;&#038; theCode != null){//if it's not set at 0
			//then create a new enemy and add it to the enemy holder
			enemyHolder.createEmptyMovieClip('enemy'+currentEnemy,enemyHolder.getNextHighestDepth());
			//now we're going to draw the enemy. It'll just be a tiny red circle
			enemyHolder['enemy'+currentEnemy].beginFill(0xFF0000);//coloring them red gray
			enemyHolder['enemy'+currentEnemy].moveTo(0, 2.5);//move the entire shape a certain way
			//create 4 curves so that it'll look like a circle
			enemyHolder['enemy'+currentEnemy].curveTo(0,10,5,10);
			enemyHolder['enemy'+currentEnemy].curveTo(10,10,10,5);
			enemyHolder['enemy'+currentEnemy].curveTo(10,0,5,0);
			enemyHolder['enemy'+currentEnemy].curveTo(0,0,0,5);
			enemyHolder['enemy'+currentEnemy].endFill();//end the fill
			
			//add a few variables to the enemy
			enemyHolder['enemy'+currentEnemy].enLevel = theCode;//setting its level to be what # it is
			enemyHolder['enemy'+currentEnemy].maxSpeed = 3;//how fast it can possibly go
			enemyHolder['enemy'+currentEnemy].xSpeed = 0;
			enemyHolder['enemy'+currentEnemy].ySpeed = 0;
			enemyHolder['enemy'+currentEnemy].health = 5*theCode;
			
			//checking what the start direction is
			if(_root.startDir == 'UP'){//if it's starting up
				enemyHolder['enemy'+currentEnemy]._y = 300;//set the y value off the field
				enemyHolder['enemy'+currentEnemy]._x = _root.startCoord;//make the x value where it should be
				enemyHolder['enemy'+currentEnemy].xSpeed = 0;//make it not move horizontally
				enemyHolder['enemy'+currentEnemy].ySpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;//make it move upwards
			} else if(_root.startDir == 'RIGHT'){//and so on for other directions
				enemyHolder['enemy'+currentEnemy]._x = -25;
				enemyHolder['enemy'+currentEnemy]._y = _root.startCoord;
				enemyHolder['enemy'+currentEnemy].xSpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;
				enemyHolder['enemy'+currentEnemy].ySpeed = 0;
			} else if(_root.startDir == 'DOWN'){
				enemyHolder['enemy'+currentEnemy]._y = -25;
				enemyHolder['enemy'+currentEnemy]._x = _root.startCoord;
				enemyHolder['enemy'+currentEnemy].xSpeed = 0;
				enemyHolder['enemy'+currentEnemy].ySpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;
			} else if(_root.startDir == 'LEFT'){
				enemyHolder['enemy'+currentEnemy]._x = 550;
				enemyHolder['enemy'+currentEnemy]._y = _root.startCoord;
				enemyHolder['enemy'+currentEnemy].xSpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;
				enemyHolder['enemy'+currentEnemy].ySpeed = 0;
			}
			
			enemyHolder['enemy'+currentEnemy]._x += 5;//fixing the x value
			enemyHolder['enemy'+currentEnemy]._y += 5;//fixing up the y value
			
			enemyHolder['enemy'+currentEnemy].onEnterFrame = function(){//give it some functions
				this._x += this.xSpeed;
				this._y += this.ySpeed;
				
				//checking what direction it goes when finishing the path
				if(_root.finDir == 'UP'){//if it finishes at the top
					if(this._y <= -25){//if the y value is too high
						_root.lives --;//take away a life
						_root.money -= 5*this.enLevel;//don't let the player gain any money
						this.removeMovieClip();//take it away from the stage
					}
				} else if(_root.finDir == 'RIGHT'){//and so on for other directions
					if(this._x >= 550){
						_root.lives --;
						_root.money -= 5*this.enLevel;
						this.removeMovieClip();
					}
				} else if(_root.finDir == 'DOWN'){
					if(this._y >= 300){
						_root.lives --;
						_root.money -= 5*this.enLevel;
						this.removeMovieClip();
					}
				} else if(_root.startDir == 'LEFT'){
					if(this._x <= 0){
						_root.lives --;
						_root.money -= 5*this.enLevel;
						this.removeMovieClip();
					}
				}
				
				if(this.health <= 0){
					_root.enemiesLeft --;
					_root.money += 5*this.enLevel;
					this.removeMovieClip();
				}
			}
		}
		currentEnemy ++;//move on to the next enemy
		enemyTime = 0;//and reset the time
	}
}
</pre>
<p>There aren't very many renovations that we've made, but they will make our code much more flexible. It allows us to create different enemy levels by setting the <tt>enLevel</tt> to be equal to the code that is placed into the enemy array. The <tt>enLevel</tt> in turn lets us dynamically change the amount of health the enemy has and the amount of money that it gives you when you kill it. Right now the health and money it gives you is 5 times the enemy level.</p>
<p>Now, we can make more levels with better enemies! You can customize your own levels, or use the ones I created by setting these values in the <tt>enemyArray</tt>:</p>
<pre lang="actionscript">
enemyArray = [//defining the array
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//1's will just represent an enemy to be created
			[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],//another row means another level
			[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],
			[100],
			[5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5],
			[250,250,250]
			  ];
</pre>
<p>Of course, I'd suggest creating your own levels, as mine aren't what you would call the best. Anyways, this wraps up the second to last part of this tutorial. Join us next time when finish up this little game!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt6/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt6/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 5</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-5/</link>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:05:40 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1519</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 5: Winning/Losing the Game Welcome back to the 5th installment of this tutorial series. In this lesson, we&#8217;ll make some playable levels, along with a winning and losing situation. Let&#8217;s dig [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 5: Winning/Losing the Game</h3>
<p>Welcome back to the 5th installment of this tutorial series. In this lesson, we&#8217;ll make some playable levels, along with a winning and losing situation. Let&#8217;s dig in, shall we?</p>
<p>Lets start off by opening up the main source file. Find where this code is:</p>
<pre lang="actionscript">
enemyArray = [//defining the array
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//1's will just represent an enemy to be created
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//another row means another level
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
			  ];
</pre>
<p>Let&#8217;s review this code for a bit. Each of those arrays within that <tt>enemyArray</tt> represents a level. So, right now, we&#8217;re set up for 3 different levels, with an increasing amount of enemies in each level. Now that we&#8217;ve covered this, we can continue on to making it possible to advance levels.</p>
<p>In order to do this, we must first count the number of enemies we actually have on stage. This is actually very easy to do. Find the <tt>startGame()</tt> function (~line 45) that has no code in it, and add the following:</p>
<pre lang="actionscript">
enemiesLeft = enemyArray[currentLvl-1].length;
</pre>
<p>Now, we have to decrease this number every time an enemy is destroyed. We can do this by adding one line of code to where we remove the Enemy from the stage in the <tt>makeEnemies()</tt> function. Add the following code (~line 346):</p>
<pre lang="actionscript">
_root.enemiesLeft --;
</pre>
<p>Now, find the main <tt>_root.onEnterFrame()</tt> function (~line 287). There should only be one line of code in there right now. We&#8217;re going to add some. Add the following code to the bottom of it:</p>
<pre lang="actionscript">
if(enemiesLeft==0){//if there are no more enemies left
	currentLvl ++;//continue to the next level
	currentEnemy = 0;//reset the amount of enemies there are
	startGame();//restart the game
}
</pre>
<p>Now, try testing out the game. Functionally, it&#8217;s working 100%. Practically, it&#8217;s not a great game. In order to make this game better, we&#8217;re going to have to show some information to the user while they&#8217;re playing, like the current level, the score, etc. This is exactly what we&#8217;re going to do now.</p>
<p>Now, try testing out the game. Functionally, it&#8217;s working 100%. Practically, it&#8217;s not a great game. In order to make this game better, we&#8217;re going to have to show some information to the user while they&#8217;re playing, like the current level, the score, etc. This is exactly what we&#8217;re going to do now.</p>
<p>Create four dynamic text boxes at the bottom left portion of the stage, each at 12 point font. Here&#8217;s an example of what yours should look like, with an example of what information we&#8217;re going to put into them later:</p>
<p><center><img loading="lazy" 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>
<pre lang="actionscript">
var money:Number=100;//how much money the player has to spend on turrets
var lives:Number=20;//how many lives the player has
</pre>
<p>Okay, good stuff. Let&#8217;s first make these two variables mean something before we update the text. We first have to make it so the player loses lives so he/she can lose the game. </p>
<p>In order to do this, we must add the following code to the Enemy&#8217;s <tt>onEnterFrame()</tt> function (~line 353):</p>
<pre lang="actionscript">
//checking what direction it goes when finishing the path
if(_root.finDir == 'UP'){//if it finishes at the top
	if(this._y <= -25){//if the y value is too high
		_root.lives --;//take away a life
		_root.money -= 5;//don't let the player gain any money
		this.removeMovieClip();//take it away from the stage
	}
} else if(_root.finDir == 'RIGHT'){//and so on for other directions
	if(this._x >= 550){
		_root.lives --;
		_root.money -= 5;
		this.removeMovieClip();
	}
} else if(_root.finDir == 'DOWN'){
	if(this._y >= 300){
		_root.lives --;
		_root.money -= 5;
		this.removeMovieClip();
	}
} else if(_root.startDir == 'LEFT'){
	if(this._x <= 0){
		_root.lives --;
		_root.money -= 5;
		this.removeMovieClip();
	}
}	
if(this.health <= 0){
	_root.enemiesLeft --;
	_root.money += 5;
	this.removeMovieClip();
}
</pre>
<p>Note that we also are giving the player money for killing the enemies. Next, we'll make each of the turrets cost a certain amount of money. I'm thinking $20 is a good price. Find the block's <tt>onRelease</tt> function (~line 80). We have to wrap all of its contents with <tt>if(_root.money >= 20){money-=20;......}</tt>. This way, the final <tt>onRelease</tt> function will look like this:</p>
<pre lang="actionscript">
_root['block'+i].onRelease = function(){
	if(_root.money >= 20){//if there's enough money
		_root.money -= 20;//spend it and make a turret
		//this function will run when the empty block is clicked on
		
		//change this guy's color back
		var newColor = new Color(this);
		newColor.setRGB(0x333333);
		//set all other mouse functions to null in order to keep it from being clicked again
		this.onRollOver = null;
		this.onRollOut = null;
		this.onRelease = null;
		//create an empty turret movieclip that will be on the top root layer
		_root.createEmptyMovieClip('t'+this._name,_root.getNextHighestDepth());
		
		//drawing the turret, it will have a gray, circular, base with a white gun
		_root['t'+this._name].beginFill(0x999999);//coloring the base light gray
		_root['t'+this._name].moveTo(0, 12.5);//move the entire shape a certain way
		//create 4 curves so that it'll look like a circle
		_root['t'+this._name].curveTo(0,25,12.5,25);
		_root['t'+this._name].curveTo(25,25,25,12.5);
		_root['t'+this._name].curveTo(25,0,12.5,0);
		_root['t'+this._name].curveTo(0,0,0,12.5);
		_root['t'+this._name].endFill();//end the fill so we can make a new one
		
		//creating the gun
		_root['t'+this._name].createEmptyMovieClip('gun',_root['t'+this._name].getNextHighestDepth());
		_root['t'+this._name].gun.beginFill(0xFFFFFF);
		_root['t'+this._name].gun.lineTo(-2,-2);
		_root['t'+this._name].gun.lineTo(2,-2);
		_root['t'+this._name].gun.lineTo(2,15);
		_root['t'+this._name].gun.lineTo(-2,15);
		_root['t'+this._name].gun.lineTo(-2,-2);
		_root['t'+this._name].gun.endFill();
		//setting the gun to be on the center of the turret
		_root['t'+this._name].gun._x = 12.5;
		_root['t'+this._name].gun._y = 12.5;
		//set the turrets coordinates to be the blocks coordinates
		_root['t'+this._name]._x = this._x;
		_root['t'+this._name]._y = this._y;
		
		_root['t'+this._name].angle = 0; //the angle that the turret is currently rotated at
		_root['t'+this._name].radiansToDegrees = 180/Math.PI;//this is needed for the rotation
		_root['t'+this._name].damage = 3;//how much damage this little baby can inflict
		_root['t'+this._name].range = 100;//how far away (in pixels) it can hit a target
		_root['t'+this._name].enTarget = null;//the current target that it's rotating towards
		_root['t'+this._name].cTime = 0;//how much time since a shot was fired by this turret
		_root['t'+this._name].reloadTime = 12;//how long it takes to fire another shot
		_root['t'+this._name].loaded = true;//whether or not this turret can shoot
					
		_root['t'+this._name].onEnterFrame = function(){
			//FINDING THE NEAREST ENEMY WITHIN RANGE
			this.distance = this.range;//let's define a variable which will be how far the nearest enemy is
			this.enTarget = null;//right now, we don't have a target to shoot at
			for(var i=_root.currentEnemy-1;i>=0;i--){//loop through the children in enemyHolder
				var cEnemy = _root.enemyHolder['enemy'+i];//define a movieclip that will hold the current child
				//this simple formula with get us the distance of the current enemy
				if(Math.sqrt(Math.pow(cEnemy._y - this._y, 2) + Math.pow(cEnemy._x - this._x, 2)) < this.distance){
					//if the selected enemy is close enough, then set it as the target
					this.enTarget = cEnemy;
				}
			}
			//ROTATING TOWARDS TARGET
			if(this.enTarget != null){//if we have a defined target
				//turn this baby towards it
				this.gun._rotation = Math.atan2((this.enTarget._y-this._y), this.enTarget._x-this._x)/Math.PI*180-90;
				if(this.loaded){//if the turret is able to shoot
					this.loaded = false;//then make in unable to do it for a bit
					//create a bullet
					_root.createEmptyMovieClip('b'+this._name,_root.getNextHighestDepth());
					//draw the bullet
					_root['b'+this._name].beginFill(0xFFFFFF);
					_root['b'+this._name].lineTo(0,0);
					_root['b'+this._name].lineTo(0,3);
					_root['b'+this._name].lineTo(3,3);
					_root['b'+this._name].lineTo(3,0);
					_root['b'+this._name].endFill();
					//set the bullet's coordinates
					_root['b'+this._name]._x = this._x+12.5;
					_root['b'+this._name]._y = this._y+12.5;
					//set the bullet's target and damage
					_root['b'+this._name].target = this.enTarget;
					_root['b'+this._name].damage = this.damage;
					
					//add some functions to this bullet
					_root['b'+this._name].onEnterFrame = function(){
						this.maxSpeed=8;
						this.yDist=this.target._y+12.5 - this._y;//how far this guy is from the enemy (x)
						this.xDist=this.target._x+12.5 - this._x;//how far it is from the enemy (y)
						this.angle=Math.atan2(this.yDist,this.xDist);//the angle that it must move
						this.ySpeed=Math.sin(this.angle) * this.maxSpeed;//calculate how much it should move the enemy vertically
						this.xSpeed=Math.cos(this.angle) * this.maxSpeed;//calculate how much it should move the enemy horizontally
						//move the bullet towards the enemy
						this._x+= this.xSpeed;
						this._y+= this.ySpeed;
						//check if it is close to the enemy
						if(this._x+this.maxSpeed*2>=this.target._x && this._x-this.maxSpeed*2<=this.target._x
						&#038;&#038; this._y+this.maxSpeed*2>=this.target._y && this._y-this.maxSpeed*2<=this.target._y){
							this.target.health -= this.damage;//make the enemy lose health
							this.removeMovieClip();//remove this sucker
						}
					}
				}
			}
			//LOADING THE TURRET
			if(!this.loaded){//if it isn't loaded
				this.cTime ++;//then continue the time
				if(this.cTime == this.reloadTime){//if time has elapsed for long enough
					this.loaded = true;//load the turret
					this.cTime = 0;//and reset the time
				}
			}
		}
}
</pre>
<p>Now, we can update these text fields. Once again, find the <tt>_root.onEnterFrame()</tt> function (~line 300). Add the following code which will update all the text fields with the needed information:</p>
<pre lang="actionscript">
//Updating the text fields
txtLevel.text = 'Level '+currentLvl;
txtMoney.text = '$'+money;
txtLives.text = 'Lives: '+lives;
txtEnemiesLeft.text = 'Enemies Left:  '+enemiesLeft;
</pre>
<p>Now, let'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's create these two frames, shall we?</p>
<p>Before, we create the frames, we have to add a layer called "labels". This will just let us easily navigate to the required frames. Next, create a frame labeled "win" and add whatever message you want to inform the player that they've won. Do the same with a "lose" frame.</p>
<p>Now, we have to make it possible for the player to restart the game. We'll let them do it by clicking anywhere on the screen. Copy and paste this code to both the "win"  frame and the "lose" frame:</p>
<pre lang="actionscript">
timeElapsed = 0;//how many frames have elapsed since this screen has been shown

_root.onEnterFrame = function(){
	timeElapsed ++;//increase the amount of frames that have elapsed
	if(timeElapsed == 120){//if 5 seconds (@24fps) have elapsed
		gotoAndStop(1);//go back to restart the game
	}
}
</pre>
<p>All right, the next thing we have to do is navigate to the desired frame when the player wins or loses. Go back to frame 1 and find the <tt>_root.onEnterFrame()</tt> function (~line 293). Add the following code to the <strong>very beginning</strong> of it:</p>
<pre lang="actionscript">
//if there aren't any levels left
	if(currentLvl > enemyArray.length){
		gameOver=true;//set the game to be over
		
		//reset all the stats
		currentLvl = 1;
		currentEnemy = 0;
		enemyTime = 0;
		enemyLimit = 12;
		enemiesLeft = 0;
		
		roadHolder.removeMovieClip();//remove the pieces of road
		enemyHolder.removeMovieClip();//remove the enemies
		//remove all of the blocks
		for(i=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array
			if(lvlArray[i] == 0){//if the current index is set to 0 
				_root['block'+i].removeMovieClip();//destroy the empty block
				_root['tblock'+i].removeMovieClip();//destroy the turret
			}
		}
		gotoAndStop('win');//go to the win frame
		_root.onEnterFrame = null//remove this function
	}
	if(lives<=0){//if the user runs out of lives
		gameOver=true;//set the game to be over
		
		//reset all the stats
		currentLvl = 1;
		currentEnemy = 0;
		enemyTime = 0;
		enemyLimit = 12;
		enemiesLeft = 0;
		
		roadHolder.removeMovieClip();//remove the pieces of road
		enemyHolder.removeMovieClip();//remove the enemies
		//remove all of the blocks
		for(i=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array
			if(lvlArray[i] == 0){//if the current index is set to 0 
				_root['block'+i].removeMovieClip();//destroy the empty block
				_root['tblock'+i].removeMovieClip();//destroy the turret
			}
		}
		gotoAndStop('lose');//go to the win frame
		_root.onEnterFrame = null//remove this function
	}
</pre>
<p>Sweet. This concludes this installment of the tutorial series. Join us next time when we expand on the game!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt5/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt5/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt5/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-4/</link>
					<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-4/#comments</comments>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:04:22 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1509</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 4: Making Turrets Attack Enemies Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we? First, we&#8217;ll define a single health variable for [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 4: Making Turrets Attack Enemies</h3>
<p>Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we?</p>
<p>First, we&#8217;ll define a single <tt>health</tt> variable for the Enemy. Find this code in the <tt>makeEnemies()</tt> function (~ Line 236):</p>
<pre lang="actionscript">
//add a few variables to the enemy
enemyHolder['enemy'+currentEnemy].maxSpeed = 3;//how fast it can possibly go
enemyHolder['enemy'+currentEnemy].xSpeed = 0;
enemyHolder['enemy'+currentEnemy].ySpeed = 0;
</pre>
<p>Just add this code:</p>
<pre lang="actionscript">
enemyHolder['enemy'+currentEnemy].health = 5;
</pre>
<p>Now, we&#8217;re going to have to add some variables to the turrets. In the <tt>makeRoad()</tt> function, find the <tt>onRelease</tt> function of the Empty Block (~ Line 77). Add this code to the bottom of that function (~ Line 116).</p>
<pre lang="actionscript">
_root['t'+this._name].angle = 0; //the angle that the turret is currently rotated at
_root['t'+this._name].radiansToDegrees = 180/Math.PI;//this is needed for the rotation
_root['t'+this._name].damage = 3;//how much damage this little baby can inflict
_root['t'+this._name].range = 100;//how far away (in pixels) it can hit a target
_root['t'+this._name].enTarget = null;//the current target that it's rotating towards
_root['t'+this._name].cTime = 0;//how much time since a shot was fired by this turret
_root['t'+this._name].reloadTime = 12;//how long it takes to fire another shot
_root['t'+this._name].loaded = true;//whether or not this turret can shoot
</pre>
<p>Those are a lot of variables, aren&#8217;t they? Well, this is a pretty complex task, so we&#8217;re going to need all of them. So, brace yourself, for we are now going to jump into some code. Add the following after the code you just added above:</p>
<pre lang="actionscript">
_root['t'+this._name].onEnterFrame = function(){
	//FINDING THE NEAREST ENEMY WITHIN RANGE
	this.distance = this.range;//let's define a variable which will be how far the nearest enemy is
	this.enTarget = null;//right now, we don't have a target to shoot at
	for(var i=_root.currentEnemy-1;i>=0;i--){//loop through the children in enemyHolder
		var cEnemy = _root.enemyHolder['enemy'+i];//define a movieclip that will hold the current child
		//this simple formula with get us the distance of the current enemy
		if(Math.sqrt(Math.pow(cEnemy._y - this._y, 2) + Math.pow(cEnemy._x - this._x, 2)) < this.distance){
			//if the selected enemy is close enough, then set it as the target
			this.enTarget = cEnemy;
		}
	}
	//ROTATING TOWARDS TARGET
	if(this.enTarget != null){//if we have a defined target
		//turn this baby towards it
		this.gun._rotation = Math.atan2((this.enTarget._y-this._y), this.enTarget._x-this._x)/Math.PI*180-90;
		if(this.loaded){//if the turret is able to shoot
			//subtract the enemy's health
			this.enTarget.health -= this.damage;
			this.loaded = false;//then make in unable to do it for a bit
			//create a bullet
			_root.createEmptyMovieClip('b'+this._name,_root.getNextHighestDepth());
			//draw the bullet
			_root['b'+this._name].beginFill(0xFFFFFF);
			_root['b'+this._name].lineTo(0,0);
			_root['b'+this._name].lineTo(0,3);
			_root['b'+this._name].lineTo(3,3);
			_root['b'+this._name].lineTo(3,0);
			_root['b'+this._name].endFill();
			//set the bullet's coordinates
			_root['b'+this._name]._x = this._x+12.5;
			_root['b'+this._name]._y = this._y+12.5;
			//set the bullet's target and damage
			_root['b'+this._name].target = this.enTarget;
			
			//add some functions to this bullet
			_root['b'+this._name].onEnterFrame = function(){
				this.maxSpeed=4;
				this.yDist=this.target._y+12.5 - this._y;//how far this guy is from the enemy (x)
				this.xDist=this.target._x+12.5 - this._x;//how far it is from the enemy (y)
				this.angle=Math.atan2(this.yDist,this.xDist);//the angle that it must move
				this.ySpeed=Math.sin(this.angle) * this.maxSpeed;//calculate how much it should move the enemy vertically
				this.xSpeed=Math.cos(this.angle) * this.maxSpeed;//calculate how much it should move the enemy horizontally
				//move the bullet towards the enemy
				this._x+= this.xSpeed;
				this._y+= this.ySpeed;
				//check if it is close to the enemy
				if(this._x+this.maxSpeed*2>=this.target._x && this._x-this.maxSpeed*2<=this.target._x
				&#038;&#038; this._y+this.maxSpeed*2>=this.target._y && this._y-this.maxSpeed*2<=this.target._y){
					this.target.health -= this.damage;//make the enemy lose health
					this.removeMovieClip();//remove this sucker
				}
			}
		}
	}
	//LOADING THE TURRET
	if(!this.loaded){//if it isn't loaded
		this.cTime ++;//then continue the time
		if(this.cTime == this.reloadTime){//if time has elapsed for long enough
			this.loaded = true;//load the turret
			this.cTime = 0;//and reset the time
		}
	}
}
</pre>
<p>Now, if you test out the game and create some turrets, they should start shooting at those darn enemies! Next, we have to make it so the enemy dies when shot. This will be pretty easy to do. Find the code where we added the <tt>onEnterFrame()</tt> code to the enemy and made it move (~line 342). Just add this little tidbit of code:</p>
<pre lang="actionscript">
if(this.health <= 0){
	this.removeMovieClip();
}
</pre>
<p>Pretty hot stuff, ain't it? Well, this concludes the fourth installment of this tutorial. Join us next time when we make levels and have winning and losing scenarios!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt4/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt4/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt4/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-4/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-3/</link>
					<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-3/#comments</comments>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:03:10 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1498</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 3: Adding Enemies Welcome back. In this part of the tutorial, we are going to add enemies to the field and we&#8217;re going to program them to move through the paths. [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 3: Adding Enemies</h3>
<p>Welcome back. In this part of the tutorial, we are going to add enemies to the field and we&#8217;re going to program them to move through the paths.</p>
<p>Let&#8217;s begin by adding the enemy to the stage. Like the other symbols, we&#8217;re going to do it by creating an empty MovieClip and drawing the shape into it. In order to do this, however, we must first define some variables. Do this at the top of the code:</p>
<pre lang="actionscript">
var currentEnemy:Number = 0;//the current enemy that we're creating from the array
var enemyTime:Number = 0;//how many frames have elapsed since the last enemy was created
var enemyLimit:Number = 12;//how many frames are allowed before another enemy is created
var enemyArray:Array = new Array();//this array will tell the function when to create an enemy
var enemiesLeft:Number;//how many enemies are left on the field
enemyArray = [//defining the array
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//1's will just represent an enemy to be created
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//another row means another level
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
			  ];
</pre>
<p>I&#8217;ve extensively commented on what each variable does. Next, we need to create an <tt>onEnterFrame()</tt> function with a few actions, along with a few other stuff. Program this into the bottom of the code:</p>
<pre lang="actionscript">
createEmptyMovieClip('enemyHolder',_root.getNextHighestDepth());//create a movieclip that will hold the enemy

_root.onEnterFrame = function(){
	makeEnemies();//we'll just make some enemies
}

function makeEnemies():Void{//this function will add enemies to the field
	if(enemyTime < enemyLimit){//if it isn't time to make them yet
		enemyTime ++;//then keep on waiting
	} else {//otherwise
		var theCode:Number = enemyArray[currentLvl-1][currentEnemy];//get the code from the array
		if(theCode == 1){//if it's set as 1
			//then create a new enemy and add it to the enemy holder
			enemyHolder.createEmptyMovieClip('enemy'+currentEnemy,enemyHolder.getNextHighestDepth());
			//now we're going to draw the enemy. It'll just be a tiny red circle
			enemyHolder['enemy'+currentEnemy].beginFill(0xFF0000);//coloring them red gray
			enemyHolder['enemy'+currentEnemy].moveTo(0, 2.5);//move the entire shape a certain way
			//create 4 curves so that it'll look like a circle
			enemyHolder['enemy'+currentEnemy].curveTo(0,10,5,10);
			enemyHolder['enemy'+currentEnemy].curveTo(10,10,10,5);
			enemyHolder['enemy'+currentEnemy].curveTo(10,0,5,0);
			enemyHolder['enemy'+currentEnemy].curveTo(0,0,0,5);
			enemyHolder['enemy'+currentEnemy].endFill();//end the fill
		}
		currentEnemy ++;//move on to the next enemy
		enemyTime = 0;//and reset the time
	}
}
</pre>
<p>Now, if you test out the game, a red dot should appear on the top left corner of the screen. But, this isn't what we want for our game, is it? In order to place the enemy so that it's right next to the start point, we're going to have to add some code. Remember the variables <tt>startDir</tt> and <tt>finDir</tt> that we created all that time ago? If you don't here's what it should look like at around line 11:</p>
<pre lang="actionscript">
var startDir:String;//the direction the enemies go when they enter
var finDir:String;//the direction the enemies go when they exit
var startCoord:Number;//the coordinates of the beginning of the road
</pre>
<p>Well, we're going to use these variables. Find in the <tt>makeRoad()</tt> function where we create add in the <strong>SPECIAL DIRECTIONAL ROAD PIECE</strong> (around line 129). Add the following code to the bottom of that <tt>if</tt> statement:</p>
<pre lang="actionscript">
if(lvlArray[i] == 'START'){//if this is a start block
	//then define the startDir and StartCoord based on it's coordinates
	if(roadHolder['block'+i]._x == 0){
		_root.startDir = 'RIGHT';
		_root.startCoord = roadHolder['block'+i]._y;
	} else if (roadHolder['block'+i]._y == 0){
		_root.startDir = 'DOWN';
		_root.startCoord = roadHolder['block'+i]._x;
	} else if (roadHolder['block'+i]._x == 525){
		_root.startDir = 'LEFT';
		_root.startCoord = roadHolder['block'+i]._y;
	} else if (roadHolder['block'+i]._y == 275){
		_root.startDir = 'UP';
		_root.startCoord = roadHolder['block'+i]._x;
	} else {
		//this level won't work if not any of these values
	}
} else if (lvlArray[i] == 'FINISH'){//if this is a finish block
	//then define the finDir based on it's coordinates
	if(roadHolder['block'+i]._x == 0){
		_root.finDir = 'LEFT';
	} else if (roadHolder['block'+i]._y == 0){
		_root.finDir = 'UP';
	} else if (roadHolder['block'+i]._x == 525){
		_root.finDir = 'RIGHT';
	} else if (roadHolder['block'+i]._y == 275){
		_root.finDir = 'DOWN';
	} else {
		//this level won't work if not any of these values
	}
}
</pre>
<p>Next, we have to take the variables we just defined in this code and make the enemy use them. We also have to make the enemy move along the path. Go back to the <tt>makeEnemies()</tt> function and add the following code in the <tt>if(theCode == 1){</tt> statement:</p>
<pre lang="actionscript">
//add a few variables to the enemy
enemyHolder['enemy'+currentEnemy].maxSpeed = 3;//how fast it can possibly go
enemyHolder['enemy'+currentEnemy].xSpeed = 0;
enemyHolder['enemy'+currentEnemy].ySpeed = 0;
//checking what the start direction is
if(_root.startDir == 'UP'){//if it's starting up
	enemyHolder['enemy'+currentEnemy]._y = 300;//set the y value off the field
	enemyHolder['enemy'+currentEnemy]._x = _root.startCoord;//make the x value where it should be
	enemyHolder['enemy'+currentEnemy].xSpeed = 0;//make it not move horizontally
	enemyHolder['enemy'+currentEnemy].ySpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;//make it move upwards
} else if(_root.startDir == 'RIGHT'){//and so on for other directions
	enemyHolder['enemy'+currentEnemy]._x = -25;
	enemyHolder['enemy'+currentEnemy]._y = _root.startCoord;
	enemyHolder['enemy'+currentEnemy].xSpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;
	enemyHolder['enemy'+currentEnemy].ySpeed = 0;
} else if(_root.startDir == 'DOWN'){
	enemyHolder['enemy'+currentEnemy]._y = -25;
	enemyHolder['enemy'+currentEnemy]._x = _root.startCoord;
	enemyHolder['enemy'+currentEnemy].xSpeed = 0;
	enemyHolder['enemy'+currentEnemy].ySpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;
} else if(_root.startDir == 'LEFT'){
	enemyHolder['enemy'+currentEnemy]._x = 550;
	enemyHolder['enemy'+currentEnemy]._y = _root.startCoord;
	enemyHolder['enemy'+currentEnemy].xSpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;
	enemyHolder['enemy'+currentEnemy].ySpeed = 0;
}

enemyHolder['enemy'+currentEnemy]._x += 5;//fixing the x value
enemyHolder['enemy'+currentEnemy]._y += 5;//fixing up the y value

enemyHolder['enemy'+currentEnemy].onEnterFrame = function(){//give it some functions
	this._x += this.xSpeed;
	this._y += this.ySpeed;
}
</pre>
<p>The final thing we have to do in this lesson is make the enemy turn when it should turn. To do this, we must go back to the <strong>Directional Block</strong>. We're going to use the directional block to access all of the enemies coordinates. If the coordinates are close enough to the block, then it will make the enemy change direction. Find the <tt>if(String(lvlArray[i])){</tt> statement in the <tt>makeRoad()</tt> function. Add this code to the end of it:</p>
<pre lang="actionscript">
roadHolder['block'+i].directType = lvlArray[i];//accessing the type of block it is
roadHolder['block'+i].onEnterFrame = function(){//add some functions to this block
	//then it'll act as a directioning block
	for(i = 0;i<_root.enemyArray[currentLvl-1].length;i++){//create a loop
		//if the enemy's coordinates are too close to this block
		if(this._x >= _root.enemyHolder['enemy'+i]._x - _root.enemyHolder['enemy'+i]._width*.5
		&& this._x +6<= _root.enemyHolder['enemy'+i]._x + _root.enemyHolder['enemy'+i]._width*.5
		&#038;&#038; this._y >= _root.enemyHolder['enemy'+i]._y - _root.enemyHolder['enemy'+i]._height*.5
		&& this._y +6<= _root.enemyHolder['enemy'+i]._y + _root.enemyHolder['enemy'+i]._height*.5){
			//then move the enemy's direction based on what direction this block points to
			if(this.directType == 'UP'){
				_root.enemyHolder['enemy'+i].xSpeed = 0;
				_root.enemyHolder['enemy'+i].ySpeed = -_root.enemyHolder['enemy'+i].maxSpeed;
			} else if(this.directType == 'RIGHT'){
				_root.enemyHolder['enemy'+i].xSpeed = _root.enemyHolder['enemy'+i].maxSpeed;
				_root.enemyHolder['enemy'+i].ySpeed = 0;
			} else if(this.directType == 'DOWN'){
				_root.enemyHolder['enemy'+i].xSpeed = 0;
				_root.enemyHolder['enemy'+i].ySpeed = _root.enemyHolder['enemy'+i].maxSpeed;
			} else if(this.directType == 'LEFT'){
				_root.enemyHolder['enemy'+i].xSpeed = _root.enemyHolder['enemy'+i].maxSpeed;
				_root.enemyHolder['enemy'+i].ySpeed = 0;
			}
		}
	}
}
</pre>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt3/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt3/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt3/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-3/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-2/</link>
					<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-2/#comments</comments>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:02:57 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1491</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 2: Adding Turrets Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 2: Adding Turrets</h3>
<p>Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is created. Let&#8217;s begin, shall we?</p>
<p>Open up your main flash file and find the code where we set the empty block&#8217;s <tt>onRollOver()</tt> and <tt>onRollOut</tt> functions. Well, now we&#8217;re going to create a <tt>onRelease()</tt> function, which just means clicking. Add this code to around <strong>Line 65</strong>, or after you set the <tt>onRollOver</tt> and <tt>onRollOut</tt> functions:</p>
<pre lang="actionscript">
_root['block'+i].onRelease = function(){
	//this function will run when the empty block is clicked on
				
	//change this guy's color back
	var newColor = new Color(this);
	newColor.setRGB(0x333333);
	//set all other mouse functions to null in order to keep it from being clicked again
	this.onRollOver = null;
	this.onRollOut = null;
	this.onRelease = null;
	//create an empty turret movieclip that will be on the top root layer
	_root.createEmptyMovieClip('t'+this._name,_root.getNextHighestDepth());

	//drawing the turret, it will have a gray, circular, base with a white gun
	_root['t'+this._name].beginFill(0x999999);//coloring the base light gray
	_root['t'+this._name].moveTo(0, 12.5);//move the entire shape a certain way
	//create 4 curves so that it'll look like a circle
	_root['t'+this._name].curveTo(0,25,12.5,25);
	_root['t'+this._name].curveTo(25,25,25,12.5);
	_root['t'+this._name].curveTo(25,0,12.5,0);
	_root['t'+this._name].curveTo(0,0,0,12.5);
	_root['t'+this._name].endFill();//end the fill so we can make a new one
	
	//creating the gun
	_root['t'+this._name].createEmptyMovieClip('gun',_root['t'+this._name].getNextHighestDepth());
	_root['t'+this._name].gun.beginFill(0xFFFFFF);
	_root['t'+this._name].gun.lineTo(-2,-2);
	_root['t'+this._name].gun.lineTo(2,-2);
	_root['t'+this._name].gun.lineTo(2,15);
	_root['t'+this._name].gun.lineTo(-2,15);
	_root['t'+this._name].gun.lineTo(-2,-2);
	_root['t'+this._name].gun.endFill();
	//setting the gun to be on the center of the turret
	_root['t'+this._name].gun._x = 12.5;
	_root['t'+this._name].gun._y = 12.5;
	//set the turrets coordinates to be the blocks coordinates
	_root['t'+this._name]._x = this._x;
	_root['t'+this._name]._y = this._y;
}
</pre>
<p>Well, it&#8217;s a lot of code, but it gets the job done.</p>
<p>That&#8217;s it for this part of the tutorial. Next time, we&#8217;ll create and program some enemies!</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt2/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt2/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt2/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2-part-2/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>Tutorial: Create a Tower Defense Game in AS2</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2/</link>
					<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2/#comments</comments>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 12:01:32 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Advanced Tutorials]]></category>
		<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[tower]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1481</guid>

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 1: Setting Up a Level The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-6/">Expanding on the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/04/tutorial-create-a-tower-defense-game-in-as2-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 1: Setting Up a Level</h3>
<p>The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always very fun to play. I am here to walk you through the creation of one of these games. Let us begin, shall we?</p>
<p>In this section of the tutorial, we&#8217;re going to set up the roads and stuff onto the stage. However, the first thing we need to do is create a blank flash document with a black background with the frames per second set at 24.</p>
<p>Now, we can dive into some code. Let&#8217;s first create some code that will lay down the track for the enemies to go through. Create a new layer called &#8220;actions&#8221; and add the following code to it:</p>
<pre lang="actionscript" line="1">
stop();

//setting vars to step in for turns and special blocks
var S:String = 'START';
var F:String = 'FINISH';
var U:String = 'UP';
var R:String = 'RIGHT';
var D:String = 'DOWN';
var L:String = 'LEFT';

var startDir:String;//the direction the enemies go when they enter
var finDir:String;//the direction the enemies go when they exit
var startCoord:Number;//the coordinates of the beginning of the road
var lvlArray:Array = new Array();//this array will hold the formatting of the roads

lvlArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,R,1,1,D,0,0,R,1,1,D,0,0,R,1,1,D,0,0,
			0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
			0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
			S,D,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,R,1,F,
			0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
			0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
			0,R,1,1,U,0,0,R,1,1,U,0,0,R,1,1,U,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
			];

//the names of these variables explain what they do
var currentLvl:Number = 1;
var gameOver:Boolean = false;

function startGame():Void{//we'll run this function every time a new level begins
	//right now we don't have any code
}

_root.createEmptyMovieClip('roadHolder',_root.getNextHighestDepth());//add a MovieClip to hold the road
function makeRoad():Void{
	var row:Number = 0;//the current row we're working on
	var block;//this will act as the block that we're placing down
	for(i=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array
		if(lvlArray[i] == 0){//if the current index is set to 0 
			/////*****EMPTY BLOCK*****/////
			_root.createEmptyMovieClip('block'+i,_root.getNextHighestDepth());//create a gray empty block
			_root['block'+i].beginFill(0x333333);
			_root['block'+i].lineTo(0,0);
			_root['block'+i].lineTo(0,25);
			_root['block'+i].lineTo(25,25);
			_root['block'+i].lineTo(25,0);
			_root['block'+i].endFill();
			//and set the coordinates to be relative to the place in the array
			_root['block'+i]._x= (i-row*22)*25;
			_root['block'+i]._y = row*25;
		} else if(lvlArray[i] == 1){//if there is supposed to be a row
			/////*****EMPTY ROAD*****/////
			//just add a box that will be a darker color and won't have any actions
			roadHolder.createEmptyMovieClip('block'+i,roadHolder.getNextHighestDepth());
			roadHolder['block'+i].beginFill(0x111111);
			roadHolder['block'+i].lineTo(0,0);
			roadHolder['block'+i].lineTo(0,25);
			roadHolder['block'+i].lineTo(25,25);
			roadHolder['block'+i].lineTo(25,0);
			roadHolder['block'+i].endFill();
			roadHolder['block'+i]._x= (i-row*22)*25;
			roadHolder['block'+i]._y = row*25;
		} else if(String(lvlArray[i])){//if it's a string, meaning a special block
			/////*****SPECIAL DIRECTIONAL ROAD PIECE*****/////
			roadHolder.createEmptyMovieClip('block'+i,roadHolder.getNextHighestDepth());
			roadHolder['block'+i].beginFill(0x111111);
			roadHolder['block'+i].lineTo(0,0);
			roadHolder['block'+i].lineTo(0,25);
			roadHolder['block'+i].lineTo(25,25);
			roadHolder['block'+i].lineTo(25,0);
			roadHolder['block'+i].endFill();
			roadHolder['block'+i]._x= (i-row*22)*25;
			roadHolder['block'+i]._y = row*25;
		}
		for(var c:Number = 1;c<=16;c++){
			if(i == c*22-1){
				//if 22 columns have gone by, then we move onto the next row
				row++;
			}
		}
	}
}
//run these functions at the start
makeRoad();
startGame();
</pre>
<p>That's a lot of code, ain't it? Hopefully, I've given you enough instructions in the comments. If you test out our game, a nice little path should be shown which the enemies will travel through, along with some blank gray boxes where we'll be able to place some turrets.</p>
<p>Before I end this part of the tutorial, I want to give these empty boxes some <tt>rollOver</tt> and <tt>rollOut</tt> effects. Find where I added the comment saying <tt>/////*****EMPTY BLOCK*****/////</tt> (lines 44-54). Add this code to the bottom of that section of the <tt>if</tt> statement:</p>
<pre lang="actionscript">
_root['block'+i].onRollOver = function(){
	//Change the color to green
	var newColor = new Color(this);
	newColor.setRGB(0x006600);
}
_root['block'+i].onRollOut = function(){
	//Change this color back to gray
	var newColor = new Color(this);
	newColor.setRGB(0x333333);
}
</pre>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt1/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt1/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as2/pt1/tower-defense-as2-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as2/feed/</wfw:commentRss>
			<slash:comments>17</slash:comments>
		
		
			</item>
		<item>
		<title>4 Reasons Why All Flash Game Developers Should Learn PHP</title>
		<link>http://www.mrsunstudios.com/blog/flash/4-reasons-why-all-flash-game-developers-should-learn-php/</link>
		
		<dc:creator><![CDATA[MrSun]]></dc:creator>
		<pubDate>Mon, 02 Mar 2009 17:59:12 +0000</pubDate>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1477</guid>

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

					<description><![CDATA[Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 7: Finishing Touches Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3/">Setting up Level</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-2/">Adding Turrets</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-3/">Adding Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-4/">Making Turrets Attack Enemies</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-5/">Winning/Losing the Game</a></li>
<li><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-6/">Expanding on the Game</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/02/tutorial-create-a-tower-defense-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 7: Finishing Touches</h3>
<p>Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I won&#8217;t comment any of the code I give you, as the knowledge you have learned from this tutorial should tell you what&#8217;s going on. Luckily for you, there aren&#8217;t too many finishing touches for us to make, so your brain won&#8217;t hurt too much.</p>
<p>One of the things I want to do is show the player the range of the turret. If you&#8217;ve ever played a tower defense game, you you should know what I&#8217;m talking about. A translucent circle will appear to show the player how far the turret can shoot. We&#8217;re going to show this both when somebody hovers over an empty block and when somebody hovers over a turret. Let&#8217;s start with hovering over an empty block.</p>
<p>Lets first start by opening up &#8220;source.fla&#8221;. Add this code to the top. Don&#8217;t ask any questions&#8230; or else&#8230;</p>
<pre lang="actionscript">
var rangeCircle:Shape = new Shape();
rangeCircle.graphics.beginFill(0x006600,.5);
rangeCircle.graphics.drawCircle(12.5,12.5,100);
rangeCircle.graphics.endFill();
</pre>
<p>Open up &#8220;EmptyBlock.as&#8221;, would you? Find the <tt>thisMouseOver()</tt> function and add the following code to it:</p>
<pre lang="actionscript">
_root.rangeCircle.x = this.x;
_root.rangeCircle.y = this.y;
_root.addChild(_root.rangeCircle);
</pre>
<p>Find the <tt>thisMouseOut()</tt> function and add this code to it:</p>
<pre lang="actionscript">
_root.removeChild(_root.rangeCircle);
</pre>
<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>
<pre lang="actionscript">
this.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);
</pre>
<p>Next, add these two functions to the end of the class:</p>
<pre lang="actionscript">
private function thisMouseOver(e:MouseEvent):void{
	_root.rangeCircle.x = this.x-12.5;
	_root.rangeCircle.y = this.y-12.5;
	_root.addChild(_root.rangeCircle);
}
private function thisMouseOut(e:MouseEvent):void{
	_root.removeChild(_root.rangeCircle);
}
</pre>
<p>Good stuff. Now, it should work if you hover over the turrets.</p>
<p>Now, what else can we add to our little game? The answer is: it&#8217;s up to you to decide what to add. It&#8217;s also up to you to use what you&#8217;ve learned to do it right. Thank you and good night.</p>
<h4>Final Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt7/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt7/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/tower-defense-as3/pt7/tower-defense-as3-source.zip">Source Files (Zipped)</a></center></p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-tower-defense-game-in-as3-part-7/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
	</channel>
</rss>
