Last Cannon issues - Javascript

Hello, I would like to know what is wrong, I have tried to make a code but it does not work, it says error because I must protect the artillery, how do I fix it? Could someone give me a hand please? :frowning:


image

I think your indentation is wrong after that while true loop. If it is wrong like I suspect, than you’ll only be attacking once, and thus not protecting the artillery very well.

I don’t think that’s the problem @enPointe77.
@Matignacio, the problem is the fact that you haven’t included your code where you attack the enemy inside your move(x, y) function. Code runs from top to bottom, so firstly, starting at the while (true), the code will check if there is an enemy, and because it’s the start of the level, there isn’t one. Next, the code will run: move(30, 10);, which will make the hero moveXY to that point, because that’s what you have in your move(x, y) function. Next, disregarding any potential enemies, your hero will move to 20, 10, then to 10, 30, because code runs downwards, so even if there is an enemy, your hero will not attack it.
This is why you need to put your code for checking for and attacking an enemy inside your move(x, y) function so that you can moveXY to a location, then attack an enemy if it’s there, rather than ignoring the enemy as you are at the moment.
Danny

1 Like