Question about distanceTo

so I keep trying to do distanceTo but this happens

I have been doing it like this for a while but it isn’t working now

Ah you need to define enemy right on the line above the line marked by the error. It happens to me too

That may work in some cases, but really you should follow the duck’s instructions: use if. How can you work out the distance to an enemy if there is no enemy? There is an enemy on the screen now but I bet the error occurred when there were no enemies… hero.distanceTo() does not have a built in conditional statement, it doesn’t find the distance if there is an enemy, it just tries to find the distance even when there isn’t an enemy, which causes the error.

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.