Log In  


trouble with animation

i am learning pico 8 and watching some videos to kind of learn how to do things but not copy directly so i actually retain knowledge, and im animating sprite and i got it to work going right and left but not up and down. im wondering if anyone can look at my code and see if its glaringly obvious? its my second day learning game dev in general so any notes would be helpful. here is the code:

okay, the bbs will not let me post my code in a nice little box, it cuts it off very short, so excuse the wall of text if anyone has tips on that too, i would appreciate it.

poke( 0x5f2e, 1 )
pal({[0]=0,4,128,133,1,129,3,139,140,12,14,15,135,138,9,7},1)
-- color palette --

function _init()

position=63

player={
x=63,
y=63,
f=false,
sp=1,
speed=1,
stimer=0,
ani_speed=10,
first_frame=1,
last_frame=4,

}

end

function _update()

--amimation player--
if btn(➡️) then
if player.stimer<player.ani_speed then
player.stimer+=1
else
if player.sp<player.last_frame then
player.sp+=.3
else
player.sp=player.first_frame
end

stimer=0

end
end

if btn(⬅️) then
if player.stimer<player.ani_speed then
player.stimer+=1
else
if player.sp<player.last_frame then
player.sp+=.3
else
player.sp=player.first_frame
end

stimer=0

end
end

if btn(⬇️) then
if player.stimer<player.ani_speed then
player.stimer+=1
else
if player.sp<player.last_frame then
player.sp+=.3
else
player.sp=player.first_frame
end

stimer=0

end
end

if btn(⬆️) then
if player.stimer<player.ani_speed then
player.stimer+=1
else
if player.sp<20 then
player.sp+=.3
else
player.sp=17
end

stimer=0

end
end

--player movement--
if btn (➡️) then
player.x+=player.speed
player.f=false
end
if btn (⬅️) then
player.x-=player.speed
player.f=true
end

if btn (⬇️) then
player.y+=player.speed
player.sp=1
end

if btn (⬆️) then
player.y-=player.speed
player.sp=17
end

end
--speed on roads--
// if mget(flr((player.x+4)/8),flr((player.y+4)/8))==27 then
// player.speed=1.5
// end

function _draw()
cls(7)
map()
spr(player.sp,player.x,player.y,1,1,player.f)

end



2

It looks like for both up and down, you're resetting the player sprite to a fixed number after you calculate it. You have two if() statements for both up and down. In the first one, you do this:

if btn(⬇️) then
  if player.stimer<player.ani_speed then
    player.stimer+=1
  else
    if player.sp<player.last_frame then
      player.sp+=.3
    else
      player.sp=player.first_frame
    end

    stimer=0

  end
end

But then later, you just set it directly to 1 for down or 17 for up, so whatever you set it to earlier is replaced:

if btn (⬇️) then
  player.y+=player.speed
  player.sp=1
end

So if you're pressing the down button, you're doing both of those if blocks, so whatever you set the sprite to in the first one always gets overridden and set to 1 when the second one executes. Same for the up direction.


1

Oh my gosh. That makes sense! Thank you so much.



[Please log in to post a comment]