{"id":1531,"date":"2009-04-25T08:06:07","date_gmt":"2009-04-25T12:06:07","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1531"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as2-part-6","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as2-part-6\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 6"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2\/\">Setting up Level<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2-part-2\/\">Adding Turrets<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2-part-3\/\">Adding Enemies<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2-part-4\/\">Making Turrets Attack Enemies<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2-part-5\/\">Winning\/Losing the Game<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2-part-6\/\">Expanding on the Game<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/04\/tutorial-create-a-tower-defense-game-in-as2-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>Open up &#8220;source.fla&#8221; and find the <tt>makeEnemies<\/tt> function. We&#8217;re going to have some major renovations to this functions. Just replace the function with this new code:<\/p>\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:Number = enemyArray[currentLvl-1][currentEnemy];\/\/get the code from the array\r\n\t\tif(theCode != 0 &#038;&#038; theCode != null){\/\/if it's not set at 0\r\n\t\t\t\/\/then create a new enemy and add it to the enemy holder\r\n\t\t\tenemyHolder.createEmptyMovieClip('enemy'+currentEnemy,enemyHolder.getNextHighestDepth());\r\n\t\t\t\/\/now we're going to draw the enemy. It'll just be a tiny red circle\r\n\t\t\tenemyHolder['enemy'+currentEnemy].beginFill(0xFF0000);\/\/coloring them red gray\r\n\t\t\tenemyHolder['enemy'+currentEnemy].moveTo(0, 2.5);\/\/move the entire shape a certain way\r\n\t\t\t\/\/create 4 curves so that it'll look like a circle\r\n\t\t\tenemyHolder['enemy'+currentEnemy].curveTo(0,10,5,10);\r\n\t\t\tenemyHolder['enemy'+currentEnemy].curveTo(10,10,10,5);\r\n\t\t\tenemyHolder['enemy'+currentEnemy].curveTo(10,0,5,0);\r\n\t\t\tenemyHolder['enemy'+currentEnemy].curveTo(0,0,0,5);\r\n\t\t\tenemyHolder['enemy'+currentEnemy].endFill();\/\/end the fill\r\n\t\t\t\r\n\t\t\t\/\/add a few variables to the enemy\r\n\t\t\tenemyHolder['enemy'+currentEnemy].enLevel = theCode;\/\/setting its level to be what # it is\r\n\t\t\tenemyHolder['enemy'+currentEnemy].maxSpeed = 3;\/\/how fast it can possibly go\r\n\t\t\tenemyHolder['enemy'+currentEnemy].xSpeed = 0;\r\n\t\t\tenemyHolder['enemy'+currentEnemy].ySpeed = 0;\r\n\t\t\tenemyHolder['enemy'+currentEnemy].health = 5*theCode;\r\n\t\t\t\r\n\t\t\t\/\/checking what the start direction is\r\n\t\t\tif(_root.startDir == 'UP'){\/\/if it's starting up\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._y = 300;\/\/set the y value off the field\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._x = _root.startCoord;\/\/make the x value where it should be\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].xSpeed = 0;\/\/make it not move horizontally\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].ySpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;\/\/make it move upwards\r\n\t\t\t} else if(_root.startDir == 'RIGHT'){\/\/and so on for other directions\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._x = -25;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._y = _root.startCoord;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].xSpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].ySpeed = 0;\r\n\t\t\t} else if(_root.startDir == 'DOWN'){\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._y = -25;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._x = _root.startCoord;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].xSpeed = 0;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].ySpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;\r\n\t\t\t} else if(_root.startDir == 'LEFT'){\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._x = 550;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy]._y = _root.startCoord;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].xSpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;\r\n\t\t\t\tenemyHolder['enemy'+currentEnemy].ySpeed = 0;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tenemyHolder['enemy'+currentEnemy]._x += 5;\/\/fixing the x value\r\n\t\t\tenemyHolder['enemy'+currentEnemy]._y += 5;\/\/fixing up the y value\r\n\t\t\t\r\n\t\t\tenemyHolder['enemy'+currentEnemy].onEnterFrame = function(){\/\/give it some functions\r\n\t\t\t\tthis._x += this.xSpeed;\r\n\t\t\t\tthis._y += this.ySpeed;\r\n\t\t\t\t\r\n\t\t\t\t\/\/checking what direction it goes when finishing the path\r\n\t\t\t\tif(_root.finDir == 'UP'){\/\/if it finishes at the top\r\n\t\t\t\t\tif(this._y <= -25){\/\/if the y value is too high\r\n\t\t\t\t\t\t_root.lives --;\/\/take away a life\r\n\t\t\t\t\t\t_root.money -= 5*this.enLevel;\/\/don't let the player gain any money\r\n\t\t\t\t\t\tthis.removeMovieClip();\/\/take it away from the stage\r\n\t\t\t\t\t}\r\n\t\t\t\t} else if(_root.finDir == 'RIGHT'){\/\/and so on for other directions\r\n\t\t\t\t\tif(this._x >= 550){\r\n\t\t\t\t\t\t_root.lives --;\r\n\t\t\t\t\t\t_root.money -= 5*this.enLevel;\r\n\t\t\t\t\t\tthis.removeMovieClip();\r\n\t\t\t\t\t}\r\n\t\t\t\t} else if(_root.finDir == 'DOWN'){\r\n\t\t\t\t\tif(this._y >= 300){\r\n\t\t\t\t\t\t_root.lives --;\r\n\t\t\t\t\t\t_root.money -= 5*this.enLevel;\r\n\t\t\t\t\t\tthis.removeMovieClip();\r\n\t\t\t\t\t}\r\n\t\t\t\t} else if(_root.startDir == 'LEFT'){\r\n\t\t\t\t\tif(this._x <= 0){\r\n\t\t\t\t\t\t_root.lives --;\r\n\t\t\t\t\t\t_root.money -= 5*this.enLevel;\r\n\t\t\t\t\t\tthis.removeMovieClip();\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\tif(this.health <= 0){\r\n\t\t\t\t\t_root.enemiesLeft --;\r\n\t\t\t\t\t_root.money += 5*this.enLevel;\r\n\t\t\t\t\tthis.removeMovieClip();\r\n\t\t\t\t}\r\n\t\t\t}\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>There aren't very many renovations that we've made, but they will make our code much more flexible. It allows us to create different enemy levels by setting the <tt>enLevel<\/tt> to be equal to the code that is placed into the enemy array. The <tt>enLevel<\/tt> in turn lets us dynamically change the amount of health the enemy has and the amount of money that it gives you when you kill it. Right now the health and money it gives you is 5 times the enemy level.<\/p>\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-as2\/pt6\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt6\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt6\/tower-defense-as2-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,7,4,6],"tags":[25,7,246,19,18,245,247,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1531"}],"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=1531"}],"version-history":[{"count":3,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions"}],"predecessor-version":[{"id":1541,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions\/1541"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1531"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}