I'm creating a small game where a player is on a grid of tiles. I want the player to move smoothly from one tile to the next, not appear instantly in the next tile. I tried to create a delay loop in the update function when the direction key was pressed, but the sprite still appears instantly 8 pixels to the left, right, etc.
The sprite is drawn, then when I hit, say, right, then the loop increments the x position, but it doesn't draw the sprite again until the loop is completed. How would I go about moving the sprite 1 pixel per frame for 8 pixels then stop?
I can sort of imagine what I need to do, but it's just a sort of jumble in my mind right now!
Since it sounds like you're just starting out, a beginner-friendly method for smooth tile-based movement is to use the modulo (%) operator to check if the sprite's x/y are divisible by 8 with no remainders.
if player.x % 8 == 0 then
The basic idea here is that the modulo operator will divide the player's x by 8 and return only the remainder, and if the remainder is equal to 0, you can set some code to stop further movement. The remainder will only be 0 when player.x is 8, 16, 24, 32, etc.
Of course, it gets more complicated when you factor in controls.
Thank you so much, guys, for your responses. The Pico-8 community is great!
@dw817 - thanks for the code, this is exactly what I was looking for. I'll study it and see where I was going wrong!
@freds72 - very interesting blog post. I'll definitely be checking it out further.
[Please log in to post a comment]