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

Part 6: Expanding on the Game

Welcome to the 6th part of the tutorial, Expanding on the Game! Well, what do I mean by “Expanding”? Well, by expanding, I mean that we’re going to create more enemies and more levels. Sounds pretty cool, doesn’t it?

Open up “source.fla” and find the makeEnemies function. We’re going to have some major renovations to this functions. Just replace the function with this new code:

function makeEnemies():Void{//this function will add enemies to the field
	if(enemyTime < enemyLimit){//if it isn't time to make them yet
		enemyTime ++;//then keep on waiting
	} else {//otherwise
		var theCode:Number = enemyArray[currentLvl-1][currentEnemy];//get the code from the array
		if(theCode != 0 && theCode != null){//if it's not set at 0
			//then create a new enemy and add it to the enemy holder
			enemyHolder.createEmptyMovieClip('enemy'+currentEnemy,enemyHolder.getNextHighestDepth());
			//now we're going to draw the enemy. It'll just be a tiny red circle
			enemyHolder['enemy'+currentEnemy].beginFill(0xFF0000);//coloring them red gray
			enemyHolder['enemy'+currentEnemy].moveTo(0, 2.5);//move the entire shape a certain way
			//create 4 curves so that it'll look like a circle
			enemyHolder['enemy'+currentEnemy].curveTo(0,10,5,10);
			enemyHolder['enemy'+currentEnemy].curveTo(10,10,10,5);
			enemyHolder['enemy'+currentEnemy].curveTo(10,0,5,0);
			enemyHolder['enemy'+currentEnemy].curveTo(0,0,0,5);
			enemyHolder['enemy'+currentEnemy].endFill();//end the fill

			//add a few variables to the enemy
			enemyHolder['enemy'+currentEnemy].enLevel = theCode;//setting its level to be what # it is
			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*theCode;

			//checking what the start direction is
			if(_root.startDir == 'UP'){//if it's starting up
				enemyHolder['enemy'+currentEnemy]._y = 300;//set the y value off the field
				enemyHolder['enemy'+currentEnemy]._x = _root.startCoord;//make the x value where it should be
				enemyHolder['enemy'+currentEnemy].xSpeed = 0;//make it not move horizontally
				enemyHolder['enemy'+currentEnemy].ySpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;//make it move upwards
			} else if(_root.startDir == 'RIGHT'){//and so on for other directions
				enemyHolder['enemy'+currentEnemy]._x = -25;
				enemyHolder['enemy'+currentEnemy]._y = _root.startCoord;
				enemyHolder['enemy'+currentEnemy].xSpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;
				enemyHolder['enemy'+currentEnemy].ySpeed = 0;
			} else if(_root.startDir == 'DOWN'){
				enemyHolder['enemy'+currentEnemy]._y = -25;
				enemyHolder['enemy'+currentEnemy]._x = _root.startCoord;
				enemyHolder['enemy'+currentEnemy].xSpeed = 0;
				enemyHolder['enemy'+currentEnemy].ySpeed = enemyHolder['enemy'+currentEnemy].maxSpeed;
			} else if(_root.startDir == 'LEFT'){
				enemyHolder['enemy'+currentEnemy]._x = 550;
				enemyHolder['enemy'+currentEnemy]._y = _root.startCoord;
				enemyHolder['enemy'+currentEnemy].xSpeed = -enemyHolder['enemy'+currentEnemy].maxSpeed;
				enemyHolder['enemy'+currentEnemy].ySpeed = 0;
			}

			enemyHolder['enemy'+currentEnemy]._x += 5;//fixing the x value
			enemyHolder['enemy'+currentEnemy]._y += 5;//fixing up the y value

			enemyHolder['enemy'+currentEnemy].onEnterFrame = function(){//give it some functions
				this._x += this.xSpeed;
				this._y += this.ySpeed;

				//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*this.enLevel;//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.enLevel;
						this.removeMovieClip();
					}
				} else if(_root.finDir == 'DOWN'){
					if(this._y >= 300){
						_root.lives --;
						_root.money -= 5*this.enLevel;
						this.removeMovieClip();
					}
				} else if(_root.startDir == 'LEFT'){
					if(this._x <= 0){
						_root.lives --;
						_root.money -= 5*this.enLevel;
						this.removeMovieClip();
					}
				}

				if(this.health <= 0){
					_root.enemiesLeft --;
					_root.money += 5*this.enLevel;
					this.removeMovieClip();
				}
			}
		}
		currentEnemy ++;//move on to the next enemy
		enemyTime = 0;//and reset the time
	}
}

There aren’t very many renovations that we’ve made, but they will make our code much more flexible. It allows us to create different enemy levels by setting the enLevel to be equal to the code that is placed into the enemy array. The enLevel in turn lets us dynamically change the amount of health the enemy has and the amount of money that it gives you when you kill it. Right now the health and money it gives you is 5 times the enemy level.

Now, we can make more levels with better enemies! You can customize your own levels, or use the ones I created by setting these values in the enemyArray:

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
			[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],//another row means another level
			[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],
			[100],
			[5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5],
			[250,250,250]
			  ];

Of course, I’d suggest creating your own levels, as mine aren’t what you would call the best. Anyways, this wraps up the second to last part of this tutorial. Join us next time when finish up this little game!

Preview

Download Source
(Requires Flash 8 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials