{"id":1509,"date":"2009-04-25T08:04:22","date_gmt":"2009-04-25T12:04:22","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1509"},"modified":"2022-05-29T08:23:29","modified_gmt":"2022-05-29T12:23:29","slug":"tutorial-create-a-tower-defense-game-in-as2-part-4","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-tower-defense-game-in-as2-part-4\/","title":{"rendered":"Tutorial: Create a Tower Defense Game in AS2 &#8211; Part 4"},"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 class=\"c_chap\"><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 4: Making Turrets Attack Enemies<\/h3>\n<p>Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we?<\/p>\n<p>First, we&#8217;ll define a single <tt>health<\/tt> variable for the Enemy. Find this code in the <tt>makeEnemies()<\/tt> function (~ Line 236):<\/p>\n<pre lang=\"actionscript\">\r\n\/\/add a few variables to the enemy\r\nenemyHolder['enemy'+currentEnemy].maxSpeed = 3;\/\/how fast it can possibly go\r\nenemyHolder['enemy'+currentEnemy].xSpeed = 0;\r\nenemyHolder['enemy'+currentEnemy].ySpeed = 0;\r\n<\/pre>\n<p>Just add this code:<\/p>\n<pre lang=\"actionscript\">\r\nenemyHolder['enemy'+currentEnemy].health = 5;\r\n<\/pre>\n<p>Now, we&#8217;re going to have to add some variables to the turrets. In the <tt>makeRoad()<\/tt> function, find the <tt>onRelease<\/tt> function of the Empty Block (~ Line 77). Add this code to the bottom of that function (~ Line 116).<\/p>\n<pre lang=\"actionscript\">\r\n_root['t'+this._name].angle = 0; \/\/the angle that the turret is currently rotated at\r\n_root['t'+this._name].radiansToDegrees = 180\/Math.PI;\/\/this is needed for the rotation\r\n_root['t'+this._name].damage = 3;\/\/how much damage this little baby can inflict\r\n_root['t'+this._name].range = 100;\/\/how far away (in pixels) it can hit a target\r\n_root['t'+this._name].enTarget = null;\/\/the current target that it's rotating towards\r\n_root['t'+this._name].cTime = 0;\/\/how much time since a shot was fired by this turret\r\n_root['t'+this._name].reloadTime = 12;\/\/how long it takes to fire another shot\r\n_root['t'+this._name].loaded = true;\/\/whether or not this turret can shoot\r\n<\/pre>\n<p>Those are a lot of variables, aren&#8217;t they? Well, this is a pretty complex task, so we&#8217;re going to need all of them. So, brace yourself, for we are now going to jump into some code. Add the following after the code you just added above:<\/p>\n<pre lang=\"actionscript\">\r\n_root['t'+this._name].onEnterFrame = function(){\r\n\t\/\/FINDING THE NEAREST ENEMY WITHIN RANGE\r\n\tthis.distance = this.range;\/\/let's define a variable which will be how far the nearest enemy is\r\n\tthis.enTarget = null;\/\/right now, we don't have a target to shoot at\r\n\tfor(var i=_root.currentEnemy-1;i>=0;i--){\/\/loop through the children in enemyHolder\r\n\t\tvar cEnemy = _root.enemyHolder['enemy'+i];\/\/define a movieclip that will hold the current child\r\n\t\t\/\/this simple formula with get us the distance of the current enemy\r\n\t\tif(Math.sqrt(Math.pow(cEnemy._y - this._y, 2) + Math.pow(cEnemy._x - this._x, 2)) < this.distance){\r\n\t\t\t\/\/if the selected enemy is close enough, then set it as the target\r\n\t\t\tthis.enTarget = cEnemy;\r\n\t\t}\r\n\t}\r\n\t\/\/ROTATING TOWARDS TARGET\r\n\tif(this.enTarget != null){\/\/if we have a defined target\r\n\t\t\/\/turn this baby towards it\r\n\t\tthis.gun._rotation = Math.atan2((this.enTarget._y-this._y), this.enTarget._x-this._x)\/Math.PI*180-90;\r\n\t\tif(this.loaded){\/\/if the turret is able to shoot\r\n\t\t\t\/\/subtract the enemy's health\r\n\t\t\tthis.enTarget.health -= this.damage;\r\n\t\t\tthis.loaded = false;\/\/then make in unable to do it for a bit\r\n\t\t\t\/\/create a bullet\r\n\t\t\t_root.createEmptyMovieClip('b'+this._name,_root.getNextHighestDepth());\r\n\t\t\t\/\/draw the bullet\r\n\t\t\t_root['b'+this._name].beginFill(0xFFFFFF);\r\n\t\t\t_root['b'+this._name].lineTo(0,0);\r\n\t\t\t_root['b'+this._name].lineTo(0,3);\r\n\t\t\t_root['b'+this._name].lineTo(3,3);\r\n\t\t\t_root['b'+this._name].lineTo(3,0);\r\n\t\t\t_root['b'+this._name].endFill();\r\n\t\t\t\/\/set the bullet's coordinates\r\n\t\t\t_root['b'+this._name]._x = this._x+12.5;\r\n\t\t\t_root['b'+this._name]._y = this._y+12.5;\r\n\t\t\t\/\/set the bullet's target and damage\r\n\t\t\t_root['b'+this._name].target = this.enTarget;\r\n\t\t\t\r\n\t\t\t\/\/add some functions to this bullet\r\n\t\t\t_root['b'+this._name].onEnterFrame = function(){\r\n\t\t\t\tthis.maxSpeed=4;\r\n\t\t\t\tthis.yDist=this.target._y+12.5 - this._y;\/\/how far this guy is from the enemy (x)\r\n\t\t\t\tthis.xDist=this.target._x+12.5 - this._x;\/\/how far it is from the enemy (y)\r\n\t\t\t\tthis.angle=Math.atan2(this.yDist,this.xDist);\/\/the angle that it must move\r\n\t\t\t\tthis.ySpeed=Math.sin(this.angle) * this.maxSpeed;\/\/calculate how much it should move the enemy vertically\r\n\t\t\t\tthis.xSpeed=Math.cos(this.angle) * this.maxSpeed;\/\/calculate how much it should move the enemy horizontally\r\n\t\t\t\t\/\/move the bullet towards the enemy\r\n\t\t\t\tthis._x+= this.xSpeed;\r\n\t\t\t\tthis._y+= this.ySpeed;\r\n\t\t\t\t\/\/check if it is close to the enemy\r\n\t\t\t\tif(this._x+this.maxSpeed*2>=this.target._x && this._x-this.maxSpeed*2<=this.target._x\r\n\t\t\t\t&#038;&#038; this._y+this.maxSpeed*2>=this.target._y && this._y-this.maxSpeed*2<=this.target._y){\r\n\t\t\t\t\tthis.target.health -= this.damage;\/\/make the enemy lose health\r\n\t\t\t\t\tthis.removeMovieClip();\/\/remove this sucker\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\/\/LOADING THE TURRET\r\n\tif(!this.loaded){\/\/if it isn't loaded\r\n\t\tthis.cTime ++;\/\/then continue the time\r\n\t\tif(this.cTime == this.reloadTime){\/\/if time has elapsed for long enough\r\n\t\t\tthis.loaded = true;\/\/load the turret\r\n\t\t\tthis.cTime = 0;\/\/and reset the time\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, if you test out the game and create some turrets, they should start shooting at those darn enemies! Next, we have to make it so the enemy dies when shot. This will be pretty easy to do. Find the code where we added the <tt>onEnterFrame()<\/tt> code to the enemy and made it move (~line 342). Just add this little tidbit of code:<\/p>\n<pre lang=\"actionscript\">\r\nif(this.health <= 0){\r\n\tthis.removeMovieClip();\r\n}\r\n<\/pre>\n<p>Pretty hot stuff, ain't it? Well, this concludes the fourth installment of this tutorial. Join us next time when we make levels and have winning and losing scenarios!<\/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\/pt4\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt4\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/tower-defense-as2\/pt4\/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 4: Making Turrets Attack Enemies Well, it&#8217;s now time to let the enemies we just created be destroyed. Let&#8217;s dig in, shall we? First, we&#8217;ll define a single health variable for [&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\/1509"}],"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=1509"}],"version-history":[{"count":5,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1509\/revisions"}],"predecessor-version":[{"id":1539,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1509\/revisions\/1539"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1509"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}