How to Create a Tower Defense Game in AS2 – Part 5

Part 5: Winning/Losing the Game

Welcome back to the 5th installment of this tutorial series. In this lesson, we’ll make some playable levels, along with a winning and losing situation. Let’s dig in, shall we?

Lets start off by opening up the main source file. Find where this code is:

enemyArray = [//defining the array
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//1's will just represent an enemy to be created
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//another row means another level
			[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
			  ];

Let’s review this code for a bit. Each of those arrays within that enemyArray represents a level. So, right now, we’re set up for 3 different levels, with an increasing amount of enemies in each level. Now that we’ve covered this, we can continue on to making it possible to advance levels.

In order to do this, we must first count the number of enemies we actually have on stage. This is actually very easy to do. Find the startGame() function (~line 45) that has no code in it, and add the following:

enemiesLeft = enemyArray[currentLvl-1].length;

Now, we have to decrease this number every time an enemy is destroyed. We can do this by adding one line of code to where we remove the Enemy from the stage in the makeEnemies() function. Add the following code (~line 346):

_root.enemiesLeft --;

Now, find the main _root.onEnterFrame() function (~line 287). There should only be one line of code in there right now. We’re going to add some. Add the following code to the bottom of it:

if(enemiesLeft==0){//if there are no more enemies left
	currentLvl ++;//continue to the next level
	currentEnemy = 0;//reset the amount of enemies there are
	startGame();//restart the game
}

Now, try testing out the game. Functionally, it’s working 100%. Practically, it’s not a great game. In order to make this game better, we’re going to have to show some information to the user while they’re playing, like the current level, the score, etc. This is exactly what we’re going to do now.

Now, try testing out the game. Functionally, it’s working 100%. Practically, it’s not a great game. In order to make this game better, we’re going to have to show some information to the user while they’re playing, like the current level, the score, etc. This is exactly what we’re going to do now.

Create four dynamic text boxes at the bottom left portion of the stage, each at 12 point font. Here’s an example of what yours should look like, with an example of what information we’re going to put into them later:

The Text Fields

Got that? Now, we just have to give each of these dynamic text fields an instance name. Label them accordingly:

  • txtLevel
  • txtMoney
  • txtLives
  • txtEnemiesLeft

Now, let’s dive into some code, shall we?

The first thing we need to do is actually define the money and the lives variables. Just add this code to the top:

var money:Number=100;//how much money the player has to spend on turrets
var lives:Number=20;//how many lives the player has

Okay, good stuff. Let’s first make these two variables mean something before we update the text. We first have to make it so the player loses lives so he/she can lose the game.

In order to do this, we must add the following code to the Enemy’s onEnterFrame() function (~line 353):

//checking what direction it goes when finishing the path
if(_root.finDir == 'UP'){//if it finishes at the top
	if(this._y <= -25){//if the y value is too high
		_root.lives --;//take away a life
		_root.money -= 5;//don't let the player gain any money
		this.removeMovieClip();//take it away from the stage
	}
} else if(_root.finDir == 'RIGHT'){//and so on for other directions
	if(this._x >= 550){
		_root.lives --;
		_root.money -= 5;
		this.removeMovieClip();
	}
} else if(_root.finDir == 'DOWN'){
	if(this._y >= 300){
		_root.lives --;
		_root.money -= 5;
		this.removeMovieClip();
	}
} else if(_root.startDir == 'LEFT'){
	if(this._x <= 0){
		_root.lives --;
		_root.money -= 5;
		this.removeMovieClip();
	}
}
if(this.health <= 0){
	_root.enemiesLeft --;
	_root.money += 5;
	this.removeMovieClip();
}

Note that we also are giving the player money for killing the enemies. Next, we’ll make each of the turrets cost a certain amount of money. I’m thinking $20 is a good price. Find the block’s onRelease function (~line 80). We have to wrap all of its contents with if(_root.money >= 20){money-=20;......}. This way, the final onRelease function will look like this:

_root['block'+i].onRelease = function(){
	if(_root.money >= 20){//if there's enough money
		_root.money -= 20;//spend it and make a turret
		//this function will run when the empty block is clicked on

		//change this guy's color back
		var newColor = new Color(this);
		newColor.setRGB(0x333333);
		//set all other mouse functions to null in order to keep it from being clicked again
		this.onRollOver = null;
		this.onRollOut = null;
		this.onRelease = null;
		//create an empty turret movieclip that will be on the top root layer
		_root.createEmptyMovieClip('t'+this._name,_root.getNextHighestDepth());

		//drawing the turret, it will have a gray, circular, base with a white gun
		_root['t'+this._name].beginFill(0x999999);//coloring the base light gray
		_root['t'+this._name].moveTo(0, 12.5);//move the entire shape a certain way
		//create 4 curves so that it'll look like a circle
		_root['t'+this._name].curveTo(0,25,12.5,25);
		_root['t'+this._name].curveTo(25,25,25,12.5);
		_root['t'+this._name].curveTo(25,0,12.5,0);
		_root['t'+this._name].curveTo(0,0,0,12.5);
		_root['t'+this._name].endFill();//end the fill so we can make a new one

		//creating the gun
		_root['t'+this._name].createEmptyMovieClip('gun',_root['t'+this._name].getNextHighestDepth());
		_root['t'+this._name].gun.beginFill(0xFFFFFF);
		_root['t'+this._name].gun.lineTo(-2,-2);
		_root['t'+this._name].gun.lineTo(2,-2);
		_root['t'+this._name].gun.lineTo(2,15);
		_root['t'+this._name].gun.lineTo(-2,15);
		_root['t'+this._name].gun.lineTo(-2,-2);
		_root['t'+this._name].gun.endFill();
		//setting the gun to be on the center of the turret
		_root['t'+this._name].gun._x = 12.5;
		_root['t'+this._name].gun._y = 12.5;
		//set the turrets coordinates to be the blocks coordinates
		_root['t'+this._name]._x = this._x;
		_root['t'+this._name]._y = this._y;

		_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

		_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
					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;
					_root['b'+this._name].damage = this.damage;

					//add some functions to this bullet
					_root['b'+this._name].onEnterFrame = function(){
						this.maxSpeed=8;
						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, we can update these text fields. Once again, find the _root.onEnterFrame() function (~line 300). Add the following code which will update all the text fields with the needed information:

//Updating the text fields
txtLevel.text = 'Level '+currentLvl;
txtMoney.text = '$'+money;
txtLives.text = 'Lives: '+lives;
txtEnemiesLeft.text = 'Enemies Left:  '+enemiesLeft;

Now, let’s create some winning and losing scenarios for the player. When the player has beaten all of the levels, then a win screen should show up. The same thing should happen with a lose screen when the player loses all of his/her lives. Let’s create these two frames, shall we?

Before, we create the frames, we have to add a layer called “labels”. This will just let us easily navigate to the required frames. Next, create a frame labeled “win” and add whatever message you want to inform the player that they’ve won. Do the same with a “lose” frame.

Now, we have to make it possible for the player to restart the game. We’ll let them do it by clicking anywhere on the screen. Copy and paste this code to both the “win” frame and the “lose” frame:

timeElapsed = 0;//how many frames have elapsed since this screen has been shown

_root.onEnterFrame = function(){
	timeElapsed ++;//increase the amount of frames that have elapsed
	if(timeElapsed == 120){//if 5 seconds (@24fps) have elapsed
		gotoAndStop(1);//go back to restart the game
	}
}

All right, the next thing we have to do is navigate to the desired frame when the player wins or loses. Go back to frame 1 and find the _root.onEnterFrame() function (~line 293). Add the following code to the very beginning of it:

//if there aren't any levels left
	if(currentLvl > enemyArray.length){
		gameOver=true;//set the game to be over

		//reset all the stats
		currentLvl = 1;
		currentEnemy = 0;
		enemyTime = 0;
		enemyLimit = 12;
		enemiesLeft = 0;

		roadHolder.removeMovieClip();//remove the pieces of road
		enemyHolder.removeMovieClip();//remove the enemies
		//remove all of the blocks
		for(i=0;i<=0){//if the user runs out of lives
		gameOver=true;//set the game to be over

		//reset all the stats
		currentLvl = 1;
		currentEnemy = 0;
		enemyTime = 0;
		enemyLimit = 12;
		enemiesLeft = 0;

		roadHolder.removeMovieClip();//remove the pieces of road
		enemyHolder.removeMovieClip();//remove the enemies
		//remove all of the blocks
		for(i=0;i

Sweet. This concludes this installment of the tutorial series. Join us next time when we expand on the game!

Preview

Download Source
(Requires Flash 8 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials