Sprite movement



One of my students is trying to get their sprite to move on the x coordinates within the grid on its own. We can get it to move from its starting location and move to 375 but how do we get it to continue moving left and right on its own without going off the grid. Does this make sense?



I’m guessing you want the character/sprite to move along the x-axis and then turn around and bounce back along the x-axis the other way?

Using the counter pattern or velocity, you’ll have the sprites.x increase (or decrease) by 1 constantly. Then use a if statement to check the position of the sprite, and if the sprite.x is greater than 400 (or less than 0) you’ll rotate it (to face the correct direction) and then increase or decrease the x accordingly. This is a great opportunity to bring in a math connection to show how you can use positive and negative numbers and multipliers to get it going the correct way.

Let me know if that helps!