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!

P#116612 2022-08-30 21:04

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)

P#116621 2022-08-30 22:10 ( Edited 2022-08-30 22:11)

Thanks, I'll check it out.

P#116622 2022-08-30 22:23

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.

P#116640 2022-08-31 04:09
2

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

P#116647 2022-08-31 05:19
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

P#116657 2022-08-31 14:14 ( Edited 2022-08-31 14:15)

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.

P#116673 2022-08-31 18:46 ( Edited 2022-08-31 19:03)

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

P#116675 2022-08-31 18:49

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

P#116750 2022-09-01 21:15

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-02-06 01:01:54 | 0.063s | Q:30