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

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

Step 7: Finishing Touches

Welcome back! This part of the tutorial is where we just add all of the finishing touches to the game! Unlike the other parts of the tutorial, I won’t comment any of the code I give you, as the knowledge you have learned from this tutorial should tell you what’s going on. Luckily for you, there aren’t too many finishing touches for us to make, so your brain won’t hurt too much.

One of the things I want to do is show the player the range of the turret. If you’ve ever played a tower defense game, you you should know what I’m talking about. A translucent circle will appear to show the player how far the turret can shoot. We’re going to show this both when somebody hovers over an empty block and when somebody hovers over a turret. Let’s start with hovering over an empty block.

Lets first start by opening up “source.fla”. Add this code to the top. Don’t ask any questions… or else…

var rangeCircle:Shape = new Shape();

Open up “EmptyBlock.as”, would you? Find the thisMouseOver() function and add the following code to it:

_root.rangeCircle.x = this.x;
_root.rangeCircle.y = this.y;

Find the thisMouseOut() function and add this code to it:


Add the same thing to the thisClick() function and the work will be done for empty blocks. Now, let’s do the same thing for Turrets.

Open up “Turret.as”. Add this code to the Turret() function (the main function):

this.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);

Next, add these two functions to the end of the class:

private function thisMouseOver(e:MouseEvent):void{
	_root.rangeCircle.x = this.x-12.5;
	_root.rangeCircle.y = this.y-12.5;
private function thisMouseOut(e:MouseEvent):void{

Good stuff. Now, it should work if you hover over the turrets.

Now, what else can we add to our little game? The answer is: it’s up to you to decide what to add. It’s also up to you to use what you’ve learned to do it right. Thank you and good night.

Final Product

Source Files (Zipped)


Link Post Sunday (1) | Freelance Flash Games News:

[...] Sun is posting again with a slew of tutorials on how to build a tower defense game using [...]


hey man
How to let the enemy find the road itself?
thx lot


This is so cool :D

SHOCK x10:

Hey dude, check your level restart… there’s a bug where the game ends and the user clicks to restart. Upon restarting there are too many enemies left to spawn, but only the starting number (16) spawn. Thus if you either kill them all or sit back, you will be stuck on level 1.
One way would be to clear the number of enemies
Another would be to reload the flash file

Currently my temp. fix is for 10 lives so the user dies if they fail to place a turret- and they begin with $20.


I am trying to put this TD engine into a movie clip that is not the root, but it will not work…any suggestions?


Hey, I love this tutorial, but I’m getting an error 1010 if I beat the game by letting the last enemy get away. Any ideas on how to fix that?


This is just a note to anyone looking for a fix for the glitch mentioned above.

I was able to fix this problem by finding the line in frame 1 where the variable ‘enemiesLeft’ was declared, and set it to 0. This will automatically reset the enemiesLeft when the game is restarted, fixing said problem.


Hey, I haven’t fix the bug, does anyone have?


i know this is old, and many people may not be looking at it anymore, but i would like to resolve an issue i have when the game ends, and the mouse is near the bottom of the screen, the rangeCircle stays on the screen, and then a bunch of #1009 errors come up, and the game does not work properly after. any idea how i can fix this and make the rangeCircle dissappear?