<?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/"
	>

<channel>
	<title>Goofball Games</title>
	<atom:link href="http://www.goofballgames.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.goofballgames.com</link>
	<description>We take games seriously</description>
	<pubDate>Mon, 01 Mar 2010 16:08:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Social Gaming</title>
		<link>http://www.goofballgames.com/2010/03/01/social-gaming/</link>
		<comments>http://www.goofballgames.com/2010/03/01/social-gaming/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 07:29:21 +0000</pubDate>
		<dc:creator>Ameesh Kapoor</dc:creator>
		
		<category><![CDATA[Featured]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=365</guid>
		<description><![CDATA[Invites, news feeds, notifications, friends and buddies, avatars, profiles - these are the staples of social networking, but it can be a very daunting process to add these social aspects to your game or website. Goofball Games has been working in the social space for several years now - we have been involved with social [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-373" title="social" src="http://www.goofballgames.com/wp-content/uploads/2010/03/social-300x265.jpg" alt="social" width="300" height="265" />Invites, news feeds, notifications, friends and buddies, avatars, profiles - these are the staples of social networking, but it can be a very daunting process to add these social aspects to your game or website. Goofball Games has been working in the social space for several years now - we have been involved with social networks since their inception. Client side or server side, we know how to promote interaction among users and get you the high traffic that social networks bring with them.</p>
<p>Here are just a few examples of games we have worked on in the social space - we have a couple in the works right now, so stay tuned!</p>
<p><a href="http://apps.facebook.com/fiftystates">Fifty States</a><br />
<a href="http://apps.facebook.com/monstertruckcurfew">Monster Truck Curfew</a><br />
<a href="http://apps.facebook.com/shufflesnakebites">Shuffle Snake Bites</a><br />
<a href="http://apps.facebook.com/mtvshowquizzes">MTV Show Quizzes</a><br />
<a href="http://backchannel.mtv.com">Backchannel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/03/01/social-gaming/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 10 Win or Lose)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-10-win-or-lose/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-10-win-or-lose/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:19:17 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=158</guid>
		<description><![CDATA[Ok, so based on the comments I realized that not having a &#8220;win&#8221; or &#8220;lose&#8221; condition at this point was not working.  Well, fear not my friends, I have fixed the situation and now you can lose or win depending on how you play the game now.  Yay!
Having a win/lose condition at this [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so based on the comments I realized that not having a &#8220;win&#8221; or &#8220;lose&#8221; condition at this point was not working.  Well, fear not my friends, I have fixed the situation and now you can lose or win depending on how you play the game now.  Yay!</p>
<p>Having a win/lose condition at this point is no good if you don&#8217;t know how anything else works.  Therefore, If you haven&#8217;t read <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-9-scores-and-wave-timeline/">How to build a tower defense flash game – Part 9 - Scores and Wave Timeline!</a> you should go back and read it before you continue. <span id="more-158"></span></p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part10.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androida/Part10.swf" /></object></p>
<p><strong>As always the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part10.zip">downloaded by clicking here</a></strong></p>
<p>Man, I have to admit, that I am pretty excited about this one, because this is the first time I can say that we have a complete and working game.  You can win and lose and it&#8217;s all up to the player to prove his worth.</p>
<p>To accomplish all we needed to have a win/lose ending, we needed to introduce a few new variables:</p>
<ul>
<li>_root.health = 20;</li>
</ul>
<p>We use this variable to keep track of the players current health.</p>
<ul>
<li>_root.hasGameFinished = false;</li>
</ul>
<p>We use this variable when the game is over and use it to add to &#8220;tower&#8221; explosions when you win or lose - We also</p>
<ul>
<li>_root.currentNumberOfCreeps = 0;</li>
</ul>
<p>We need a new way to keep track of the current creeps on the map at any time - unfortunately, _root.creepCount doesn&#8217;t fit for us since it is used to not only keep track of the total creeps that have ever existed, but we use it for create the movieclips and therefore we can use it.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/87.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>We&#8217;ve added new code to leak() to now account for the removal of health.  We now remove one point of health for each creep that the player lets through.  Once the health value falls to &#8220;0&#8243; we see if the _root.hasGameFinished is still false and we tell the user that he lost then we set _root.hasGameFinished to true so it doesn&#8217;t get run again.  Pretty neat huh!</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/88.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>We add an additional line to the UpdateInfoBar function to account for the new &#8220;health&#8221; field.  We didn&#8217;t need to add any additional calls to the function though since we have it called enough - Whenever the creeps make it through the defense this will be updated with the new correct health value.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/89.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>For a winning condition to to take place we need to keep track of how many creeps are around and what level we are on.  This is the function we use to keep track of that fact.  When a creep is created we increase _root.currentCreepCount by one and when a creep is killed we remove one from it as well.  Once we hit level 35 (that is currently the last level) we reallly start to pay attention to that value and when it is zero we know that the game is over.</p>
<p>Whether the game is won or lost by the player, we do the same animation - we blow up all the towers.  It&#8217;s a cool effect and all we need to do to make it happen is to loop through all the towers and call &#8220;Explosion&#8221; for the x/y coordinates of the given tower.  Originally it was only done for lose condition, but I liked it so much I remove the check and made it happen for the win condition as well.</p>
<p>Well, it&#8217;s a quick one, but we have a complete game - Don&#8217;t you fret though, while it is a complete game we still have more work to do&#8230; Until next time&#8230; You can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">click here</a> to go back to the introduction</p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-10-win-or-lose/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 9 Scores and Wave Timeline)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-9-scores-and-wave-timeline/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-9-scores-and-wave-timeline/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:18:46 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=156</guid>
		<description><![CDATA[Welcome to another installment of my tower defense series - Part 9.  I know you&#8217;ve been asking for more and I&#8217;ve done my best to get creative.  I&#8217;ve already built out the next few Parts the only thing I need to do now is explain it all, but for now without any more [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to another installment of my tower defense series - Part 9.  I know you&#8217;ve been asking for more and I&#8217;ve done my best to get creative.  I&#8217;ve already built out the next few Parts the only thing I need to do now is explain it all, but for now without any more delay - This lesson we will be covering the following subjects:</p>
<ol>
<li>Multiple Waves of creeps at the same time.</li>
<li>Creation of a time line for creep releasing.</li>
<li>Start tracking a users score in a full game.</li>
</ol>
<p>Before we begin, if you haven&#8217;t read <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-8-poison-tower-and-basic-startup/">How to build a tower defense flash game – Part 8 - Poison Tower and Basic Startup!</a> you may become overwhelmed at the scope and content and should consider going back and read the earlier articles which may answer some questions if you came here first. <span id="more-156"></span></p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part9.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androida/Part9.swf" /></object></p>
<p><strong>As always the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part9.zip">downloaded by clicking here</a></strong></p>
<p>So, it&#8217;s looking pretty good so far, we obviously have come a long way since setting way points and following basic paths.  Some of the things we are setting up now are to add a little excitement and competition to the game and what better way then add the concept of scoring to the mix.  Why does a game need scoring?  A game needs scoring to keep it fresh, otherwise the game is just about getting through it once.  Sure you might once in a while play a game twice, because a specific section was especially challenging, but for the most part without a method to make competition a part of the game - even on a great game - the game life will be shorter on games without scores.</p>
<p><strong>SIDE NOTE: Community Features</strong></p>
<p>When you add a personal high score the game is giving you a personal benchmark to achieve.  The game could be terrible, but it isn&#8217;t about it being bad as much as it is now a</p>
<p>challenge to the player himself (&#8221;I know I can beat that&#8221; or &#8220;Just one more time and I can do it&#8221;). The personal benchmark takes on a new meaning when introduced into a half decent game, dare I say, a game the player actually enjoys.  Another level of achievement is had when you add a leader board to the mix - now you&#8217;ve taken the concept of a personal high score and introduced the world.  Now it isn&#8217;t as much as achieving your own personal goal, but get into the mix of beating everyone around you.  There are different types of leader boards, but I believe the &#8220;daily&#8221;, &#8220;weekly&#8221;, &#8220;monthly&#8221; and &#8220;all time&#8221; are pretty good starts.  If you can add &#8220;Easy&#8221;, &#8220;Medium&#8221; and &#8220;Hard&#8221; settings to the mix all the better - Have leader boards for each other them and I guarantee that people will play your games longer than they would normally.  Just remember, these &#8220;community&#8221; features will add life to your games, but it it only helps magnifying glass for the game - if the game is terrible, it might make it just playable - HOWEVER - if the game is great, it will make it immortal.</p>
<p><strong>END SIDE NOTE</strong></p>
<p>So for the most part this tutorial will go over adding the scoring element and some features to distiguish  the good players from the great.  Ok, so to that end we obviously need to do a little setup.  For one thing we are going to need to move code around a little and make some new variables.  In fact most of the code change relating to score will all take place in the actionscript layer in the main timeline (We try to group code when we can).  So lets get right to it.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/83.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>We&#8217;ve created a new varaible, surprise surprise, it&#8217;s &#8220;_root.score&#8221; which will contain the players current score as they progress through the game.</p>
<p>The next item is more interesting, it is a new function called &#8220;executeCallback&#8221; - I&#8217;m really unoriginal with my names, but at least you can be fairly certain how this function will be called.  We will call this function everytime we want to start a new level or when it&#8217;s not progressing to the next level it&#8217;s increasing the &#8220;_root.count&#8221; by 1.  In fact you will soon see that this will be called every second when we put in a timeline!  This function will serve as the progress bar counting down the time until the next wave release.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/85.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This is the meat and potatoes of the score function.  You will need to put &#8220;updateScore()&#8221; into the following function calls to make sure they give score updates at the appropriate time:</p>
<ul>
<li>When the function newLevel() is called.</li>
<li>When a creep is killed.</li>
<li>When a tower is upgraded.</li>
</ul>
<p>What the code does is first put the current gold amount in a temporary variable &#8220;a&#8221; and then loop through all the towers and check to see which are still active and add the cost of that tower to variable as well.  At the end we take that temporary variable and store its value back into _root.score.  As you can imagine if you have a lot of towers or if you save a lot of gold, you can score a bunch of points, however, only a good plan will determine whether saving money or spending it pays out in the end.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/86.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This is the last real update to the actionscript code in the main timeline.  This might be lost in the code change, so I felt it was important to point out.  This function originally only updated the &#8220;gold&#8221; amount when the player was killing creeps.  Now that we have a score though, it has taken on the expanded role of updating the score as well.  I haven&#8217;t changed the functions that called it since the last tutorial, because any time we are increasing or decreasing gold, we are also updating the score as well.</p>
<p>Just to keep you informed - This does mean that you will need to create a new text field in the movie clip &#8220;InfoBar&#8221; with the instance name of &#8220;ScoreAmount&#8221;, but that shouldn&#8217;t be too hard for a tower defense master like yourself.</p>
<p><strong>Now to the Timeline</strong></p>
<p>So what&#8217;s a &#8220;wave&#8221; time line you ask.  Well, it&#8217;s easy, instead of just having a level end when the towers are all destroyed or a timer has been reached, we give the player the control over when the next wave takes place.  We also give an added incentive that you score more points if we release the wave before the next wave is scheduled to be released.  Again, you can see how we re tapping into the users desire to get bigger scores (It doesn&#8217;t hurt that the average end score can be like 100,000 points - my best was 534,680 points).</p>
<p>So to do this we need to make a &#8220;timeline&#8221;  you can see the code below:</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/82.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Basically we just do a simple mask for a large line of colorful rectangles that *eventually* will be different types of creeps.  The distance of each rectangle is such, that when the timer starts the full rectangle will move off the screen as the next wave is released.  The action script code is setup to align &#8220;programmatically&#8221; so the first rectangle is in the correct position when the game loads for the first time.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/84.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This is where all the fun happens.</p>
<p>When this code is first run, we create a timer interval that runs every 1 second and calls our function above &#8220;executeCallback&#8221;.  The function does nothing while the game has not yet been started.  When the game is started, the player can click on the timeline and it will automatically set the &#8220;nextLevel&#8221; in motion - giving an interest on money saved, resetting the &#8220;Count&#8221; variable and sending the next wave.  This has the awesome benefit that we can get multiple creep waves on the same screen for a crazy experience.</p>
<p>You can now get a whole new experience then before with this type of functionality.  Don&#8217;t mind my non descriptive art, you could lable the timeline&#8217;s rectangles with whatever you want.  A red rectangle could be a flying creep, a blue a strong creep, green a groups of closely knit creeps, etc.  Try it out and post some feedback with your scores&#8230; I&#8217;d love to see if you can beat my score.</p>
<p>That&#8217;s it for now - How about checking out the next part while were on a roll -<br />
<strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-10-win-or-lose/">How to build a tower defense flash game – Part 10 - Win or Lose!</a></strong></p>
<p>You can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">click here</a> to go back to the introduction</p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-9-scores-and-wave-timeline/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 8 - Poison Tower and Basic Startup)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-8-poison-tower-and-basic-startup/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-8-poison-tower-and-basic-startup/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:18:18 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=154</guid>
		<description><![CDATA[_Note: Fixed a problem with Part 8 download it should now be working - Thanks MIV for pointing that out._
We are now at Part 8!  You should be pretty up on your tower defense games at this point in the series.  However, before we begin you should make sure you have reviewed the [...]]]></description>
			<content:encoded><![CDATA[<p>_Note: Fixed a problem with Part 8 download it should now be working - Thanks MIV for pointing that out._</p>
<p>We are now at Part 8!  You should be pretty up on your tower defense games at this point in the series.  However, before we begin you should make sure you have reviewed the previous tutorials if you came here directly from a link.  If this is the first time you&#8217;re viewing the page you may want to start at the beginning by <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">clicking here</a> or you can go to the previous tutorial Part 7 by clicking the below link:<br />
<strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-7-money-and-upgrades/">How to build a tower defense flash game – Part 7 - Gold and Upgrades!</a></strong> <span id="more-154"></span></p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part8.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androida/Part8.swf" /></object></p>
<p><strong>Once again all the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part8.zip">downloaded by clicking here</a></strong></p>
<p>Ok, so this is the meat and potatoes that handles the &#8220;poison&#8221; functionality.   We need to dole out damage consistently for a period of time.  To do this properly, I&#8217;ve decided to have just a time that runs for the express purpose of doing &#8220;creep events&#8221;.  This doesn&#8217;t just mean poison, but it works for my purposes now.    So we setup a timer for the given function.</p>
<ul>
<li>_root["creepEventInterval"] = setInterval(_root, &#8220;creepEvent&#8221;, 500);</li>
</ul>
<p>This just runs the function &#8220;creepEvent&#8221; every 1/2 second continuously until the game ends -</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/02/78.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The &#8220;fire&#8221; function is now changed:</p>
<ul>
<li>function fire(from, to, speed, acc, maxspeed, projectile, damage, splash, slow, poison);</li>
</ul>
<p>We&#8217;ve included a &#8220;poison&#8221; parameter which acts as the % amount to deduct from enemies help have 1/2 second.  The change is are relatively simple - The first one just asking - if the projectile that hit the enemy creep has the poison parameter set the poison attribute to the creep.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/79.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>and later in the same &#8220;fire&#8221; function and given out when a tower causes splash damage.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/80.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>We want the poison damage we set to be set by the distance from one enemy to another - so if they are far enough away the poison they take is less.</p>
<p>Finally, the last thing we set the &#8220;marker&#8221; movie clip to contain a clause that once we click on a &#8220;valid&#8221; spot of the green area and place a tower, we finally call the &#8220;newLevel&#8221; function and start the creeps moving and remove the instruction screen.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/81.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>So we close yet another chapter - We have covered a bunch of new subjects and improved what towers can now do.  We can now interchange some new towers - splash, poison and slow.  We&#8217;ve only concentrated on the specific types of towers, but the true power comes from mixing them together.</p>
<p>Think of it a tower with poison/high damage/small range/splash!  Wow, I can&#8217;t wait to start really making stuff now!  You can now move on to <strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-9-scores-and-wave-timeline/">How to build a tower defense flash game – Part 9 - Scores and Wave Timeline!</a></strong></p>
<p>Or you can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">click here</a> to go back to the introduction</p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-8-poison-tower-and-basic-startup/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 7 - Money and Upgrades!)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-7-money-and-upgrades/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-7-money-and-upgrades/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:17:45 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=152</guid>
		<description><![CDATA[I&#8217;ve taken a much needed holiday vacation from my work, but I&#8217;m back to work and I&#8217;m all ready to get back into this.  If this is the first time you&#8217;re viewing the page you may want to start at the beginning by clicking here or you can see the previous part 6 if [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve taken a much needed holiday vacation from my work, but I&#8217;m back to work and I&#8217;m all ready to get back into this.  If this is the first time you&#8217;re viewing the page you may want to start at the beginning by <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">clicking here</a> or you can see the previous part 6 if you just missed that one :^D <span id="more-152"></span></p>
<p><strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-6-tower-powers/">How to build a tower defense flash game – Part 6 - Tower Powers!</a></strong></p>
<p>Anyway so you&#8217;ve read from the beginning lets get started!  You can&#8217;t get something for nothing.  That&#8217;s what my family always said, so why should tower defense games be any different.  You need to make people work for it.  That means you need to pay your towers for hire and the better the towers, the more they should cost.  Don&#8217;t worry we won&#8217;t make anything too expensive, but keep in mind that this is &#8220;level balancing&#8221; and it is a hugely important step.  If you give too much gold or make the towers too inexpensive then the game will be too easy, but if you make them cost too much people may not play your game long enough to buy them.</p>
<p>So you&#8217;ll find out that this specific tutorial we more about designing that complicated math or real programming - They&#8217;ll be plenty of programming, but nothing we can&#8217;t handle.  It&#8217;s a tricky balance, that usually can only be helped by play testing - lots of play testing, but hey, it&#8217;s your game your making it&#8217;ll be fun.</p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part7.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androidas/Part7.swf" /></object></p>
<p><strong>Once again all the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part7.zip">downloaded by clicking here</a></strong></p>
<p>So you want to keep your gold pretty consistent across the board, you don&#8217;t want to give out different amounts of gold for each creep, it makes it hard for the user to make a plan with their tower strategy - What you usually see if a steady increase in the amount of gold given out on a per wave basis rise every 5 or 10 waves, starting at 1 or 2 gold with a wave comprising of about 10 or 20 creeps attacking at a time.</p>
<p>If you decide to have fewer (for say boss levels) then adjust your gold accordingly.  You could even setup some form of equation based on hit-points of the creeps.</p>
<p>I will stick with the the trusty multiple of two golds for every 10 levels(ish).  To make it interesting I usually give a boss level every 5th level and I give a a multiplier of 2.5 times.  We&#8217;ll also give the play 80 to play around with as well.  Obviously this is a general guide line not a strict rule.</p>
<p>So to recap the gold per level:</p>
<ul>
<li>Level 1 - 5: 1 gold per creep</li>
<li>Level 6: Boss enemy of a specific type (25 gold)</li>
<li>Level 7 - 11: 1 gold per creep</li>
<li>Level 12: Another Boss&#8230; (50 gold)</li>
<li>Level 13 - 18: 2 gold per creep</li>
</ul>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/76.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>and so on&#8230;</p>
<p>We can also give an interest rate for each level - You can make things interesting in the later levels forcing the user to decide between building a new tower and collecting interesting on the money saved.</p>
<p>Next we need to determine how much to charge for each tower.  Well this is a little more specific, but it must be tied to the amount of money brought in.  Don&#8217;t make a tower that cost so much you need to save 25 levels for it, because you still need to give it upgrades.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/77.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The above graphic labels where the upgrade window shows up when you click on a tower (Labeled as 1) and the info box that currently only lets the user know how much gold he has (Labeled as 2).</p>
<p>Starting at label 1.  There is almost nothing hard to create here in terms of programming - We have a button &#8220;upgrade&#8221; that when clicked calls _root.upgrade().  That code is listed below:</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/73.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The only thing that isn&#8217;t listed here is an multi-dim array within each of the towers.  This multi-dim array just has enough information to upgrade &#8220;description&#8221;, &#8220;cost in gold&#8221;, &#8220;damage&#8221;, &#8220;range&#8221;, &#8220;rate&#8221;, &#8220;splash percentage&#8221;, &#8220;slow percentage&#8221;.  Such as that listed below.</p>
<ul>
<li>["Damage: 40rRange: 120rSplash:NorSlow:No", 20, 40, 120, 5, 0, 0]</li>
</ul>
<p>At the point in the above code - We have a tower selected and we just check to see if the gold the user has is enough to purchase the upgrade - if the player has the gold we upgrade the current tower levels to that of the new upgrade (we keep track of which upgrade we are on with the &#8220;level&#8221; variable).  We then call &#8220;towerInfo&#8221; to display the now upgraded levels to the player and deduct the gold for that upgrade cost. Viola, you have a new upgrade tower - easy wasn&#8217;t it&#8230;</p>
<p>Lets quickly talk about &#8220;towerInfo&#8221;, this function was created before, but now we are using the function is not just draw the range of the tower anymore.  Now we are going to display the upgrade information and the towers current level information.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/74.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>If a tower is selected - then we display all the information we know about the tower - if no tower is selected then we show nothing.  To sum this up, we you click on a tower make this information visible, otherwise don&#8217;t.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/75.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Right now this function is just for displaying &#8220;gold&#8221; levels for the player so he doesn&#8217;t have to guess how much gold he/she currently has.  We are going to use it more when we want to give a score or have a &#8220;status&#8221; of what creeps might next be coming up .</p>
<p>Wow, we&#8217;ve done a lot of polish in this one - Not so much code was changed seriously, much more in design then anything else, but that&#8217;s &#8216;ok&#8217; it means we are really making this into something special.</p>
<p>That&#8217;s it!  We still have so much to do, but we&#8217;ve really hit a mile stone!  You still can&#8217;t die, but it really feels like a complete game now.</p>
<p><strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-8-poison-tower-and-basic-startup/">How to build a tower defense flash game – Part 8 - Poison Tower and Basic Startup!</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-7-money-and-upgrades/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 6 - Tower Powers!)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-6-tower-powers/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-6-tower-powers/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:17:12 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=150</guid>
		<description><![CDATA[Wow, the last article was intense - How can I possibly beat that?  Oh, I think I can come up with an idea, um, how about tower powers?!  You know that thing that makes the towers placed unique.  Well I could think of a few powers that can be combined to make [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, the last article was intense - How can I possibly beat that?  Oh, I think I can come up with an idea, um, how about tower powers?!  You know that thing that makes the towers placed unique.  Well I could think of a few powers that can be combined to make virtually all tower types - but then i realized something so complete would take a lot of writing to explain.  So here are the intermediate level tower powers! <span id="more-150"></span></p>
<p>If you haven&#8217;t already read the last tutorial - <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-5-wave-after-wave-of-creep-killin/">How to build a tower defense flash game – Part 5 - Wave after Wave of Creep Kill’in!</a> you should seriously consider giving it a go.</p>
<p>So this is what we will build today - Select the different towers and play around.  The black tower is our default &#8220;damage only&#8221; tower.    The red tower is an area of effect tower that does a tad less damage but it is spread across a larger area.  The blue has a small area of effect and slows enemies by 25% and does minimal damage.</p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part6.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androida/Part6.swf" /></object></p>
<p><strong>Once again all the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part6.zip">downloaded by clicking here</a></strong>.</p>
<p>So if you start to look at the code, you may also have noticed that &#8220;tower powers&#8221; wasn&#8217;t the only thing I added.  We&#8217;ll your right, I did a little clean up - I also added a health bar for all the creeps.  I reorganized the tower onEnterFrame, well maybe you didn&#8217;t realize that but I did and it was all for a good cause.</p>
<p>The micro changes were:<br />
The creeps now have health bars over their heads.  This is mainly so you can see the splash damage at work, but we needed to have one created anyway - After creating the health bar symbols (health_bar &gt; bar) I put the following code in the creeps onEnterFrame:</p>
<ul>
<li>this.health_bar.health._width = 30 / this.maxHealth * this.health;</li>
</ul>
<p>We now have a health bar.</p>
<p>Next, I changed the spawn function code so now it uses the local variable &#8220;level&#8221; instead of &#8220;_root.wave&#8221;.  I did this because _root.level is global and level is local.  When we spawn a new enemy we need to check the level of the spawn object not the current level (_root["spawned" + level] == 0).  This comes into play if you want the user to be able to spawn multiple waves at the same time.</p>
<p>Another change was to the tower code itself, I reorganized the onEnterFrame because it was redundant in some places - For example I checked for &#8220;target=0&#8243; in multiple places.  I just condensed the code and put the fire function in the right place.</p>
<p>Even with some of the micro changes I did - All the main changes were to support these 6 new symbols:</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/69.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>We now have a &#8220;Fire&#8221; and a &#8220;Water&#8221; tower!  The firing code is almost exactly the same, and could be if I didn&#8217;t want the towers to actually function differently.  Since I did want them to do different things we had to change the fire function, which now takes two additional parameters.</p>
<p>These parameters are:</p>
<blockquote><p>&#8220;splash&#8221; - This value is actually the range around projectile when it hit the creep.  It does more damage to those creeps that are closer to the target creep in the blast radius.</p>
<p>&#8220;slow&#8221; - This value is a number from 1 - 100 and it is the actual percentage that a given creep will be slowed down by when hit by the projectile.</p></blockquote>
<p>So without further ado, lets jump right into the code.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/70.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Ok you see the two new parameters in the fire code - I also added a little extra at the start as well, that would be considered a micro change if it wasn&#8217;t so obviously necessary.  The new code above positions the &#8220;projectile&#8221; symbol we just duplicated at the tip of the tower.  What this means is that when the tower fires the projectile will appear at the tip of the gun.  If you look at Part 4, you will notice the projectiles were being spawned in the center of the tower.  This wasn&#8217;t too obvious when the bullets were small, but if you replace the code with what was there before:</p>
<blockquote><p>r._x = from._x;<br />
r._y = from._y;</p></blockquote>
<p>The fire and water towers look all wrong.  So I fixed it by taking in the towers rotation into account and then once we have the correct radians, we added half the width (because the tower was centered in the symbol) and half the height to the x and y coordinates.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/71.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Now we get into the heart of the matter.  In Part 4 we literally had a call to the &#8220;Explosion&#8221; function, took away some health and then deleted the projectile.  Not anymore, our projectiles have powers now baby!</p>
<p>Now if the tower passes in a &#8220;slow&#8221; parameter, we take that specific percentage off of the enemy the projectile hit.  It&#8217;s pretty straight forward, but just to write out the math:</p>
<p>If the creeps maxspeed is 10 and the slow parameter was 25</p>
<blockquote><p>this.targ.speed = 10 / 100 * (100 - 25);<br />
this.targ.speed = 10 / 100 * (75);<br />
this.targ.speed = .1 * (75);<br />
this.targ.speed = 7.5;</p></blockquote>
<p>Viola, the target just lost 25% of his forward momentum.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/72.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Ok this &#8220;beast&#8221; of code handles all of the &#8220;splash&#8221; issues.  It can look daunting, but we&#8217;ll make it through it.  When the projectile hits its target we loop through all of the other creeps and find out how far they were from the target creep when the hit took place.  If the other creep was within the splash radius, we give out damage based on how far away the creep was at the time.</p>
<ul>
<li>_root.creepArray[i].health = _root.creepArray[i].health - this.damage * (1 - (d / this.splash));</li>
</ul>
<p>Let run through one example:</p>
<p>If the splash distance was 50 (very big bomb - Think of the slash number as the same thing as the range number for towers), the distance away was 10 (fairly close to the explosion) and the damage from this type of bomb is 20.</p>
<blockquote><p>_root.creepArray[i].health = _root.creepArray[i].health - 20 * (1 - (10 / 50));<br />
_root.creepArray[i].health = _root.creepArray[i].health - 20 * (1 - (.2));<br />
_root.creepArray[i].health = _root.creepArray[i].health - 20 * (.8);<br />
_root.creepArray[i].health = _root.creepArray[i].health - 16;</p></blockquote>
<p>So in this blast the surrounding creep will suffer 16 points of damage!</p>
<p>Now the other great thing which is in this function, is that we can do all sorts of powers as well - This is where slow comes back into play.  We check if the tower has a &#8220;slow&#8221; marker on and apply that  slow percentage to all the surrounding creeps as well.  Hurting them by the blast, but also slowing them down.  We are going to use this again when we make some more complicated towers (like poison towers - you may have noticed I made some green towers off to the side but didn&#8217;t use them yet -*wink*- -*wink*-).</p>
<p>Ok so the man difference between the towers is as follows:</p>
<p><strong>CannonTower:</strong></p>
<blockquote><p>range = 120 (large distance)<br />
rate = 5 (not to fast at shooting)<br />
_root.fire(this, creep, 10, 1, 10, &#8220;projectile_cannonball&#8221;, 15, 0, 0);</p></blockquote>
<p>The cannon tower doesn&#8217;t do anything fancy - but it does do more damage then the fire and water towers on an individual projectile basis, but has a slower rate of fire.  It also has the largest range<br />
so far.</p>
<p><strong>Fire Tower:</strong></p>
<blockquote><p>range = 80 ( medium distance)<br />
rate = 2 ( faster shooting)<br />
_root.fire(this, creep, 10, 1, 10, &#8220;projectile_fire&#8221;, 5, 40, 0);</p></blockquote>
<p>The fire tower has the second highest damage amount (1/3 the damage of the Cannon Tower), but is able to splash damage in a wide area.  This means it works perfectly against a group of creeps that are packed together.</p>
<p><strong>*Water Tower:*</strong></p>
<blockquote><p>range = 80 ( medium distance)<br />
rate = 2 (faster shooting)<br />
_root.fire(this, creep, 10, 1, 10, &#8220;projectile_water&#8221;, 2, 10, 25);</p></blockquote>
<p>The water tower has the smallest range and damager, but it slows enemies by 25% as they pass by and is able to cause splash damage and thereby slow nearby enemies as well.</p>
<p>Excellent, look at that, you could already made a ton of combinations already and we&#8217;ve just started doing cool things with tower powers.</p>
<p>So what have we learned from this lesson:</p>
<ol>
<li>We learned how to create a creeps health bar</li>
<li>We created two new towers that we can click on and place</li>
<li>We created a tower that does &#8220;splash damage&#8221;</li>
<li>We also create a tower that slows enemies down as they pass</li>
</ol>
<p>We still have a long way to go to making a complete game, but it&#8217;s already shaping up to being a fun time.  We still need to upgrade the towers and give information about each as well as a score and gold!  We have a lot to do, but we&#8217;re moving fast.</p>
<p><strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-7-money-and-upgrades/">How to build a tower defense flash game – Part 7 - Gold and Upgrades!</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-6-tower-powers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 5 - Wave after Wave of Creep Kill&#8217;in)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-5-wave-after-wave-of-creep-killin/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-5-wave-after-wave-of-creep-killin/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:16:35 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=148</guid>
		<description><![CDATA[As of today, we&#8217;re bring it all together - Today is the day that the tower defense game will start to really look as it should.  Even thought this is an insanely interesting article, you really should read the other articles first - Most especially, How to build a tower defense flash game – [...]]]></description>
			<content:encoded><![CDATA[<p>As of today, we&#8217;re bring it all together - Today is the day that the tower defense game will start to really look as it should.  Even thought this is an insanely interesting article, you really should read the other articles first - Most especially, <strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack/">How to build a tower defense flash game – Part 4 - Starting the tower attack!</a></strong></p>
<p>In this article today we are going to do so many things:</p>
<ol>
<li>Creeps can now die</li>
<li>We will make *WAVES of CREEPS* with different groupings and speeds</li>
<li>We&#8217;ll add in some in game messaging</li>
<li>Lastly, creatures that make it all the way around will attack again</li>
</ol>
<p><span id="more-148"></span></p>
<p>Are you excited, well then lets get right into it.  This is what we&#8217;ll have at the end of the tutorial:</p>
<p><object width="550" height="400" data="http://www.goofballgames.com/wp-content/themes/androida/Part5.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androida/Part5.swf" /></object></p>
<p>Yeah, you&#8217;ll know how to make that at the end of this one article.  Each creep has an increasing number of hit points after each wave, starting at 40 and going up to the final boss on wave 6 at 6000.  Don&#8217;t worry I didn&#8217;t limit you with the burden of money yet, so you should have no problems.  Just place as many towers as you need to get the job done!</p>
<p><strong>Once again all the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part5.zip">downloaded by clicking here</a></strong>.</p>
<p>Ok before we go over the code, lets first quickly overview what I added and where.</p>
<p># In the instance of &#8220;Creep&#8221; &gt; &#8220;CreepList&#8221; I added 5 additional frames of Enemy images in the &#8220;Creeps&#8221; layer</p>
<ul>
<li>We now have three new functions &#8220;kill&#8221;, leak and inGameMessage</li>
<li>the function kill, removes the instance of the enemy and if it matches the number of kills for the wave it starts the next one</li>
<li>the function leak, starts the enemy back at the first waypoint once it gets to the last one</li>
<li>the function inGameMessage, records to the screen all text that calls it</li>
<li>We added the &#8220;_root&#8221; variables &#8220;kills&#8221;, &#8220;creepLeaks&#8221;, &#8220;waves&#8221; and &#8220;wave&#8221;</li>
<li>We added &#8220;health&#8221;, &#8220;maxhealth&#8221; and &#8220;isDead&#8221; as parameters to the enemies object.</li>
<li>We also added &#8220;damage&#8221; to the projectile.</li>
</ul>
<p>I know that looks like a lot, however, it makes logical sense - I swear.  Basically what we are doing is giving the creeps health and a way to take that health away from them (the projectiles).  Each time the projectile hits a given enemy it does a little bit of damage to it.  Once the enemies health falls below zero, we remove the instance of it.  To make this happen we add an additional parameter to the code in the &#8220;spawn&#8221; function such that when the creep is created we give it a certain amount of health.  Where do we get this parameter to put things in?  Good question, here it is</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/66.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This is where we customize our waves!  It&#8217;s a pretty straight forward multi dimensional array.  Right now we only concern ourselves with the number of attacking enemies, the health of them, the speed they travel at and the speed between releases.</p>
<p>It gives us the ability to do some pretty cool combinations.  How about Groups - Set the release time to something small.  How about fast enemies - set a high speed.  You want to make a boss?   Set the number to 1 and give it a ton of health. Done.</p>
<p>Now lets see how we put it all together.</p>
<p>Another important function that directly impacts how we handle the creeps.  The &#8220;fire&#8221; function is called we now pass in an additional parameter that accounts for the damage that projectile will do.</p>
<p>The code in the fire function is just this</p>
<ul>
<li>r.damage = damage;</li>
</ul>
<p>and later one in the &#8220;r&#8221; onEnterFrame, we do the check to see if we killed it with this shot - If we did we mark the &#8220;target enemy&#8221; as dead.  The below code is placed right between the call to &#8220;Explosions&#8221; and the &#8220;this.removeMovieClip()&#8221; for the projectile.</p>
<blockquote>
<pre>this.targ.health = this.targ.health - this.damage;
if (targ.health &lt;= 0)
{
	targ.isDead = 1;
}</pre>
</blockquote>
<p>Once we have this code in the projectile, it&#8217;s a trivial matter to put the check in the onEnterFrame for the creep itself to see if it&#8217;s health has reached zero as I have done, or if isDead=1.</p>
<blockquote>
<pre>if (this.health &lt;= 0)
{
        _root.kill(this);
}</pre>
</blockquote>
<p>So now wen the projectile removes the damage from the creep and his health falls below zero the creep calls the kill function and we do the clean up.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/62.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The kill function as describe above, simply removes the creep movie clip and increases the number of kills.  We make an additional check to see if that total number of kills matches the number of kills required to finish the level.  If this is the case, we call out &#8220;newLevel&#8221; function - A function we&#8217;ve used since the first tutorial.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/68.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The leak function will be used later on to have the player take damage.  Right now however, we use the function to update the position of the enemy and have them start back at the beginning of the waypoints.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/64.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This is how we post text to the screen and give updates to the player - It&#8217;s currently a simple log windows that keeps the most active text history at the top.  It is useful to give the player information about what is currently happening on the screen - What wave are we on?  What type of creeps are attacking? etc.  We&#8217;ll expand on this later as well.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/65.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This code directly impacts the towers ability to enemy targets.  We use the &#8220;&#8221; + creep == &#8220;&#8221; to determine if the creep that the tower that has targeted in fact is still alive.  If it has already been destroyed we would catch it here and set our target to zero and pick another enemy target.</p>
<p>Ok, that&#8217;s it for another tutorial - We still have a lot to do!  We still need to do upgrades, info, special towers, special creeps.  So look forward to another tutorial soon.</p>
<p><strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-6-tower-powers/">How to build a tower defense flash game – Part 6 - Tower Powers!</a><br />
</strong></p>
<p>You can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">click here</a> to go back to the introduction</p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-5-wave-after-wave-of-creep-killin/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 4 - Starting the tower attack)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:15:56 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=146</guid>
		<description><![CDATA[We&#8217;re back with another lesson in the never ending saga to create the perfect tower defense game. This is part 4 of the the series, you would be well advised to consult the first 3 tutorials in the series, How to build a tower defense flash game – Part 3 - Rotation and Realism before [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re back with another lesson in the never ending saga to create the perfect tower defense game. This is part 4 of the the series, you would be well advised to consult the first 3 tutorials in the series, <strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-3-rotation-and-realism/">How to build a tower defense flash game – Part 3 - Rotation and Realism</a></strong> before attempting this section. <span id="more-146"></span></p>
<p><strong>Once again all the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part4.zip">downloaded by clicking here</a></strong>.</p>
<p>Today we are going to charge right into the newest section in the series - &#8220;Starting the tower attack!&#8221;  This is one of the most important components in a tower defense game and will ultimately be one of the most enjoyable to program.  I will definitely not get to all the cooler aspects of attacking towers in this one section, but I promise that we will create all sorts of fun towers soon.  You will however, at the end of this particular tutorial, have enough knowledge to make any cool towers you can think of.  I&#8217;ve tried to make the codes robustness enough for that!</p>
<p>So swf file below is what we&#8217;ll have at the end of the day - Place a couple of towers around and see what happens</p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part4.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androidas/Part4.swf" /></object></p>
<p>Pretty cool huh.  The creeps are still indestructable, but you can shoot the hell out of them.</p>
<p>Ok, so to make things a little easier this time around, I&#8217;ll layout exactly what has changed from Part3 before I dive directly into the code.</p>
<ol>
<li>We&#8217;ve created two new symbols - &#8220;CannonBallProjectile&#8221; and &#8220;CannonBallExplosion&#8221;</li>
<li>We&#8217;ve created a new function called &#8220;Fire&#8221; in the main time lines action script code</li>
<li>We&#8217;ve created a new function called &#8220;Explosion&#8221; in the main time lines action script code</li>
<li>We&#8217;ve added a call to &#8220;Fire&#8221; within the onEnterFrame of the &#8220;creeps&#8221; time line.</li>
</ol>
<p>Pretty straight forward, I hope.</p>
<p>The two symbols &#8220;CannonBallProjectile&#8221; and &#8220;CannonBallExplosion&#8221; are shown in the picture below next to each other (right below the tower).  The symbols were given the instance names of  &#8220;projectile_cannonball&#8221; and &#8220;projectile_cannonball_explosions&#8221; respectively.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/58.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The symbol highlighted will act as our tower projectile or bullet.  A new projectile will be created every time the tower fires and will be destroyed after it comes within proximity of the creep it is tracking.  The explosion symbol would be considered &#8220;shrapnel&#8221; in the situation - If we had an area of effect, we could look into damaging the creeps around it.  For now it&#8217;s just looks like the bullet is ricocheting pieces off the side of the creep.</p>
<p>Now that we&#8217;ve gotten the symbols down and listed as to what they do, lets jump to the code.  We&#8217;ll start with the &#8220;Fire&#8221; procedure that is located in the main time line:</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/60.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>For now, the fire function takes in as a parameter the tower that is doing the firing, the creep being fired on, the starting speed of the projectile, the acceleration, the max speed of the projectile and the projectile clip itself.</p>
<p>We use the information to duplicate the projectile, in this case &#8220;CannonBallProjectile&#8221; is being passed in and all the parameters are mapped to the object being duplicated.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/61.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>So here is where most of the heavy lifting is done - It is the onEnterFrame for our projectile.  In essence what we expect our projectile do do are as follows:</p>
<ol>
<li>Recalculate your target every frame - if the target exists</li>
<li>Find the direction of the target based</li>
<li>move the projectile forward based on the direction and speed</li>
<li>Increase the speed of the projectile until it hits max velocity</li>
<li>If we come within a range of 10 of the creep, explode and remove the movie clip</li>
<li>Lastly, we have a specific range of projectiles - anymore than our max threshhold on the screen at one time and we start to lose projectiles.</li>
</ol>
<p>That&#8217;s it, that&#8217;s all the code does.</p>
<p>We made the call to Fire(&#8230;) in the &#8220;GunTowerPlacement&#8221; symbol&#8217;s action script code.  Here were the parameters I used:  _root.fire(this, creep, 10, 1, 10, &#8220;projectile_cannonball&#8221;);</p>
<p>Looking at the code, the great thing is that most of this code has been dealt with already - Think tower rotation and creep rotation and moving the creeps from one waypoint to the next.  The only new things is the call to Explosions which passes the &#8220;movieclip&#8221; name and the current x and y coordinate and the acceleration that gets added to the speed every round until it hits max speed.</p>
<p>We will revisit this function later in the series when we intend to add special abilities to the individual projectiles, but for now lets keep it simple.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/59.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Holy cow that looks complicated!  Well if by complicated you mean long, you&#8217;re right.  However, code wise, there is nothing we haven&#8217;t seen before so it&#8217;s pretty simple.</p>
<ol>
<li>We use a for loops to generate all the little pieces of debris in the explosions - We use 1 to 10 for now</li>
<li>We use our trusty &#8220;duplicateMovieclip&#8221; to make our 10 copies</li>
<li>We set some location information and some random directions</li>
<li>We set our onEnterFrame to handle moving in one direction for a time</li>
</ol>
<p>Of all that the only thing we haven&#8217;t really seen before is the reducing the alpha on the debris so that every frame it gets more and more transparent.  Once the alpha gets to 0 we remove the movie clip.  We also give the debris an initial speed and slowly reduce it making it seems like the explosion was pretty powerful.</p>
<p>The _root.explosionCount is just there to make sure we don&#8217;t put too much on the screen at any one time - We will play around with these &#8220;count&#8221; variables as we go more into the tower defense tutorials/series.  For now any more than 100 on the screen and we overwrite them.</p>
<p>Ok what have we learned:</p>
<blockquote><p>We now how fully loaded Towers!<br />
The towers themselves now have mini explosions when they hit their targets</p></blockquote>
<p>We have almost all we need for a full game - Then we can add more polish to the different towers (like slow/air/area of effect/poison/etc).  I almost can&#8217;t wait to start, however, first we need to do bring a little information into the tower defense game.  We can give some information and lets finally handle creep kill&#8217;in and multiple waves.  It&#8217;ll be great!</p>
<p>You can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">click here</a> to go back to the introduction</p>
<p><strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-5-wave-after-wave-of-creep-killin/">How to build a tower defense flash game – Part 5 - Wave after Wave of Creep Kill’in!</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 3 - Rotation and Realism)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-3-rotation-and-realism/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-3-rotation-and-realism/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:15:30 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=144</guid>
		<description><![CDATA[Welcome to part 3 of the tower defense series - Today we are going to be delving into the fine art of realism.  Nothing ruins the immersion of games like something that doesn&#8217;t make sense in the real world (unless that&#8217;s what your game is completely about.  Since most tower defense games are [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to part 3 of the tower defense series - Today we are going to be delving into the fine art of realism.  Nothing ruins the immersion of games like something that doesn&#8217;t make sense in the real world (unless that&#8217;s what your game is completely about.  Since most tower defense games are rooted in the real world, we need to make sure that the items in the game act like real object. <span id="more-144"></span></p>
<p>If you&#8217;ve been diligently following the past 2 part of the series (&#8221;part 1 - Waypoint and Enemies&#8221; and &#8220;part 2 - Placing towers&#8221;) you know that currently both the towers and the creeps don&#8217;t rotate.  Now that&#8217;s fine for hot air balloons and flying saucers, but people turn in the direction their either walking in or shooting in.  So to this end we have to add a few new abilities.</p>
<ul>
<li>Towers need to be able to target the nearest creep</li>
<li>Towers need to face in the direction of the creep they&#8217;re targeting</li>
<li>Creeps need to rotate when they move in a specific direction</li>
</ul>
<p>Before we begin, if you haven&#8217;t read <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-1-waypoints-and-enemies/">How to build a tower defense flash game – Part 1 - Waypoints and Enemies</a> or <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-2-placing-towers/">How to build a tower defense flash game – Part 2 - Placing Towers</a><br />
, you should go back and read them since this article builds on the ideas started there.</p>
<p><strong>Also, the source code for this tutorial can be <a href="http://goofballgames.com/wp-content/themes/androida/Part3.zip">Downloaded Here</a></strong>.</p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part3.swf" type="application/x-shockwave-flash"><param name="src" value="http://goofballgames.com/wp-content/themes/androida/Part3.swf" /></object></p>
<p>Well lets get right in it - To have a successful game we need to have a &#8220;somewhat&#8221; realistic enemy.  Movie people say something like &#8220;level of immersion&#8221; quality - If all of a sudden in the middle of the lord of the ring series Frodo and the gang where running from the black rider and you saw a boom mic over the black riders shoulder, you&#8217;d say &#8220;that&#8217;s not right - this movie sucks&#8221;.  As a matter of fact, you would be right.  The movie producer could have the best movie ever, but without keeping the movie goer engaged they realize they are just watching a movie.  An expensive movie.</p>
<p>As game developers we have a tough job ahead of us too, making a game that also engages the player. However, we don&#8217;t want to be too bogged down with the tedious stuff to make it real, making things real is expensive in time and energy.  So we do what true game developer do - we fake it.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/55.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This  code is added to the &#8220;spawn&#8221; function on the main timeline and is run every frame for the creep.  What it does is simple - It takes the current direction that we are looking in (we already know how far we had to travel based on radians) and add a bit of randomness.</p>
<ul>
<li>Math.sin((getTimer() + this.rnd * 100) / 100) * 7</li>
</ul>
<p>The above code returns a number between -1 and 1, which is then multiplies by 7 and added to the current direction (which is in degrees) to give the impression of unique movement.  What I mean is that we take the most direct path to the waypoint and then add a randomness of +7 or -7 to give it a some realism.  The creep will look partially in the direction that it is moving giving the impression of more expanded movement.</p>
<p>The &#8220;if&#8221; and &#8220;else if&#8221; statement are just to make sure that the roation stays between -10 and +10 of the rotation.  Again, we could have just had the code as &#8220;this.r = this.dir&#8221; and set that to the _rotation, but there is no fun in a completely straight line of troops following a path.  Always try to mix is up a bit!</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/56.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>So now we get to the fun - First we want to deal with how the tower picks a target.  So lets go straight to the code:</p>
<blockquote><p>++lastChecked;<br />
if (lastChecked &gt;= rate)<br />
{<br />
lastChecked = 0;</p></blockquote>
<p>Since there is no needs to slow everyone computer constantly checking if the tower has a enemy in it&#8217;s &#8220;range&#8221; - We keep a counter going for the last time this particular tower checked for new enemies.  This prevents from tedious &#8220;Math.sqrt&#8221; from being called once a frame for every tower we have out.</p>
<ul>
<li>if (target == 0 &amp;&amp; _root.creepArray.length &gt; 0)</li>
</ul>
<p>We also make a check for it the tower in fact alreay targeting a creep (the target variable would be &#8220;1&#8243;) or if the creepArray is empty (meaning that there are currently no enemies on the screen).</p>
<ul>
<li>for (i = 0; i &lt;= _root.creepArray.length; i++)</li>
</ul>
<p>Given that we don&#8217;t already have a target and that there are creeps on the map already, we can go ahead and traverse the array of creeps.</p>
<p>The variable &#8220;d&#8221; is the distance from the inidividual creep to the tower itself.  This is a basic version of the code we used to move a &#8220;creep&#8221; closer to a &#8220;waypoint&#8221;.  If the distance (the variable &#8220;d&#8221;) falls within the range of the tower we want the tower to track that enemy.</p>
<p><strong>Side note about targeting enemies</strong><br />
Depending on what container you use for your creep list (We use a simple array, but this is by no means the only way) and how you traverse that list you may have unexpected targeting issues - you could have your tower track only the furthest enemies by keeping a variable of the farthest creep range that fall in the range of the tower.  I will list just one</p>
<p>So since <strong>WE </strong>store the creep list in an array and since <strong>WE</strong> are traversing the list (i = 0, 1, 2, &#8230; , N) the code above will always track the last creep to appear in a tower range.  If you didn&#8217;t understand I mean assume that we have a tower has three creeps in its sight.  Once the first creep leaves the towers range, the tower will now be looking for a new creep to target.  The tower will target the next creep in the &#8220;for loop&#8221; next and recognize that he is the next one farther along - However, since we don&#8217;t stop traversing the list at that point the tower will eventually pick the farther one back.  Therefore, it is possible, given the code above, to actually have multiple towers along the road and have every other creep that comes out to never be targeted and therefore killed.</p>
<p>To solve this problem, we can put a &#8220;break;&#8221; in after line 39.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/57.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>In the next tutorial we will be combining both the first and forth article we will now add the ability to destroy the creeps.  You can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">Click Here</a> to go back to the introduction</p>
<p><strong>For the next tutorial our next subject is <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack/">How to build a tower defense flash game – Part 4 - Starting the tower attack!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-3-rotation-and-realism/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Build a Tower Defense Flash Game (Part 2 - Placing Towers)</title>
		<link>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-2-placing-towers/</link>
		<comments>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-2-placing-towers/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:14:50 +0000</pubDate>
		<dc:creator>Walter Reid</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.goofballgames.com/?p=142</guid>
		<description><![CDATA[Welcome to part 2 of the tower defense series - Today we&#8217;re going to be adding code to place towers.  Whoa, slow down there you say - &#8220;place towers? That&#8217;s crazy talk&#8221;.  I assure you it isn&#8217;t and once you are able to place towers in the next section we will combine our [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to part 2 of the tower defense series - Today we&#8217;re going to be adding code to place towers.  Whoa, slow down there you say - &#8220;place towers? That&#8217;s crazy talk&#8221;.  I assure you it isn&#8217;t and once you are able to place towers in the next section we will combine our lessons together to have a single level of play!</p>
<p><strong>If you haven&#8217;t read <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-1-waypoints-and-enemies/">How to build a tower defense flash game – Part 1 - Waypoints and Enemies</a>, you may want to review it before you continue on</strong> <span id="more-142"></span></p>
<p>Before I get ahead of myself, we still need to finish this task in the series! <strong>You can <a href="http://goofballgames.com/wp-content/themes/androida/Part2.zip">Click Here</a> for all the material I present for part 2 of this tower defense series.</strong></p>
<p><strong>Getting Started Placing Towers</strong></p>
<p>Ok, so one thing you may have learned from the <a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-1-waypoints-and-enemies/">last article</a> was that making games isn&#8217;t like real life - it just need to act like real life.  We didn&#8217;t need a complicated A*, DFS/BFS  or Best-first searches to move our enemies around.  In fact we didn&#8217;t need a tile based map at all.  That doesn&#8217;t mean we still won&#8217;t need one in the future, but for now we follow the K.I.S.S system (keep it simple, stupid).</p>
<p>So lets take a look at what we are trying to recreate.  Try to place the tower and see how it works - Please forgive me for my poor &#8220;gun tower&#8221; skills!</p>
<p><object width="550" height="400" data="http://goofballgames.com/wp-content/themes/androida/Part2.swf" type="application/x-shockwave-flash"><param name="src" value="<br />
http://goofballgames.com/wp-content/themes/androida/Part2.swf" /></object><br />
<strong>Click on the little gun in the middle of the screen to select the tower and then place it on the green square in the middle of the map.</strong></p>
<p>When the user clicks the &#8220;gun tower&#8221; at the bottom of the screen a white square attaches itself to the mouse and marks the locations that can be built on.  This &#8220;marker&#8221; symbol is white when the user can build a tower in the area and red when the user can&#8217;t.  The tower also has its own respective range which is also visible after the user selects the tower.   This &#8220;range&#8221; symbol works much like the marker symbol in that it has it&#8217;s own red/white components.  Now to the exciting part - If the user clicks the mouse on the &#8220;grass&#8221; we place one &#8220;gun tower&#8221;.  If the user clicks anywhere else, we assume that the user didn&#8217;t want to click that tower in the first place and remove the tower placing option.</p>
<p>We&#8217;ve created a a total of eleven symbols to help our cause</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/45.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>* DeselectionArea (Graphic - On Frame 1 of &#8220;DeselectionAreaContainer&#8221;)<br />
* DeselectionAreaContainer (Instance name: &#8220;deselect&#8221;)</p>
<p>The Deselection area is just a movieclip with an instance name on the stage that we can &#8220;hitTest&#8221; against to find if the user clicked an invalid area on or off the map.  This is used both when placing a tower and again when the user selects the towers that has already been placed.</p>
<p><strong>Grass (Instance name: &#8220;grass&#8221;)</strong></p>
<p>This is the map we are using - Since it was green I called it grass, that&#8217;s pretty much as simple as it gets.  It works like the deselect area, but used to correctly identify area where a tower can be placed.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/46.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>GunTower (MovieClip that acts like a button)</strong></p>
<p>This movie clip acts like a button when pressed - It could have easily been one but there was really no need for roll over states.  It contains very basic actionscript code that I will list out below:</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/50.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>This symbol when clicked will modify the object params of our other symbol, &#8220;marker&#8221;, that lives just off the stage to the left.  If makes that symbol &#8220;active&#8221; and tell it that we are dealing with a tower of type &#8220;gun&#8221;.  That will come into place when the tower is being placed.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/47.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>GunTowerPlacement (Instance name: &#8220;tower_gun&#8221;)</strong></p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/51.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The function &#8220;TowerInfo&#8221; is defined in action script code on the main time line.  It handles the showing of towers that have already been reselected off of the map.  It just redisplays the towers individual range and is listed for your convenience below:</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/54.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The &#8220;GunTowerPlacement&#8221; symbol is the movie clip that ultimately get placed on the stage after the user clicks on the &#8220;grass&#8221;.  This movie clip will contain more code relating to the targeting of the enemies and</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/48.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>TowerPlacementMarker (Instance name: &#8220;marker&#8221;)</strong></p>
<p>The &#8220;TowerPlacementMarker&#8221; is the symbol that contains 2 additional Graphic elements on two of it&#8217;s frames.  When this is made active by clicking on the &#8220;GunTower&#8221; the marker tracks to the mouse.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/52.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>The code above is extra to handle the movement of the marker to mimic &#8220;tiles&#8221;, but that is just there to later prevent overlapping towers.  We could just have it track directly to the &#8220;_xmouse&#8221; and &#8220;_ymouse&#8221;, but it wouldn&#8217;t look at nice when laying out the towers.</p>
<p>We then make hitTest on the &#8220;grass&#8221; movie clip to show either a white box or a red one.  The white means that the current coordinate is &#8220;ok&#8221; for placing a new tower and a red one means it is not.</p>
<p>Those two clips are located in the symbols listed below:</p>
<ul>
<li>TowerPlacementNotOk (Graphic - On Frame 2 of &#8220;TowerPlacementMarker&#8221;)</li>
<li>TowerPlacementOk (Graphic - On Frame 1 of &#8220;TowerPlacementMarker&#8221;)</li>
</ul>
<p>The last think associated with the &#8220;TowerPlacementMarker&#8221; is when it is pressed.  This is the interesting part if you ask me.  There are two conditions.</p>
<ul>
<li>Has the user clicked on the &#8220;grass&#8221; movie clip - OR</li>
<li>Has the user clicked on the &#8220;deselect&#8221; movie clip</li>
</ul>
<p>If the user clicks on either &#8220;deselect&#8221; or &#8220;grass&#8221; movie clips we will remove the marker (the white/red box) and the range (the white/red circle) instances and move them to off stage left.</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/53.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>If the user clicks on the &#8220;grass&#8221; movie clip it means that we are clicking on a valid build location.  We then increments the towerCount by one and then check the &#8220;tower&#8221; parameter of the &#8220;TowerPlacementMarker&#8221; object.  This &#8220;tower&#8221; parameter will ultimately be the tower type that we want to place on the screen.  In the example we used &#8220;gun&#8221;.  So we attempt to duplicate a tower off the stage with the name &#8220;tower_&#8221; + &#8220;gun&#8221; and the one instance name that matches that is &#8220;GunTowerPlacement&#8221;.</p>
<p>We then set the proper _x and _y and set active equal to 1.  The tower is now online and operational!</p>
<table style="text-align:left;" border="0">
<tbody>
<tr>
<td align="left"><img src="http://www.goofballgames.com/wp-content/uploads/2010/01/49.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>* TowerPlacementRange (Instance name: &#8220;ranger&#8221;)</p>
<p>The &#8220;TowerPlacementRange&#8221; is set purely for a visual component of the tower when lacing it on the screen.  The width and height are set when &#8220;TowerPlacementMarker&#8221; is made active.  The two components contained in this symbol listed below in their respective frames.</p>
<p>* TowerRanceNotOk (Graphic - On Frame 2 of &#8220;TowerPlacementRange&#8221;)<br />
* TowerRangeOk (Graphic - On Frame 1 of &#8220;TowerPlacementRange&#8221;)</p>
<p>Ok, so what do we know how to do now?</p>
<p># We have a basic button that creates a pseudo mask of the tower we want to place<br />
# We can distinguish between buildable and non-buildable areas of a map<br />
# We can place an object on the map and make it active<br />
# We can click on that place object and get the object current tower range</p>
<p>Wasn&#8217;t that simple?  I hope you found this second part of the series as helpful as the first part.  If you have any additional questions, please feel to ask away and I&#8217;ll see if I can help!</p>
<p>In the next tutorial we will be combining both the first and second articles and adding rotation and realism.  You can <a href="http://www.goofballgames.com/2010/01/30/building-a-tower-defense-game-in-flash-as2/">Click Here</a> to go back to the introduction</p>
<p><strong><a href="http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-3-rotation-and-realism/">How to build a tower defense flash game – Part 3 - Rotation and Realism</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-2-placing-towers/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
