Log In  


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!



If you want to have a smooth move point from any point to any other, check out @dw817's comment here

(It's faster than my trig method)


Thanks, I'll check it out.


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.


2

everything needed for grid movement and more: https://www.lexaloffle.com/bbs/?tid=46735


3

Thanks for the shoutout, @mrawesome !

Hi @Gannett.

Here is some code I wrote for you to examine for simple grid movement.

Cart #riramoniye-0 | 2022-08-31 | Code ▽ | Embed ▽ | No License
3


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.


You are very welcome, @Gannett. We are all friends here. :)


I found a cart that shows exactly what you want: https://www.lexaloffle.com/bbs/?pid=43370



[Please log in to post a comment]