Last step. Fixing the monkey direction
There is one final problem to solve. When you try to aim to the left, your monkey still shoots to the right.
Here's what is happening:
Coordinates to the left and up are negative, so in certain positions we find ourselves calculating -2 ÷ -2 = 1.
What's wrong with this? A negative divided by a negative gives you a positive number!
This is correct, but atan has no way to know that we weren't doing a very similar calculation (2 ÷ 2 = 1).
This is how two opposite directions can give the same result.
It turns out that atan
will give the wrong answer any time dx
is negative.
To fix this, whenever dx is negative, we need to add 180 degrees to make it point in the opposite direction. Let's do this now in our code.
> Click on Control Flow, drag out the if do block and place it directly after the set rotation block.
> Click on Operators. Drag out the operator = block and place it in the right side slot of the if block.
> Add the number dx variable block and the 0 number block (found in Operators) in the empty slots and change the operator in the drop down to < ("less than").
> In the do section, set the number angle to number angle + 180. Your code blocks should look like this:
> Click the PLAY button again. If your code is right, your monkey will shoot in the direction you click.
If not, try editing your script.