{"id":1447,"date":"2009-02-28T08:05:30","date_gmt":"2009-02-28T12:05:30","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1447"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as3-part-5","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as3-part-5\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 5"},"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 class=\"c_chap\"><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><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 5: Winning\/Losing the Game<\/h3>\n<p>Welcome back to the 5th installment of this tutorial series. In this lesson, we&#8217;ll make some playable levels, along with a winning and losing situation. Let&#8217;s dig in, shall we?<\/p>\n<p>Lets start off by opening up the main source file. Find where this code is:<\/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[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],\/\/another row means another level\r\n\t\t\t[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]\r\n\t\t\t  ];\r\n<\/pre>\n<p>Let&#8217;s review this code for a bit. Each of those arrays within that <tt>enemyArray<\/tt> represents a level. So, right now, we&#8217;re set up for 3 different levels, with an increasing amount of enemies in each level. Now that we&#8217;ve covered this, we can continue on to making it possible to advance levels.<\/p>\n<p>In order to do this, we must open up &#8220;Enemy.as&#8221;. Find the <tt>destroyThis()<\/tt> function. Add this code to the bottom.<\/p>\n<pre lang=\"actionscript\">\r\n_root.enemiesLeft --;\r\n<\/pre>\n<p>Next, we&#8217;ll have to go back to &#8220;source.fla&#8221;. Find the <tt>eFrame()<\/tt> function. There should only be one line of code in there right now. We&#8217;re going to add some. Add the following code to the bottom of the <tt>eFrame()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\nif(enemiesLeft==0){\/\/if there are no more enemies left\r\n\tcurrentLvl ++;\/\/continue to the next level\r\n\tcurrentEnemy = 0;\/\/reset the amount of enemies there are\r\n\tstartGame();\/\/restart the game\r\n}\r\n<\/pre>\n<p>Now, try testing out the game. Functionally, it&#8217;s working 100%. Practically, it&#8217;s not a great game. In order to make this game better, we&#8217;re going to have to show some information to the user while they&#8217;re playing, like the current level, the score, etc. This is exactly what we&#8217;re going to do now.<\/p>\n<p>Create four dynamic text boxes at the bottom left portion of the stage, each at 12 point font. Here&#8217;s an example of what yours should look like, with an example of what information we&#8217;re going to put into them later:<\/p>\n<p><center><img loading=\"lazy\" src=\"http:\/\/www.mrsunstudios.com\/wp-content\/uploads\/2009\/02\/textboxes.gif\" alt=\"The Text Fields\" title=\"The Text Fields\" width=\"146\" height=\"88\" class=\"size-full wp-image-1450\" \/><\/center><\/p>\n<p>Got that? Now, we just have to give each of these dynamic text fields an instance name. Label them accordingly:<\/p>\n<ul>\n<li><tt>txtLevel<\/tt><\/li>\n<li><tt>txtMoney<\/tt><\/li>\n<li><tt>txtLives<\/tt><\/li>\n<li><tt>txtEnemiesLeft<\/tt><\/li>\n<\/ul>\n<p>Now, let&#8217;s dive into some code, shall we?<\/p>\n<p>The first thing we need to do is actually define the <tt>money<\/tt> and the <tt>lives<\/tt> variables. Just add this code to the top:<\/p>\n<pre lang=\"actionscript\">\r\nvar money:int=100;\/\/how much money the player has to spend on turrets\r\nvar lives:int=20;\/\/how many lives the player has\r\n<\/pre>\n<p>Okay, good stuff. Let&#8217;s first make these two variables mean something before we update the text. We first have to make it so the player loses lives so he\/she can lose the game. Open up &#8220;Enemy.as&#8221; and find this code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking what direction it goes when finishing the path\r\nif(_root.finDir == 'UP'){\/\/if it finishes at the top\r\n\tif(this.y <= -25){\/\/if the y value is too high\r\n\t\tdestroyThis();\/\/then remove this guy from the field\r\n\t}\r\n} else if(_root.finDir == 'RIGHT'){\/\/and so on for other directions\r\n\tif(this.x >= 550){\r\n\t\tdestroyThis();\t\t\t\t\t\r\n\t}\r\n} else if(_root.finDir == 'DOWN'){\r\n\tif(this.y >= 300){\r\n\t\tdestroyThis();\r\n\t}\r\n} else if(_root.startDir == 'LEFT'){\r\n\tif(this.x <= 0){\r\n\t\tdestroyThis();\r\n\t}\r\n}\r\n<\/pre>\n<p>All we have to do is make the player lose a life after all of these conditionals. It should look like this now:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking what direction it goes when finishing the path\r\nif(_root.finDir == 'UP'){\/\/if it finishes at the top\r\n\tif(this.y <= -25){\/\/if the y value is too high\r\n\t\tdestroyThis();\/\/then remove this guy from the field\r\n\t\t_root.lives --;\/\/take away a life\r\n\t}\r\n} else if(_root.finDir == 'RIGHT'){\/\/and so on for other directions\r\n\tif(this.x >= 550){\r\n\t\tdestroyThis();\t\t\t\t\t\r\n\t\t_root.lives --;\r\n\t}\r\n} else if(_root.finDir == 'DOWN'){\r\n\tif(this.y >= 300){\r\n\t\tdestroyThis();\r\n\t\t_root.lives --;\r\n\t}\r\n} else if(_root.startDir == 'LEFT'){\r\n\tif(this.x <= 0){\r\n\t\tdestroyThis();\r\n\t\t_root.lives --;\r\n\t}\r\n}\r\n<\/pre>\n<p>Next, we'll make each of the turrets cost a certain amount of money. I'm thinking $20 is a good price. Open up \"EmptyBlock.as\" and find the <tt>thisClick<\/tt> function. Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\nprivate function thisClick(e:MouseEvent):void{\r\n\tif(_root.money >= 20){\/\/if the player has enough money\r\n\t\t_root.makeTurret(this.x,this.y);\/\/make the turret\r\n\t\t\/\/remove all the listeners so it can't be clicked on again\r\n\t\tthis.buttonMode = false;\r\n\t\tthis.graphics.beginFill(0x333333);\r\n\t\tthis.graphics.drawRect(0,0,25,25);\r\n\t\tthis.graphics.endFill();\r\n\t\tthis.removeEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);\r\n\t\tthis.removeEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);\r\n\t\tthis.removeEventListener(MouseEvent.CLICK, thisClick);\r\n\t\t\r\n\t\t_root.money -= 20; \/\/spend the money\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, we can update these text fields. Once again, find the <tt>eFrame()<\/tt> function in \"source.fla\". Add the following code which will update all the text fields with the needed information:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/Updating the text fields\r\ntxtLevel.text = 'Level '+currentLvl;\r\ntxtMoney.text = '$'+money;\r\ntxtLives.text = 'Lives: '+lives;\r\ntxtEnemiesLeft.text = 'Enemies Left:  '+enemiesLeft;\r\n<\/pre>\n<p>Now, let's create some winning and losing scenarios for the player. When the player has beaten all of the levels, then a win screen should show up. The same thing should happen with a lose screen when the player loses all of his\/her lives. Let's create these two frames, shall we?<\/p>\n<p>Before, we create the frames, we have to add a layer called \"labels\". This will just let us easily navigate to the required frames. Next, create a frame labeled \"win\" and add whatever message you want to inform the player that they've won. Do the same with a \"lose\" frame.<\/p>\n<p>Now, we have to make it possible for the player to restart the game. We'll let them do it by clicking anywhere on the screen. Copy and paste this code to the \"win\"  frame.<\/p>\n<pre lang=\"actionscript\">\r\nstage.addEventListener(MouseEvent.CLICK, restartGame);\/\/adding a mouse event listener for clicking the stage\r\nfunction restartGame(e:MouseEvent):void{\r\n\tgotoAndStop(1);\/\/go to the first frame\r\n\tstage.removeEventListener(MouseEvent.CLICK, restartGame);\/\/remove the listener\r\n}\r\n<\/pre>\n<p>We don't need to define the function again in the \"lose\" frame, so you can just add this code to it:<\/p>\n<pre lang=\"actionscript\">\r\nstage.addEventListener(MouseEvent.CLICK, restartGame);\/\/adding a mouse event listener for clicking the stage\r\n<\/pre>\n<p>All right, the next thing we have to do is navigate to the desired frame when the player wins or loses. Go back to \"source.fla\" and find the <tt>eFrame()<\/tt> function. Add the following code to the <strong>very beginning<\/strong> of it:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if there aren't any levels left\r\nif(currentLvl > enemyArray.length){\r\n\tgameOver=true;\/\/set the game to be over\r\n\t\r\n\t\/\/reset all the stats\r\n\tcurrentLvl = 1;\r\n\tcurrentEnemy = 0;\r\n\tenemyTime = 0;\r\n\tenemyLimit = 12;\r\n\tenemiesLeft = 0;\r\n\t\r\n\tremoveEventListener(Event.ENTER_FRAME, eFrame);\/\/remove this listener\r\n\tremoveChild(roadHolder);\/\/remove the pieces of road\r\n\tgotoAndStop('win');\/\/go to the win frame\r\n}\r\nif(lives<=0){\/\/if the user runs out of lives\r\n\tgameOver=true;\/\/set the game to be over\r\n\t\r\n\t\/\/reset all the stats\r\n\tcurrentLvl = 1;\r\n\tcurrentEnemy = 0;\r\n\tenemyTime = 0;\r\n\tenemyLimit = 12;\r\n\tenemiesLeft = 0;\r\n\t\r\n\tremoveEventListener(Event.ENTER_FRAME, eFrame);\/\/remove this listener\r\n\tremoveChild(roadHolder);\/\/remove the pieces of road\r\n\tgotoAndStop('lose');\/\/go to the lose frame\r\n}\r\n<\/pre>\n<p>Sweet. This concludes this installment of the tutorial series. Join us next time when we expand on the 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\/pt5\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt5\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt5\/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 5: Winning\/Losing the Game Welcome back to the 5th installment of this tutorial series. In this lesson, we&#8217;ll make some playable levels, along with a winning and losing situation. Let&#8217;s dig [&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\/1447"}],"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=1447"}],"version-history":[{"count":11,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1525,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1447\/revisions\/1525"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}