Tutorial: Create a Tower Defense Game in AS3 – Part 6


Written By MrSun at 8:06 am - Saturday, February 28th, 2009
Categories: Advanced Tutorials, All Tutorials, AS3, Flash, Game Development

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<enemyArray[currentLvl-1].length;i++){
		if(enemyArray[currentLvl-1][i] != 0){
			enemiesLeft ++;
		}
	}
}

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

Source Files (Zipped)

2 Comments

Craksy:

Hey!
just want to say great tutorial!
seems like you really put some time into it! thumbs up for you :D

it really inspires me, and (in opposite to many other tutorials) it gives you a final product which is easy expandable and not just a copied game, where you can add your own graphics :P

also i have never seen the use of “directional tiles” before!
pretty smart ;P
i would have made some sort of simple AI for the creeps, but this is so much easier :D

to be honest im actually not completely following the tutorial… i read the first couple of pages, and then started my project!
i like the methods, but still i like to do stuff on my own :P
right now im just reading the last couple of pages, for joy, and just felt like dropping a comment, to give my opinion about your great tutorial :)

if you for some reason wants to contact me you should know that the email address provided is temporarily unavailable, because of my host having problems with one of his servers…
you should try to add me on msn: abesovs@hotmail.com

not saying that you should have any reason for contacting me… just thought i would give you the opportunity to answer me back, if you felt like it :P

apologizing for my bad english :P (hope you are able to understand me though)
and keep up the good work :D

-Craksy


MartinThatcher:

Hey, I’m loving the tutorial. However, there is one small bug: the LEFT directional tile actually makes the creeps turn right! lol Not that big of a deal though. Thanks for the great tutorial! :)


«
»