# Slow Tower for a Tower Defense Game

I am making a tower defense game in Flash and I have a slow tower that is built directly on the path. I have it setup to use a hitTestObject function.

The tower is in its own class and the enemies are in their own class.

In the update function for the tower, it has

if (this.hitTestObject(enemy))
{
enemy.slow = true;
}


In the enemy class update function, I have:

if (slow == true)
{
finalSpeed = speed/2;
}
else
{
finalSpeed = speed;
}


This seems to work fine until I build a second slow tower. Whenever I build the second one, it works, but the first one stops working (it no longer slows enemies). However, it is still detecting the collisions with the enemies. I don't understand why it doesn't slow them down though.

• Half your post is missing. – The Communist Duck May 23 '11 at 14:47
• Can you describe exactly what doesn't work with the first tower when the second is built? Should they cumulate the speed reduction (and it doesn't) or should it behave another way ? – Valkea May 23 '11 at 14:51
• @Valkea as i can understand from his question i think he means after building the second tower the first tower doesn't slow enemies anymore. – Ali1S232 May 23 '11 at 15:57
• Yes, my problem is, once the second tower is built, the first one stops working (it no longer slows enemies). However, it is still detecting the collisions with the enemies. I don't understand why it doesn't slow them down though. – Sean May 23 '11 at 16:03
• Is the enemy's slow variable being set but the speed is not changing? or is the slow variable not being set? – lathomas64 May 23 '11 at 16:47

## 2 Answers

I can't tell for sure without seeing more of the code, but what it sounds like is that something involving the later tower is setting the .slow of enemies it's not hitting back to false.

I haven't really worked with actionscript, but I'd look into watching when that variable changes to false. If it's getting set to true but when the enemies movement comes around it is back to false, something between the two has to be resetting it.

• I think that is exactly what is happening. I didn't think of it until you said it. I need to figure out a way to set it back to false without constantly doing it. – Sean May 23 '11 at 17:51
• I would recommend putting the slow on some sort of countdown. That way each time the enemy moves it would subtract 1 from it, and each time it's hit by a slow tower you can set it to a fixed number. If slowTimer == 0 you move normal speed, but if slowTimer > 0 you know you need to apply the slowdown effect and subtract 1 from slowTimer. If one tower keeps hitting the enemy, it will keep resetting to the fixed number, avoiding stacking the slowdown or pre-emptively turning it off. – Lunin May 23 '11 at 18:04
• and if you don't what to use slow timer you can set slow to false right after you compute final speed. – Ali1S232 May 23 '11 at 19:15
• I ended up using a countdown, but instead of time, it uses distance traveled. So basically, once the enemy is not over the tower any more, it will speed up. – Sean May 24 '11 at 0:49

i guess for some reason the first if statement which should be working for every tower is only working for the latest tower build, (maybe it's just an instancing problem, eg. you just create an instance for tower shape but not from the whole tower class)

some new ideas : what if you just unmark slow flag whenever enemy is not near a tower (you know some if/else statement) and that is cuasing tower 2 overwrite what tower 1 flaged?