3
\$\begingroup\$

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.

\$\endgroup\$
5
  • 5
    \$\begingroup\$ Half your post is missing. \$\endgroup\$ Commented May 23, 2011 at 14:47
  • 1
    \$\begingroup\$ 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 ? \$\endgroup\$
    – Valkea
    Commented May 23, 2011 at 14:51
  • \$\begingroup\$ @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. \$\endgroup\$
    – Ali1S232
    Commented May 23, 2011 at 15:57
  • \$\begingroup\$ 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. \$\endgroup\$
    – Sean
    Commented May 23, 2011 at 16:03
  • \$\begingroup\$ Is the enemy's slow variable being set but the speed is not changing? or is the slow variable not being set? \$\endgroup\$
    – lathomas64
    Commented May 23, 2011 at 16:47

2 Answers 2

4
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – Sean
    Commented May 23, 2011 at 17:51
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Lunin
    Commented May 23, 2011 at 18:04
  • \$\begingroup\$ and if you don't what to use slow timer you can set slow to false right after you compute final speed. \$\endgroup\$
    – Ali1S232
    Commented May 23, 2011 at 19:15
  • \$\begingroup\$ 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. \$\endgroup\$
    – Sean
    Commented May 24, 2011 at 0:49
0
\$\begingroup\$

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?

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .