## Tutorial: Create a Tower Defense Game in AS2 – Part 4

Written By MrSun at 8:04 am - Saturday, April 25th, 2009
Categories: Advanced Tutorials, All Tutorials, AS2, Flash, Game Development

### Step 4: Making Turrets Attack Enemies

Well, it’s now time to let the enemies we just created be destroyed. Let’s dig in, shall we?

First, we’ll define a single health variable for the Enemy. Find this code in the makeEnemies() function (~ Line 236):

```//add a few variables to the enemy enemyHolder['enemy'+currentEnemy].maxSpeed = 3;//how fast it can possibly go enemyHolder['enemy'+currentEnemy].xSpeed = 0; enemyHolder['enemy'+currentEnemy].ySpeed = 0;```

`enemyHolder['enemy'+currentEnemy].health = 5;`

Now, we’re going to have to add some variables to the turrets. In the makeRoad() function, find the onRelease function of the Empty Block (~ Line 77). Add this code to the bottom of that function (~ Line 116).

```_root['t'+this._name].angle = 0; //the angle that the turret is currently rotated at _root['t'+this._name].radiansToDegrees = 180/Math.PI;//this is needed for the rotation _root['t'+this._name].damage = 3;//how much damage this little baby can inflict _root['t'+this._name].range = 100;//how far away (in pixels) it can hit a target _root['t'+this._name].enTarget = null;//the current target that it's rotating towards _root['t'+this._name].cTime = 0;//how much time since a shot was fired by this turret _root['t'+this._name].reloadTime = 12;//how long it takes to fire another shot _root['t'+this._name].loaded = true;//whether or not this turret can shoot```

Those are a lot of variables, aren’t they? Well, this is a pretty complex task, so we’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:

```_root['t'+this._name].onEnterFrame = function(){ //FINDING THE NEAREST ENEMY WITHIN RANGE this.distance = this.range;//let's define a variable which will be how far the nearest enemy is this.enTarget = null;//right now, we don't have a target to shoot at for(var i=_root.currentEnemy-1;i>=0;i--){//loop through the children in enemyHolder var cEnemy = _root.enemyHolder['enemy'+i];//define a movieclip that will hold the current child //this simple formula with get us the distance of the current enemy if(Math.sqrt(Math.pow(cEnemy._y - this._y, 2) + Math.pow(cEnemy._x - this._x, 2)) < this.distance){ //if the selected enemy is close enough, then set it as the target this.enTarget = cEnemy; } } //ROTATING TOWARDS TARGET if(this.enTarget != null){//if we have a defined target //turn this baby towards it this.gun._rotation = Math.atan2((this.enTarget._y-this._y), this.enTarget._x-this._x)/Math.PI*180-90; if(this.loaded){//if the turret is able to shoot //subtract the enemy's health this.enTarget.health -= this.damage; this.loaded = false;//then make in unable to do it for a bit //create a bullet _root.createEmptyMovieClip('b'+this._name,_root.getNextHighestDepth()); //draw the bullet _root['b'+this._name].beginFill(0xFFFFFF); _root['b'+this._name].lineTo(0,0); _root['b'+this._name].lineTo(0,3); _root['b'+this._name].lineTo(3,3); _root['b'+this._name].lineTo(3,0); _root['b'+this._name].endFill(); //set the bullet's coordinates _root['b'+this._name]._x = this._x+12.5; _root['b'+this._name]._y = this._y+12.5; //set the bullet's target and damage _root['b'+this._name].target = this.enTarget;   //add some functions to this bullet _root['b'+this._name].onEnterFrame = function(){ this.maxSpeed=4; this.yDist=this.target._y+12.5 - this._y;//how far this guy is from the enemy (x) this.xDist=this.target._x+12.5 - this._x;//how far it is from the enemy (y) this.angle=Math.atan2(this.yDist,this.xDist);//the angle that it must move this.ySpeed=Math.sin(this.angle) * this.maxSpeed;//calculate how much it should move the enemy vertically this.xSpeed=Math.cos(this.angle) * this.maxSpeed;//calculate how much it should move the enemy horizontally //move the bullet towards the enemy this._x+= this.xSpeed; this._y+= this.ySpeed; //check if it is close to the enemy if(this._x+this.maxSpeed*2>=this.target._x && this._x-this.maxSpeed*2<=this.target._x && this._y+this.maxSpeed*2>=this.target._y && this._y-this.maxSpeed*2<=this.target._y){ this.target.health -= this.damage;//make the enemy lose health this.removeMovieClip();//remove this sucker } } } } //LOADING THE TURRET if(!this.loaded){//if it isn't loaded this.cTime ++;//then continue the time if(this.cTime == this.reloadTime){//if time has elapsed for long enough this.loaded = true;//load the turret this.cTime = 0;//and reset the time } } }```

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 onEnterFrame() code to the enemy and made it move (~line 342). Just add this little tidbit of code:

```if(this.health <= 0){ this.removeMovieClip(); }```

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!

#### Final Product

Source Files (Zipped)

### One Comment

Anonymous:

mine doesnt work and it has no errors