Log In  


I'm new to Pico-8 and game dev. I've been following a YouTube tutorial (Nerdy Teachers) and mostly everything works (the cam gets funky on the right side of the map but that's a different issue). What's killing me is the animation works for jumping, falling (I omitted sliding because I wasn't interested in it) and running right, but when the character moves left the animation is stuck on idle. I added debugging code to print the running variable and it's "true" when facing right but "false" when facing left. The code for movement is simple and the same for left and right, at least in regards to setting the "running" variable. I just can't figure out why it works for right but not for left. Am I missing something obvious? Code below.

Tab 1:
--player
function spawn_player()
plr={
sp=1,
x=59,
y=59,
w=8,
h=8,
flp=false,
dx=0,
dy=0,
max_dx=2,
max_dy=3,
acc=0.5,
boost=4,
anim=0,
running=false,
jumping=false,
falling=false,
landed=false,
}

gravity=0.3
friction=0.75

end

function update_player()
--physics
plr.dy+=gravity
plr.dx*=friction

--controls
if btn(⬅️) then
plr.dx-=plr.acc
plr.running=true
plr.flp=true
else
plr.running=false
end

if btn(➡️) then
plr.dx+=plr.acc
plr.running=true
plr.flp=false
else
plr.running=false
end

--jump
if btnp(❎) and plr.landed then
plr.dy-=plr.boost
plr.landed=flase
end

--check collision ⬆️ and ⬇️
if plr.dy>0 then
plr.falling=true
plr.landed=false
plr.jumping=false

if collide_map(plr,"down",0) then
plr.landed=true
plr.falling=false
plr.dy=0
plr.y-=(plr.y+plr.h)%8
end
elseif plr.dy<0 then
plr.jumping=true
if collide_map(plr,"up",1) then
plr.dy=0
end
end

--check collision ⬅️ and ➡️
if plr.dx<0 then
if collide_map(plr,"left",1) then
plr.dx=0
end
elseif plr.dx>0 then
if collide_map(plr,"right",1) then
plr.dx=0
end
end

--player animation
function animate_player()
if plr.jumping then
plr.sp=6
elseif plr.falling then
plr.sp=7
elseif plr.running then
if time()-plr.anim>.1 then
plr.anim=time()
plr.sp+=1
if plr.sp>5 then
plr.sp=1
end
end
else
plr.running=false
plr.sp=1
end
end
plr.x+=plr.dx
plr.y+=plr.dy

--limit player to map
if plr.x<map_start then
plr.x=map_start
end
if plr.x>map_end-128 then
plr.x=map_end
end
end

function draw_player()
spr(plr.sp,plr.x,plr.y,1,1,plr.flp)
end



In your if btn(➡️) then you have it so if the player isn't going right then running is false, so the result of running left gets ignored.


Yeah, that was it. I was thinking that resetting the variable back to false was needed and it seems I was wrong. Thank you!



[Please log in to post a comment]