I can not get the sprite “player” to move left and right when keys are pressed. I am trying to only use the code the students see, so there is no “while key pressed” option on this game lab project. I have tried moving the variables all around, then they are at the beginning then the movement code says the variable is undeclared but when they are in the If then it doesn’t move (or moves but keeps showing them). Thoughts? Game Lab - Code.org
The var createSprite and setAnimation for the cave and the player should be at the top of the code above and outside the draw loop (so move those 4 blocks out of the draw loop). This is because when you create the sprite in the draw loop the x and y positions are constantly being reset to the x and y coordinates in the create sprite block. In your code, that means the xLocation is always 30 and so does not move. Then, in the draw loop use the sprite.x =sprite.x +1 counter pattern to change the x position of the sprite and move the sprite to the right. Use subtraction to have it move left. Using this method, you do not need the xLocation variable.
Hope that helps!