{"id":1464,"date":"2009-02-28T08:07:28","date_gmt":"2009-02-28T12:07:28","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1464"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as3-part-7","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as3-part-7\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS3 &#8211; Part 7"},"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><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 class=\"c_chap\"><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 7: Finishing Touches<\/h3>\n<p>Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I won&#8217;t comment any of the code I give you, as the knowledge you have learned from this tutorial should tell you what&#8217;s going on. Luckily for you, there aren&#8217;t too many finishing touches for us to make, so your brain won&#8217;t hurt too much.<\/p>\n<p>One of the things I want to do is show the player the range of the turret. If you&#8217;ve ever played a tower defense game, you you should know what I&#8217;m talking about. A translucent circle will appear to show the player how far the turret can shoot. We&#8217;re going to show this both when somebody hovers over an empty block and when somebody hovers over a turret. Let&#8217;s start with hovering over an empty block.<\/p>\n<p>Lets first start by opening up &#8220;source.fla&#8221;. Add this code to the top. Don&#8217;t ask any questions&#8230; or else&#8230;<\/p>\n<pre lang=\"actionscript\">\r\nvar rangeCircle:Shape = new Shape();\r\nrangeCircle.graphics.beginFill(0x006600,.5);\r\nrangeCircle.graphics.drawCircle(12.5,12.5,100);\r\nrangeCircle.graphics.endFill();\r\n<\/pre>\n<p>Open up &#8220;EmptyBlock.as&#8221;, would you? Find the <tt>thisMouseOver()<\/tt> function and add the following code to it:<\/p>\n<pre lang=\"actionscript\">\r\n_root.rangeCircle.x = this.x;\r\n_root.rangeCircle.y = this.y;\r\n_root.addChild(_root.rangeCircle);\r\n<\/pre>\n<p>Find the <tt>thisMouseOut()<\/tt> function and add this code to it:<\/p>\n<pre lang=\"actionscript\">\r\n_root.removeChild(_root.rangeCircle);\r\n<\/pre>\n<p>Add the same thing to the <tt>thisClick()<\/tt> function and the work will be done for empty blocks. Now, let&#8217;s do the same thing for Turrets.<\/p>\n<p>Open up &#8220;Turret.as&#8221;. Add this code to the <tt>Turret()<\/tt> function (the main function):<\/p>\n<pre lang=\"actionscript\">\r\nthis.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);\r\nthis.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);\r\n<\/pre>\n<p>Next, add these two functions to the end of the class:<\/p>\n<pre lang=\"actionscript\">\r\nprivate function thisMouseOver(e:MouseEvent):void{\r\n\t_root.rangeCircle.x = this.x-12.5;\r\n\t_root.rangeCircle.y = this.y-12.5;\r\n\t_root.addChild(_root.rangeCircle);\r\n}\r\nprivate function thisMouseOut(e:MouseEvent):void{\r\n\t_root.removeChild(_root.rangeCircle);\r\n}\r\n<\/pre>\n<p>Good stuff. Now, it should work if you hover over the turrets.<\/p>\n<p>Now, what else can we add to our little game? The answer is: it&#8217;s up to you to decide what to add. It&#8217;s also up to you to use what you&#8217;ve learned to do it right. Thank you and good night.<\/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\/pt7\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt7\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as3\/pt7\/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 7: Finishing Touches Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I [&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\/1464"}],"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=1464"}],"version-history":[{"count":3,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1473,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1464\/revisions\/1473"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}