{"id":1459,"date":"2009-02-28T08:06:41","date_gmt":"2009-02-28T12:06:41","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1459"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as3-part-6","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as3-part-6\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 6"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3\/\">Setting up Level<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3-part-2\/\">Adding Turrets<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3-part-3\/\">Adding Enemies<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3-part-4\/\">Making Turrets Attack Enemies<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3-part-5\/\">Winning\/Losing the Game<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3-part-6\/\">Expanding on the Game<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/02\/tutorial-create-a-tower-defense-game-in-as3-part-7\/\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3>Step 6: Expanding on the Game<\/h3>\n<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>\n<p>The first thing we&#8217;re going to do before creating stronger enemies is to make the enemies give you money when you kill them. This will be easy. Just open up &#8220;Enemy.as&#8221; and find this code in <tt>eFrameEvents()<\/tt>:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/destroy this if health is equal to or below 0\r\nif(health <= 0){\r\n\tdestroyThis();\r\n}\r\n<\/pre>\n<p>Just add the following code to the if statement:<\/p>\n<pre lang=\"actionscript\">\r\n_root.money += 5;\r\n<\/pre>\n<p>Now, we can continue on to making better enemies. Open up \"source.fla\" and find the <tt>makeEnemies<\/tt> function. We're going to have some major renovations to this functions. Just replace the function with this new code:<\/p>\n<pre lang=\"actionscript\">\r\nfunction makeEnemies():void{\/\/this function will add enemies to the field\r\n\tif(enemyTime < enemyLimit){\/\/if it isn't time to make them yet\r\n\t\tenemyTime ++;\/\/then keep on waiting\r\n\t} else {\/\/otherwise\r\n\t\tvar theCode:int = enemyArray[currentLvl-1][currentEnemy];\/\/get the code from the array\r\n\t\tif(theCode != 0){\/\/if it isn't an empty space\r\n\t\t\tvar newEnemy:Enemy = new Enemy(theCode);\/\/then create a new enemy and pass in the code\r\n\t\t\tenemyHolder.addChild(newEnemy);\/\/and add it to the enemyholder\r\n\t\t}\r\n\t\tcurrentEnemy ++;\/\/move on to the next enemy\r\n\t\tenemyTime = 0;\/\/and reset the time\r\n\t}\r\n}\r\n<\/pre>\n<p>Okay, so the renovations weren't too major. The only thing we changed was that instead of checking for the code of \"1\", we check for any code that isn't equal to \"0\". Then, we pass that value into the Enemy class. <\/p>\n<p>We'll also have to make some changes in the <tt>startGame()<\/tt> functions. Don't worry, they'll be just as minor as the ones we just made. Replace the code inside of the function with this:<\/p>\n<pre lang=\"actionscript\">\r\nfunction startGame():void{\/\/we'll run this function every time a new level begins\r\n\tfor(var i:int=0;i<enemyArray[currentLvl-1].length;i++){\r\n\t\tif(enemyArray[currentLvl-1][i] != 0){\r\n\t\t\tenemiesLeft ++;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>The only thing we changed here was that now we check for all numbers not equal to 0, instead of just counting those set as 1.<\/p>\n<p>Of course, now we're going to have to make some changes to the Enemy class. Open up \"Enemy.as\" and find the topmost code where we define the variables and define the <tt>Enemy()<\/tt> function. Just replace that with this:<\/p>\n<pre lang=\"actionscript\">\r\nprivate var _root:MovieClip;\r\npublic var xSpeed:int;\/\/how fast it's going horizontally\r\npublic var ySpeed:int;\/\/how fast it's going vertically\r\npublic var maxSpeed:int = 3;\/\/how fast it can possibly go\r\npublic var health:int;\r\npublic var level:int;\/\/this will be set to the number passed in\r\n\r\npublic function Enemy(code:int){\r\n\tthis.addEventListener(Event.ADDED, beginClass);\r\n\tthis.addEventListener(Event.ENTER_FRAME, eFrameEvents);\r\n\t\r\n\tlevel = code;\/\/set the level to the value passed in for use in other functions\r\n}\r\n<\/pre>\n<p>Not too many changes have been made. Now, we're using that variable that was passed into the <tt>Enemy()<\/tt> function and making it usable. We're also making the health undefined so we can change it based on the level. In fact, let's change them now. Add this to the top of the <tt>beginCode()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\nhealth = level*5;\r\n<\/pre>\n<p>This will set the health based on the level of the enemy. Next, let's make him worth a bit more points, shall we? Find the code that we added in the beginning of the tutorial. Simply replace it with this:<\/p>\n<pre lang=\"actionscript\">\r\n_root.money += 5*level;\r\n<\/pre>\n<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>\n<pre lang=\"actionscript\">\r\nenemyArray = [\/\/defining the array\r\n\t\t\t[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\r\n\t\t\t[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\r\n\t\t\t[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],\r\n\t\t\t[100],\r\n\t\t\t[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],\r\n\t\t\t[250,250,250]\r\n\t\t\t  ];\r\n<\/pre>\n<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>\n<h4>Final Product<\/h4>\n<p><center><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"550\" height=\"400\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt6\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt6\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt6\/tower-defense-as3-source.zip\">Source Files (Zipped)<\/a><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[160,5,8,4,6],"tags":[25,8,246,19,18,245,247,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1459"}],"collection":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/comments?post=1459"}],"version-history":[{"count":4,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1459\/revisions"}],"predecessor-version":[{"id":1472,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1459\/revisions\/1472"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1459"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}