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!