Log In  


Hello world,
I'm brand new to coding and Pico 8. I have created a sprite and figured out how to get it to move, and even flip when he changes directions. I have a second sprite that makes it look like he is taking a step.
My main problem is I can't figure out how to get it to swap between these 2 frames when you move him around. Whenever you press up, down, left or right, I want him to take a step (switch between the 2 frames).
Can anyone point me toward a resource to help me figure it out?
Thanks in advance



Check out @SpaceCat on YouTube. He has videos that break down everything in a wonderful way.

SPACECAT - BASIC ANIMATION


see that excellent tutorial:
https://www.lexaloffle.com/bbs/?tid=44686


Option 1

A short and dirty way for me is to just establish a tick-tock variable that you change whenever a global animation timer runs out.
So wherever your timer is,add:

If time()>timer then
 Ticktock=1-Ticktock
End

This allows you to change it for dual frame animations,but you could also write some extra logic for more frames.
And for your "animation" just check wether the direction vector is unequal to a (0,0)

If dir.x!=0 or dir.y!=0 then
 --if direction vector(or your movement speed) is not (0,0), so if your character is moving
 Spr(1+ticktock,p.x,p.y)
Else
 Spr(1,p.x,p.y)
End

Your two animationsprites need to be next to each other for this to work though.
The Ticktock solution can be useful for other things, blinking text etc. But one even shorter that way works with %(Modulo, basically the remainder of a division) and is based on time is this.

Option 2

If dir.x!=0 or dir.y!=0 then
 --if direction vector(or your movement speed) is not (0,0), so if your character is moving
 spr(SPRITE_START_FRAME+flr((time()*ANIMATION_SPEED)%AMOUNT_OF_FRAMES),x,y)
Else
 Spr(SPRITE_IDLE_FRAME,p.x,p.y)
End

You can multiply time() by something to achieve a higher or slower animation speed.

Here it is wrapped up in a neat function(that @merwok improved):

function spr_anim(n,x,y,nframes,speed,flip_x,flip_y)
    local speed=speed or 1
    local nframes=nframes or 2
    spr(n+(time()*speed)%nframes,x,y,1,1,flip_x,flip_y)
end

Is there a way to do it without a timer? I don't want the sprite to move unless a direction is pressed


if you're using a static sprite when not moving you would just have to alter the code so that it uses the math for animation when a button is pressed.

EDIT: In fact the link I gave for SpaceCat's video does exactly what you're asking.


And that is literally option 2,ill copy it here again and explain it:

function spr_anim(n,x,y,nframes,speed,flip_x,flip_y)
    local speed=speed or 1
    local nframes=nframes or 2
    spr(n+(time()*speed)%nframes,x,y,1,1,flip_x,flip_y)
end

This function is used similarly to the regular spr() function, but it has two additional parameters. The number of frames(in your case 2) and the speed of the animation(5 worked great for me).
It works by taking the Modulo (the remainder of a division,in this case divided by your number of frames) of the time() (the time elapsed since your program started), which is always a nr between 0 and the max frame. You can call it like this in your game. Let's say your walking sprites are 120 and 121.

spr_anim(120,x,y,2,5,flip_player_sprite_x,flip_player_sprite_y)

And you can use this logic to make the animation dependend on movement.
It checks the Players movement variables, if they're not 0 the player is moving. You could also substitute this with btn() presses,but that .ight lead to unwanted behavior.

If dir.x!=0 or dir.y!=0 then
 --if direction vector(or your movement speed) is not (0,0), so if your character is moving
 spr_anim(120,x,y,2,5,flip_player_sprite_x,flip_player_sprite_y)
Else
 Spr(SPRITE_IDLE_FRAME,p.x,p.y)
End

Do you know how functions work? They are a way to establish custom methods, you can copy them anywhere outside of your _update,_init and _draw functions. They are very handy :)


Nerdy teachers would be great way to start pico 8



[Please log in to post a comment]