Log In  

So I currently have a movement system down for my player, and the sprite changes based on direction. Here's an excerpt:

function make_player()
    p={}
    p.x=3
    p.y=10
    p.sprite=4

end

function draw_player()
 spr(p.sprite,p.x*8,p.y*8)
end

function move_player()
    newx=p.x
    newy=p.y

    if (btn(⬅️)) newx-=.1 
    if (btn(➡️)) newx+=.1
    if (btn(⬆️)) newy-=.1
    if (btn(⬇️)) newy+=.1

    if (can_move(newx,newy)) then
    p.x=mid(0,newx,127)
    p.y=mid(0,newy,63)
    else
    p.x+=0
    p.y+=0
    end
end

function player_anim()
    if (btnp(⬅️)) p.sprite=3
    if (btnp(➡️)) p.sprite=2
    if (btnp(⬆️)) p.sprite=4
    if (btnp(⬇️)) p.sprite=1
end

function player_walk()

end

Now how would I go about adding footstep animations every second or so as a direction is pressed?

P#98768 2021-10-17 03:27

If you're going to do anything fancy later, this thread might help: https://www.lexaloffle.com/bbs/?tid=44686 . It shows how to set things up for in-depth, even if it's probably too much for most pico-8 games.

In general, though, raster animations just need a list of sprites to run through and a timing algorithm. If your game doesn't need the animation to be tied to any mechanics, the timing algorithm can just be a variable with a number that goes up when the player is moving.

In the case of your player_anim() function, this adjustment would set it up for that:

    if (btnp(⬅️)) p.sprite=p.walk_left[p.walk_t%p.anim_speed+1]
    -- then the equivalent for the other directions

Then the tables for animation can be set up in your player_make() function and changed as you decide how your footsteps should look. They would be set like so:

    p.anim_speed=4
    p.walk_t=0
    p.walk_left={3,}
    -- then tables for the other directions

Lastly, you would then need to have the timing variable (what I put as p.walk_t above) change when the player is moving. How to do this depends on both whether you want the animation to reset when the player stops moving and whether you want the player to continuing animating even when there's a wall blocking their movement.

The simplest option is to have the player animate whenever the button is being pressed. For that, this change in move_player would be enough:

    if (p.walk_t>1000) p.walk_t=0 -- to avoid an overflow bug
    if (btn(⬅️)) newx-=.1 p.walk_t+=1
    -- then adding that to the other three as well

Ah, also, if there's anything in the above code you don't understand, I would suggest looking it up, asking about it or experimenting to figure it out. You'll run head-first into brick walls otherwise.

P#98783 2021-10-17 10:30 ( Edited 2021-10-17 10:33)

Thank you very much!

P#98804 2021-10-17 22:11

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2021-12-01 04:07:18 | 0.007s | Q:9