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


Written By MrSun at 8:06 am - Saturday, February 28th, 2009
Categories: Flash

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

Source Files (Zipped)

Read More...

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


Written By MrSun at 8:05 am - Saturday, February 28th, 2009
Categories: Flash

Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 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 […]

Read More...

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


Written By MrSun at 8:04 am - Saturday, February 28th, 2009
Categories: Flash

Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches 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 […]

Read More...

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


Written By MrSun at 8:03 am - Saturday, February 28th, 2009
Categories: Flash

Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 3: Adding Enemies Welcome back. In this part of the tutorial, we are going to add enemies to the field and we’re going to program them to move through the paths. […]

Read More...

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


Written By MrSun at 8:02 am - Saturday, February 28th, 2009
Categories: Flash

Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 2: Adding Turrets Okay, so in this part of the tutorial, we are going to make it so when the user clicks on any of the empty blocks, a turret is […]

Read More...

Tutorial: Create a Tower Defense Game in AS3


Written By MrSun at 8:01 am - Saturday, February 28th, 2009
Categories: Flash

Table of Contents Setting up Level Adding Turrets Adding Enemies Making Turrets Attack Enemies Winning/Losing the Game Expanding on the Game Finishing Touches Step 1: Setting Up a Level The tower defense genre is one that has become extremely popular over the years. Although they can become quite complicated to develop, they are almost always […]

Read More...

So You Wanna Make a Game? A Step-by-Step Guide to Becoming a Flash Game Developer


Written By MrSun at 8:00 am - Monday, February 16th, 2009
Categories: Flash

So you want to be a flash game developer, eh? It’s something that’s easy to get into, but hard to get good at. I’m going to guide you through the process that a total newbie must get into in order to get into the business.

Read More...

Tutorial: Make a Rhythm Game in AS3 – Part 7


Written By MrSun at 8:07 am - Saturday, January 24th, 2009
Categories: Flash

The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 7: Finishing Touches Well, now for the last step, adding the finishing touches. Just like in my previous tutorial, I won’t really go into much description in this section, in hopes that you can […]

Read More...

Tutorial: Make a Rhythm Game in AS3 – Part 6


Written By MrSun at 8:06 am - Saturday, January 24th, 2009
Categories: Flash

The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 6: Scoring The next step in making our rhythm game is scoring. The user will get points depending on how far away the arrow is from the receptor when it hits it. We’re also […]

Read More...

Tutorial: Make a Rhythm Game in AS3 – Part 5


Written By MrSun at 8:05 am - Saturday, January 24th, 2009
Categories: Flash

The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 5: Make a Level (or five) Now that we’ve programmed the basic gameplay for our game, we can make a menu system with levels. It’s going to be very simple, only one page because […]

Read More...