Log In  


I got my player character to walk, jump, die, and be idle. But I haven't been able to get its attack animation beyond one frame.

For context, this is how I update and draw the player:

function _init()
 plr={
  x=10,     --x position
  y=75,     --y position
  dx=0,     --movement direction
  fl=false, --flip sprite
  sp=1,     --sprite
  state="idle",
 }

function _update()
 moveplayer()
 animate_plr()
end

function _draw()
 spr(plr.sp,plr.x,plr.y,1,1,plr.fl)
end

function moveplayer()

 --attack
 if btn(🅾️) then
  plr.state="attack"

 --move left and right 
 elseif btn(⬅️) then
  plr.dx=-1
  plr.fl=true
  plr.state="walk"
 elseif btn(➡️) then
  plr.dx=1
  plr.fl=false
  plr.state="walk"
 else
  plr.dx=0
  plr.state="idle"
 end
 plr.x+=plr.dx
end

To animate, I use the below function. plr.sp calls the sprite number, which are as follows:
1-2 = Walk
2 = Jump
3-5 = Attack
8 = dead

function animate_plr()

 --handle animation state
 if plr.state=="dead" then
  plr.sp=8
 --idle
 elseif plr.state=="idle" then
  plr.sp=1
 --attack
 elseif plr.state=="attack" then
  plr.dx=0
  plr.sp=3
  while plr.sp<5 do
   plr.sp+=1
  end
 --walk
 elseif plr.state=="walk" then
  if plr.sp<2.75 then
   plr.sp+=0.25
  else
   plr.sp=1
  end
 --jump
 elseif plr.state=="jump" then
  plr.sp=2
 end
end

So the problem is that when I use btnp in the code above it only shows sprite 5 and then immediately switches back to the walk frames. If I use btn then the character just stays frozen in sprite 5.

What I want to achieve, is to have it go through all three attack sprites (3, 4, 5) in succession and then revert back to its previous state, e.g. walking animation or standing still.

Thanks in advance.



When/where do you call animate_plr()?

Just at a cursory glance, in that function you check for state == "attack" and if it is, you set the player sprite to 3 and immediately go into a loop that adds one until it hits 5. That's all going to happen in a single frame prior to drawing, so I'd expect that you'd only see sprite 5 and not sprites 3 & 4. But I could be missing something since I don't know where/when you're calling animate_plr().


Ah yes, sorry. I amended it in my post now.

animate_plr() is called in _update() after moveplayer().
And indeed it is sprite 5 you end up seeing and not sprite 3.



[Please log in to post a comment]