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.
Something like...
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.
everything needed for grid movement and more: https://www.lexaloffle.com/bbs/?tid=46735
I found a cart that shows exactly what you want: https://www.lexaloffle.com/bbs/?pid=43370
[Please log in to post a comment]