{"id":1384,"date":"2009-02-28T08:02:42","date_gmt":"2009-02-28T12:02:42","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1384"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as3-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as3-part-2\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 2"},"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 class=\"c_chap\"><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><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 2: Adding Turrets<\/h3>\n<p>Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is created. The first step to take in to create a <tt>Turret<\/tt> class. You know the drill, create a new ActionScript File, save it as &#8220;Turret.as&#8221;, and type in the following code:<\/p>\n<pre lang=\"actionscript\">\r\npackage{\/\/creating the basic skeleton\r\n\timporProxy-Connection: keep-alive\r\nCache-Control: max-age=0\r\n\r\nflash.display.MovieClip;\r\n\timport flash.events.*;\r\n\tpublic class Turret extends MovieClip{\r\n\t\tprivate var _root:MovieClip;\r\n\t\t\t\t\r\n\t\tpublic function Turret(){\r\n\t\t\t\/\/adding the required listeners\r\n\t\t\tthis.addEventListener(Event.ADDED, beginClass);\r\n\t\t\tthis.addEventListener(Event.ENTER_FRAME, eFrameEvents);\r\n\t\t}\r\n\t\tprivate function beginClass(e:Event):void{\r\n\t\t\t_root = MovieClip(root);\r\n\t\t\t\r\n\t\t\t\/\/drawing the turret, it will have a gray, circular, base with a white gun\r\n\t\t\tthis.graphics.beginFill(0x999999);\r\n\t\t\tthis.graphics.drawCircle(0,0,12.5);\r\n\t\t\tthis.graphics.endFill();\r\n\t\t\tthis.graphics.beginFill(0xFFFFFF);\r\n\t\t\tthis.graphics.drawRect(-2.5, 0, 5, 20);\r\n\t\t\tthis.graphics.endFill();\r\n\t\t}\r\n\t\tprivate function eFrameEvents(e:Event):void{\r\n\t\t\t\r\n\t\t\tif(_root.gameOver){\/\/destroy this if game is over\r\n\t\t\t\tthis.removeEventListener(Event.ENTER_FRAME, eFrameEvents);\r\n\t\t\t\tMovieClip(this.parent).removeChild(this);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>This will be only the beginning of what we program into the <tt>Turret<\/tt>. Next, we have to define a function in the <tt>_root<\/tt> of the document that will create the turrets. Add this code to the bottom in your source .fla file:<\/p>\n<pre lang=\"actionscript\">\r\nfunction makeTurret(xValue:int,yValue:int):void{\/\/this will need to be told the x and y values\r\n\tvar turret:Turret = new Turret();\/\/creating a variable to hold the Turret\r\n\t\/\/changing the coordinates\r\n\tturret.x = xValue+12.5;\r\n\tturret.y = yValue+12.5;\r\n\taddChild(turret);\/\/add it to the stage\r\n}\r\n<\/pre>\n<p>Now, we can finally make it so the turret is created when the user clicks on an empty block. Find the function <tt>thisClick()<\/tt> in &#8220;EmptyBlock.as&#8221;. Add the following code to that:<\/p>\n<pre lang=\"actionscript\">\r\n_root.makeTurret(this.x,this.y);\/\/make the turret\r\n\/\/remove all the listeners so it can't be clicked on again\r\nthis.buttonMode = false;\r\nthis.graphics.beginFill(0x333333);\r\nthis.graphics.drawRect(0,0,25,25);\r\nthis.graphics.endFill();\r\nthis.removeEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);\r\nthis.removeEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);\r\nthis.removeEventListener(MouseEvent.CLICK, thisClick);\r\n<\/pre>\n<p>Now, if you test out the game, a turret should appear whenever you click on any empty block!<\/p>\n<p>Well, that&#8217;s it for this tutorial. Next time, we&#8217;ll add enemies and program them!<\/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\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt2\/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 2: Adding Turrets Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is [&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\/1384"}],"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=1384"}],"version-history":[{"count":11,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":1496,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions\/1496"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}