Log In  

Hi!

I´m new to pico 8 and I do my first steps in moving an sprite through an dungeon.

that works well, but i the sprite moves on and on while keeping the button pressed

I want to stop after moving one tile in the pressed direction

so for moving on and on you have to press the button over and over again

How can I do this?

I tried it whith

repeat
until btn()==true
return

but it doesnt work

Thanks for help!!

P#70361 2019-11-29 11:17

Hi and welcome!
There are probably multiple ways to do this, but here's what I came up with.

First, you need to keep a few variables:

  • playerX and playerY (numbers): The position of the player in tiles, not pixels.
  • isMoving (boolean): Whether or not the player is moving right now
  • moveX and moveY (numbers): In which direction the player is moving right now
  • moveAmount (number): How much the player has moved from the original position

When a button is pressed and isMoving is false, you have to:

  • Set moveX and moveY to the direction of the pressed key (e.g. if right was pressed, set moveX = 1 and moveY = 0)
  • Set isMoving to true

In your _update function, if isMoving is true, do the following:

  • Increment moveAmount by whatever your move speed is
  • If moveAmount is greater than or equal to 8 (the tile size):
    • Set isMoving to false
    • Change playerX by moveX, and playerY by moveY
    • Set moveAmount to 0

And in your _draw function:

  • Draw the player sprite at (playerX * 8 + moveAmount * moveX, playerY * 8 + moveAmount * moveY)

Obviously, you can name your variables whatever you want, these are just names I came up with.

Also, remember that everything in _update or _draw, including loops, happens instantly in one frame.

Hope this helps!

P#70368 2019-11-29 16:20
:: Udo

Thank you very much for your help!

works ;)

P#70383 2019-11-29 21:31

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-01-18 15:16 | 0.009s | 4194k | Q:17