Tutorial: Create a Tower Defense Game in AS3 – Part 6
Categories: Flash
Table of Contents
Step 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?
The first thing we’re going to do before creating stronger enemies is to make the enemies give you money when you kill them. This will be easy. Just open up “Enemy.as” and find this code in eFrameEvents():
//destroy this if health is equal to or below 0
if(health <= 0){
destroyThis();
}
Just add the following code to the if statement:
_root.money += 5;
Now, we can continue on to making better enemies. 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:int = enemyArray[currentLvl-1][currentEnemy];//get the code from the array
if(theCode != 0){//if it isn't an empty space
var newEnemy:Enemy = new Enemy(theCode);//then create a new enemy and pass in the code
enemyHolder.addChild(newEnemy);//and add it to the enemyholder
}
currentEnemy ++;//move on to the next enemy
enemyTime = 0;//and reset the time
}
}
Okay, so the renovations weren't too major. The only thing we changed was that instead of checking for the code of "1", we check for any code that isn't equal to "0". Then, we pass that value into the Enemy class.
We'll also have to make some changes in the startGame() functions. Don't worry, they'll be just as minor as the ones we just made. Replace the code inside of the function with this:
function startGame():void{//we'll run this function every time a new level begins
for(var i:int=0;i
The only thing we changed here was that now we check for all numbers not equal to 0, instead of just counting those set as 1.
Of course, now we're going to have to make some changes to the Enemy class. Open up "Enemy.as" and find the topmost code where we define the variables and define the Enemy() function. Just replace that with this:
private var _root:MovieClip;
public var xSpeed:int;//how fast it's going horizontally
public var ySpeed:int;//how fast it's going vertically
public var maxSpeed:int = 3;//how fast it can possibly go
public var health:int;
public var level:int;//this will be set to the number passed in
public function Enemy(code:int){
this.addEventListener(Event.ADDED, beginClass);
this.addEventListener(Event.ENTER_FRAME, eFrameEvents);
level = code;//set the level to the value passed in for use in other functions
}
Not too many changes have been made. Now, we're using that variable that was passed into the Enemy() function and making it usable. We're also making the health undefined so we can change it based on the level. In fact, let's change them now. Add this to the top of the beginCode() function:
health = level*5;
This will set the health based on the level of the enemy. Next, let's make him worth a bit more points, shall we? Find the code that we added in the beginning of the tutorial. Simply replace it with this:
_root.money += 5*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!
Final Product