{"id":1491,"date":"2009-04-25T08:02:57","date_gmt":"2009-04-25T12:02:57","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1491"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as2-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as2-part-2\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 2"},"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 class=\"c_chap\"><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 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. Let&#8217;s begin, shall we?<\/p>\n<p>Open up your main flash file and find the code where we set the empty block&#8217;s <tt>onRollOver()<\/tt> and <tt>onRollOut<\/tt> functions. Well, now we&#8217;re going to create a <tt>onRelease()<\/tt> function, which just means clicking. Add this code to around <strong>Line 65<\/strong>, or after you set the <tt>onRollOver<\/tt> and <tt>onRollOut<\/tt> functions:<\/p>\n<pre lang=\"actionscript\">\r\n_root['block'+i].onRelease = function(){\r\n\t\/\/this function will run when the empty block is clicked on\r\n\t\t\t\t\r\n\t\/\/change this guy's color back\r\n\tvar newColor = new Color(this);\r\n\tnewColor.setRGB(0x333333);\r\n\t\/\/set all other mouse functions to null in order to keep it from being clicked again\r\n\tthis.onRollOver = null;\r\n\tthis.onRollOut = null;\r\n\tthis.onRelease = null;\r\n\t\/\/create an empty turret movieclip that will be on the top root layer\r\n\t_root.createEmptyMovieClip('t'+this._name,_root.getNextHighestDepth());\r\n\r\n\t\/\/drawing the turret, it will have a gray, circular, base with a white gun\r\n\t_root['t'+this._name].beginFill(0x999999);\/\/coloring the base light gray\r\n\t_root['t'+this._name].moveTo(0, 12.5);\/\/move the entire shape a certain way\r\n\t\/\/create 4 curves so that it'll look like a circle\r\n\t_root['t'+this._name].curveTo(0,25,12.5,25);\r\n\t_root['t'+this._name].curveTo(25,25,25,12.5);\r\n\t_root['t'+this._name].curveTo(25,0,12.5,0);\r\n\t_root['t'+this._name].curveTo(0,0,0,12.5);\r\n\t_root['t'+this._name].endFill();\/\/end the fill so we can make a new one\r\n\t\r\n\t\/\/creating the gun\r\n\t_root['t'+this._name].createEmptyMovieClip('gun',_root['t'+this._name].getNextHighestDepth());\r\n\t_root['t'+this._name].gun.beginFill(0xFFFFFF);\r\n\t_root['t'+this._name].gun.lineTo(-2,-2);\r\n\t_root['t'+this._name].gun.lineTo(2,-2);\r\n\t_root['t'+this._name].gun.lineTo(2,15);\r\n\t_root['t'+this._name].gun.lineTo(-2,15);\r\n\t_root['t'+this._name].gun.lineTo(-2,-2);\r\n\t_root['t'+this._name].gun.endFill();\r\n\t\/\/setting the gun to be on the center of the turret\r\n\t_root['t'+this._name].gun._x = 12.5;\r\n\t_root['t'+this._name].gun._y = 12.5;\r\n\t\/\/set the turrets coordinates to be the blocks coordinates\r\n\t_root['t'+this._name]._x = this._x;\r\n\t_root['t'+this._name]._y = this._y;\r\n}\r\n<\/pre>\n<p>Well, it&#8217;s a lot of code, but it gets the job done.<\/p>\n<p>That&#8217;s it for this part of the tutorial. Next time, we&#8217;ll create and program some enemies!<\/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\/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-as2\/pt2\/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 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,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\/1491"}],"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=1491"}],"version-history":[{"count":5,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1491\/revisions"}],"predecessor-version":[{"id":1536,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1491\/revisions\/1536"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1491"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}