{"id":1481,"date":"2009-04-25T08:01:32","date_gmt":"2009-04-25T12:01:32","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1481"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as2\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS2"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li class=\"c_chap\"><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><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 1: Setting Up a Level<\/h3>\n<p>The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always very fun to play. I am here to walk you through the creation of one of these games. Let us begin, shall we?<\/p>\n<p>In this section of the tutorial, we&#8217;re going to set up the roads and stuff onto the stage. However, the first thing we need to do is create a blank flash document with a black background with the frames per second set at 24.<\/p>\n<p>Now, we can dive into some code. Let&#8217;s first create some code that will lay down the track for the enemies to go through. Create a new layer called &#8220;actions&#8221; and add the following code to it:<\/p>\n<pre lang=\"actionscript\" line=\"1\">\r\nstop();\r\n\r\n\/\/setting vars to step in for turns and special blocks\r\nvar S:String = 'START';\r\nvar F:String = 'FINISH';\r\nvar U:String = 'UP';\r\nvar R:String = 'RIGHT';\r\nvar D:String = 'DOWN';\r\nvar L:String = 'LEFT';\r\n\r\nvar startDir:String;\/\/the direction the enemies go when they enter\r\nvar finDir:String;\/\/the direction the enemies go when they exit\r\nvar startCoord:Number;\/\/the coordinates of the beginning of the road\r\nvar lvlArray:Array = new Array();\/\/this array will hold the formatting of the roads\r\n\r\nlvlArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t\t\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t\t\t0,0,0,0,R,1,1,D,0,0,R,1,1,D,0,0,R,1,1,D,0,0,\r\n\t\t\t0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,\r\n\t\t\t0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,\r\n\t\t\tS,D,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,R,1,F,\r\n\t\t\t0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,\r\n\t\t\t0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,\r\n\t\t\t0,R,1,1,U,0,0,R,1,1,U,0,0,R,1,1,U,0,0,0,0,0,\r\n\t\t\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t\t\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t\t\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r\n\t\t\t];\r\n\r\n\/\/the names of these variables explain what they do\r\nvar currentLvl:Number = 1;\r\nvar gameOver:Boolean = false;\r\n\r\nfunction startGame():Void{\/\/we'll run this function every time a new level begins\r\n\t\/\/right now we don't have any code\r\n}\r\n\r\n_root.createEmptyMovieClip('roadHolder',_root.getNextHighestDepth());\/\/add a MovieClip to hold the road\r\nfunction makeRoad():Void{\r\n\tvar row:Number = 0;\/\/the current row we're working on\r\n\tvar block;\/\/this will act as the block that we're placing down\r\n\tfor(i=0;i<lvlArray.length;i++){\/\/creating a loop that'll go through the level array\r\n\t\tif(lvlArray[i] == 0){\/\/if the current index is set to 0 \r\n\t\t\t\/\/\/\/\/*****EMPTY BLOCK*****\/\/\/\/\/\r\n\t\t\t_root.createEmptyMovieClip('block'+i,_root.getNextHighestDepth());\/\/create a gray empty block\r\n\t\t\t_root['block'+i].beginFill(0x333333);\r\n\t\t\t_root['block'+i].lineTo(0,0);\r\n\t\t\t_root['block'+i].lineTo(0,25);\r\n\t\t\t_root['block'+i].lineTo(25,25);\r\n\t\t\t_root['block'+i].lineTo(25,0);\r\n\t\t\t_root['block'+i].endFill();\r\n\t\t\t\/\/and set the coordinates to be relative to the place in the array\r\n\t\t\t_root['block'+i]._x= (i-row*22)*25;\r\n\t\t\t_root['block'+i]._y = row*25;\r\n\t\t} else if(lvlArray[i] == 1){\/\/if there is supposed to be a row\r\n\t\t\t\/\/\/\/\/*****EMPTY ROAD*****\/\/\/\/\/\r\n\t\t\t\/\/just add a box that will be a darker color and won't have any actions\r\n\t\t\troadHolder.createEmptyMovieClip('block'+i,roadHolder.getNextHighestDepth());\r\n\t\t\troadHolder['block'+i].beginFill(0x111111);\r\n\t\t\troadHolder['block'+i].lineTo(0,0);\r\n\t\t\troadHolder['block'+i].lineTo(0,25);\r\n\t\t\troadHolder['block'+i].lineTo(25,25);\r\n\t\t\troadHolder['block'+i].lineTo(25,0);\r\n\t\t\troadHolder['block'+i].endFill();\r\n\t\t\troadHolder['block'+i]._x= (i-row*22)*25;\r\n\t\t\troadHolder['block'+i]._y = row*25;\r\n\t\t} else if(String(lvlArray[i])){\/\/if it's a string, meaning a special block\r\n\t\t\t\/\/\/\/\/*****SPECIAL DIRECTIONAL ROAD PIECE*****\/\/\/\/\/\r\n\t\t\troadHolder.createEmptyMovieClip('block'+i,roadHolder.getNextHighestDepth());\r\n\t\t\troadHolder['block'+i].beginFill(0x111111);\r\n\t\t\troadHolder['block'+i].lineTo(0,0);\r\n\t\t\troadHolder['block'+i].lineTo(0,25);\r\n\t\t\troadHolder['block'+i].lineTo(25,25);\r\n\t\t\troadHolder['block'+i].lineTo(25,0);\r\n\t\t\troadHolder['block'+i].endFill();\r\n\t\t\troadHolder['block'+i]._x= (i-row*22)*25;\r\n\t\t\troadHolder['block'+i]._y = row*25;\r\n\t\t}\r\n\t\tfor(var c:Number = 1;c<=16;c++){\r\n\t\t\tif(i == c*22-1){\r\n\t\t\t\t\/\/if 22 columns have gone by, then we move onto the next row\r\n\t\t\t\trow++;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\/\/run these functions at the start\r\nmakeRoad();\r\nstartGame();\r\n<\/pre>\n<p>That's a lot of code, ain't it? Hopefully, I've given you enough instructions in the comments. If you test out our game, a nice little path should be shown which the enemies will travel through, along with some blank gray boxes where we'll be able to place some turrets.<\/p>\n<p>Before I end this part of the tutorial, I want to give these empty boxes some <tt>rollOver<\/tt> and <tt>rollOut<\/tt> effects. Find where I added the comment saying <tt>\/\/\/\/\/*****EMPTY BLOCK*****\/\/\/\/\/<\/tt> (lines 44-54). Add this code to the bottom of that section of the <tt>if<\/tt> statement:<\/p>\n<pre lang=\"actionscript\">\r\n_root['block'+i].onRollOver = function(){\r\n\t\/\/Change the color to green\r\n\tvar newColor = new Color(this);\r\n\tnewColor.setRGB(0x006600);\r\n}\r\n_root['block'+i].onRollOut = function(){\r\n\t\/\/Change this color back to gray\r\n\tvar newColor = new Color(this);\r\n\tnewColor.setRGB(0x333333);\r\n}\r\n<\/pre>\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\/pt1\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt1\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt1\/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 1: Setting Up a Level The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always [&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\/1481"}],"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=1481"}],"version-history":[{"count":10,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1481\/revisions"}],"predecessor-version":[{"id":1537,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1481\/revisions\/1537"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1481"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}