Tutorial: Create a Tower Defense Game in AS2
Categories: Flash
Table of Contents
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 very fun to play. I am here to walk you through the creation of one of these games. Let us begin, shall we?
In this section of the tutorial, we’re going to set up the roads and stuff onto the stage. However, the first thing we need to do is create a blank flash document with a black background with the frames per second set at 24.
Now, we can dive into some code. Let’s first create some code that will lay down the track for the enemies to go through. Create a new layer called “actions” and add the following code to it:
stop();
//setting vars to step in for turns and special blocks
var S:String = 'START';
var F:String = 'FINISH';
var U:String = 'UP';
var R:String = 'RIGHT';
var D:String = 'DOWN';
var L:String = 'LEFT';
var startDir:String;//the direction the enemies go when they enter
var finDir:String;//the direction the enemies go when they exit
var startCoord:Number;//the coordinates of the beginning of the road
var lvlArray:Array = new Array();//this array will hold the formatting of the roads
lvlArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,R,1,1,D,0,0,R,1,1,D,0,0,R,1,1,D,0,0,
0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
S,D,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,R,1,F,
0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
0,R,1,1,U,0,0,R,1,1,U,0,0,R,1,1,U,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
];
//the names of these variables explain what they do
var currentLvl:Number = 1;
var gameOver:Boolean = false;
function startGame():Void{//we'll run this function every time a new level begins
//right now we don't have any code
}
_root.createEmptyMovieClip('roadHolder',_root.getNextHighestDepth());//add a MovieClip to hold the road
function makeRoad():Void{
var row:Number = 0;//the current row we're working on
var block;//this will act as the block that we're placing down
for(i=0;i
That's a lot of code, ain't it? Hopefully, I've given you enough instructions in the comments. If you test out our game, a nice little path should be shown which the enemies will travel through, along with some blank gray boxes where we'll be able to place some turrets.
Before I end this part of the tutorial, I want to give these empty boxes some rollOver and rollOut effects. Find where I added the comment saying /////*****EMPTY BLOCK*****///// (lines 44-54). Add this code to the bottom of that section of the if statement:
_root['block'+i].onRollOver = function(){
//Change the color to green
var newColor = new Color(this);
newColor.setRGB(0x006600);
}
_root['block'+i].onRollOut = function(){
//Change this color back to gray
var newColor = new Color(this);
newColor.setRGB(0x333333);
}
Final Product
Really cool!
May 14th, 2009 at 10:15 am
Hello, I’ve been following this tutorial exactly how it is written and everything and when I go to test my game the level will not show up. All I see is the black background and the script has no errors. I’m using this tutorial with Macromedia Flash MX Professional 2004, is this the problem?
July 16th, 2009 at 10:54 pm
what’s AS2 im a noob but im here to learn :O
xD
August 18th, 2009 at 11:03 am
wow me too im using mx what a coincidence! i see just black stuff and nothing
August 20th, 2009 at 4:48 pm
Hey. I’m having problems with the onRollOver mouse square color change. I keep getting 3 errors.
} else if(lvlArray[i] == 1){//if there is supposed to be a row
function makeRoad():Void{
startGame();
I’ve been adding the scrip into line 45 of the code, just after your comment about Empty Blocks, and the errors come as i press CTRL+Enter to see the Movie.
Thanks 😀
August 21st, 2009 at 6:31 am
I don’t program in Flash but just looking over the code i think you need another For loop after
for(i=0;i<lvlArray.length;i++)
you seem to be only rendering i row and not going through the entire array
September 12th, 2009 at 10:02 am
hi the piece of code for the rollover didnt work
can i ask what flash you are using?
September 15th, 2009 at 5:13 pm
Awesome ty
September 23rd, 2009 at 10:24 pm
A good tip to check your code is to use /* */ between blocks of the code and copy the above code into your own actionscript – sometimes it’s just something simple like typing 1 for l or vise versa
September 29th, 2009 at 4:42 pm
I can’t see the rollOver and rollOut stuff on my SWF file, please help
November 15th, 2009 at 1:55 pm
I do believe this code is set for the adobe cs3 version of flash, I followed the tutorial and see the end result so far. That may be the problem.
January 1st, 2010 at 6:22 am
These errors come in AS:
Clipboard Actions: Line 34: ‘{‘ expected
function startGame():Void{//we’ll run this function every time a new level begins
Clipboard Actions: Line 39: ‘{‘ expected
function makeRoad():Void{
Clipboard Actions: Line 86: Unexpected ‘}’ encountered
}
January 16th, 2010 at 10:20 am
Im using macromedia flash 8 and this 1st part has worked fine on my version
February 19th, 2010 at 7:00 pm
Hi I am making a game for a class and want to alterter this game i was wondering how I would go about instead of having a grey box having a white one
March 16th, 2010 at 2:50 am
i dont get where it says “Before I end this part of the tutorial, I want to give these empty boxes some rollOver and rollOut effects. Find where I added the comment saying /////*****EMPTY BLOCK*****///// (lines 44-54). Add this code to the bottom of that section of the if statement”
May 13th, 2010 at 5:01 am
i have the same problem as micheal but i am useing flash CS4
May 13th, 2010 at 12:40 pm
doesn’t work on mine either…I used flash mx…
May 25th, 2010 at 2:22 am