Sprite Rotation Help

Hi,

I am having trouble with why the sprite rotation isn’t working. It isn’t responding to the key press. Can you help?

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Link to the project or level: Game Lab - Code.org

What I expect to happen: Press up or down keys and the cannon on the helicopter should rotate

What actually happens: No rotation

What I’ve tried: Verifying code is in the right place, adding rotation speed

Greetings @Flynn_Lives,

You actually have 2 problems with the code:

You are declaring the sprite variables in the draw loop, causing the same variables to be created each frame. This can be easily fixed by moving the sprite creation outside of the draw loop, including the background drawing function; it’s not needed.

The next problem is how you rotate it. You are setting the rotation. Not adding/subtracting it. So you should use the operators like -= or +=.

  if (keyDown("up")) {
    attatchment.rotation += 3;
  }
  if (keyDown("down")) {
    attatchment.rotation -= 3;
  }

I have an example here: Game Lab - Code.org

2 Likes

Thank you! I appreciate the help