Welcome to part 3 of the tower defense series – Today we are going to be delving into the fine art of realism. Nothing ruins the immersion of games like something that doesn’t make sense in the real world (unless that’s what your game is completely about. Since most tower defense games are rooted in the real world, we need to make sure that the items in the game act like real object.
If you’ve been diligently following the past 2 part of the series (”part 1 – Waypoint and Enemies” and “part 2 – Placing towers”) you know that currently both the towers and the creeps don’t rotate. Now that’s fine for hot air balloons and flying saucers, but people turn in the direction their either walking in or shooting in. So to this end we have to add a few new abilities.
- Towers need to be able to target the nearest creep
- Towers need to face in the direction of the creep they’re targeting
- Creeps need to rotate when they move in a specific direction
Before we begin, if you haven’t read How to build a tower defense flash game – Part 1 – Waypoints and Enemies or How to build a tower defense flash game – Part 2 – Placing Towers
, you should go back and read them since this article builds on the ideas started there.
Also, the source code for this tutorial can be Downloaded Here.
Well lets get right in it – To have a successful game we need to have a “somewhat” realistic enemy. Movie people say something like “level of immersion” quality – If all of a sudden in the middle of the lord of the ring series Frodo and the gang where running from the black rider and you saw a boom mic over the black riders shoulder, you’d say “that’s not right – this movie sucks”. As a matter of fact, you would be right. The movie producer could have the best movie ever, but without keeping the movie goer engaged they realize they are just watching a movie. An expensive movie.
As game developers we have a tough job ahead of us too, making a game that also engages the player. However, we don’t want to be too bogged down with the tedious stuff to make it real, making things real is expensive in time and energy. So we do what true game developer do – we fake it.
![]() |
This code is added to the “spawn” function on the main timeline and is run every frame for the creep. What it does is simple – It takes the current direction that we are looking in (we already know how far we had to travel based on radians) and add a bit of randomness.
- Math.sin((getTimer() + this.rnd * 100) / 100) * 7
The above code returns a number between -1 and 1, which is then multiplies by 7 and added to the current direction (which is in degrees) to give the impression of unique movement. What I mean is that we take the most direct path to the waypoint and then add a randomness of +7 or -7 to give it a some realism. The creep will look partially in the direction that it is moving giving the impression of more expanded movement.
The “if” and “else if” statement are just to make sure that the roation stays between -10 and +10 of the rotation. Again, we could have just had the code as “this.r = this.dir” and set that to the _rotation, but there is no fun in a completely straight line of troops following a path. Always try to mix is up a bit!
![]() |
So now we get to the fun – First we want to deal with how the tower picks a target. So lets go straight to the code:
++lastChecked;
if (lastChecked >= rate)
{
lastChecked = 0;
Since there is no needs to slow everyone computer constantly checking if the tower has a enemy in it’s “range” – We keep a counter going for the last time this particular tower checked for new enemies. This prevents from tedious “Math.sqrt” from being called once a frame for every tower we have out.
- if (target == 0 && _root.creepArray.length > 0)
We also make a check for it the tower in fact alreay targeting a creep (the target variable would be “1″) or if the creepArray is empty (meaning that there are currently no enemies on the screen).
- for (i = 0; i <= _root.creepArray.length; i++)
Given that we don’t already have a target and that there are creeps on the map already, we can go ahead and traverse the array of creeps.
The variable “d” is the distance from the inidividual creep to the tower itself. This is a basic version of the code we used to move a “creep” closer to a “waypoint”. If the distance (the variable “d”) falls within the range of the tower we want the tower to track that enemy.
Side note about targeting enemies
Depending on what container you use for your creep list (We use a simple array, but this is by no means the only way) and how you traverse that list you may have unexpected targeting issues – you could have your tower track only the furthest enemies by keeping a variable of the farthest creep range that fall in the range of the tower. I will list just one
So since WE store the creep list in an array and since WE are traversing the list (i = 0, 1, 2, … , N) the code above will always track the last creep to appear in a tower range. If you didn’t understand I mean assume that we have a tower has three creeps in its sight. Once the first creep leaves the towers range, the tower will now be looking for a new creep to target. The tower will target the next creep in the “for loop” next and recognize that he is the next one farther along – However, since we don’t stop traversing the list at that point the tower will eventually pick the farther one back. Therefore, it is possible, given the code above, to actually have multiple towers along the road and have every other creep that comes out to never be targeted and therefore killed.
To solve this problem, we can put a “break;” in after line 39.
![]() |
In the next tutorial we will be combining both the first and forth article we will now add the ability to destroy the creeps. You can Click Here to go back to the introduction
For the next tutorial our next subject is How to build a tower defense flash game – Part 4 – Starting the tower attack!




[...] part 4 of the the series, you would be well advised to consult the first 3 tutorials in the series, How to build a tower defense flash game – Part 3 – Rotation and Realism before attempting this [...]
[...] How to build a tower defense flash game – Part 3 – Rotation and Realism [...]
This is why I keep going to this place. I can’t believe how many entries I missed since I was here last!
Hi and thanks for a incredible tutorial !!!!! I need help in something, my math is pretty rusty, i was starting to add more waypoints and making the path so that they go in and out an almost full circle. Here comes the problem, when the MC creep1 goes over 180 degrees for the next point it turn almost 360 and continues its normal path, i was fiddling with the code, like changing the PI/180 to 2PI/360 i saw in a radians table, but this just leaves me clueless, and the almost 360 turn persists, i really have no idea how to do this, i would be very grateful for a hand here please !! Oh yeah, still waiting for the “sell tower” tutorial, thanks in advance and again congratulations for a great tutorial !!!
I’d love to see something isometric views. I don’t know the math aspect of isometric perspective very well.
I know exactly what Lestat is saying. It took about 3 hours of numbers through my brain, and a pen on paper to solve the problem.
It’s because the angles go from 0° to 359°, and then back to 0°. For example: to make a 90° turn from 330° to 60°, your creep would turn 270° counter-clockwise instead of 90° clockwise. He still ends up facing the right direction, but it looks really dumb because he spun the wrong way.
Try replacing the rotation code with my own.
The code I used looks something like this:
if ((dir>_rotation+10 && dir_rotation+10-360 && dir_rotation-10+180 && dir_rotation-10-180 && dir<_rotation-10)) {
_rotation -= 10;
} else {
_rotation = dir;
}
That should do the trick!
Crap it didn’t post right. Don’t use that! lol
if ((dir>_rotation+10 && dir_rotation+10-360 && dir_rotation-10+180 && dir_rotation-10-180 && dir<_rotation-10)) {
_rotation -= 10;
} else {
_rotation = dir;
}
The code is too long or something. It completely changed the code. Dont use that either!
if (
(dir>_rotation+10 && dir_rotation+10-360 && dir_rotation-10+180 && dir_rotation-10-180 && dir<_rotation-10)
){_rotation -= 10;
} else {_rotation = dir;}
Ok I’m really sorry but the stupid site keeps changing my code. I give up, it’s impossible. :’(
Ok, what you have to do is rotate clockwise by 10° if the direction you want to face is between 0° and +180° of your current rotation or between -360° and -180° of your current rotation. Rotate counter-clockwise if the direction you want to face is between +180° and +360° of your current rotation or between -180° and 0° of your current rotation.
Ok finally, I hope that clarifies things.
i tryd this over a miljion times but my own created creature wont turn.
it would be nice if you explain this so that one who does not use flash every day in day out wil get this to.
the stupit thing is that if i copy of your code your creep it wil turn and move ( replace option when paste it)
I can’t seem to get my creeps to rotate, any suggestions?
For those that can’t get the creeps to rotate (but towers do):
Make sure you set up the Creep/CreepList correctly! He touched on it in Part 1 but didn’t really explain it in detail.
Create a CreepList movieclip symbol. The actual drawing of the creep goes here. If you had created the Creep movieclip (as I did), you can rename it to CreepList.
Create a Creep moiveclip symbol. Drag the CreepList symbol into its drawing area. Name the instance ‘creep’.
In the Scene 1 area where everything is at, drag the Creep symbol off to the side and name the instance ‘creep1′.
Hope that helps!
yes i cant figure out how to get my creeps to stay on one scene they keep carrying over to my other scenes in my game and their not disappering some one please if you can help me please reply back