Log In  


Hi there!

I've been using a relatively simple system for sprite animations that I find works pretty well without much overhead. It relies on a simple function to retrieve the current frame of an animation frame list (ani) based on time (t()) and a given speed (spd) in frames per second:

--get animation frame
function anifrm(ani,spd)
 return ani[1+flr(spd*t()%#ani)]
end

That's it!

Now all we need is to define an animation as a list of sprite IDs:

--define player walk animation
pwalkani={2,3,4,5}

And we can assign this animation to our player just like assigning a single sprite:

--in _update(), during walk logic (e.g. btn left or right)
player.sprite=anifrm(pwalkani,10)

--in _draw(), using the normal spr() call
spr(player.sprite,player.x,player.y)

Here's an example cart to demonstrate fully, also taking sprite flipping into account:

Cart #koz_anidemo-1 | 2024-06-23 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1


Full code:

player={
 x=60,
 y=60,
 sprite=1,
 sprflip=false
}

--define player walk animation
pwalkani={2,3,4,5}

--get animation frame
function anifrm(ani,spd)
 return ani[1+flr(spd*t()%#ani)]
end

function _update()
 if btn(⬅️) then
  player.x-=1
  player.sprite=anifrm(pwalkani,10)
  player.sprflip=true
 elseif btn(➡️) then
  player.x+=1
  player.sprite=anifrm(pwalkani,10)
  player.sprflip=false
 else
  player.sprite=1
  player.sprflip=false
 end
end

function _draw()
 cls()
 spr(player.sprite,player.x,player.y,1,1,player.sprflip)
end

Be aware that because this function is tied to global time (t()) the current frame shown isn't dependent on when the action began/the animation was set.

Good luck and feel free to let me know if there are any questions or comments about this function or its usage!

1


Nice. For those who need the timed animation to be relative to the start of movement, I added a 3rd optional parameter that is the time stamp of the start of the animation.

Cart #zezitesipa-0 | 2024-07-12 | Code ▽ | Embed ▽ | No License

Full code

player={
 x=60,
 y=60,
 sprite=1,
 sprflip=false
}

roller={
 x=40,
 y=60,
 sprite=17,
 sprflip=false
}

--define player walk animation
pwalkani={2,3,4,5}
rwalkani={18,19,20,21,22,23,17}

--get animation frame
function anifrm(ani,spd,tstart)
 tstart=tstart or 0
 return ani[1+flr(spd*(t()-tstart)%#ani)]
end

function _update()
 if btn(⬅️) then
  player.x-=1
  player.sprite=anifrm(pwalkani,10)
  player.sprflip=true

  roller.tanim=roller.tanim or t()
  roller.x-=1
  roller.sprite=anifrm(rwalkani,5,roller.tanim)
  roller.sprflip=true

 elseif btn(➡️) then
  player.x+=1
  player.sprite=anifrm(pwalkani,10)
  player.sprflip=false

  roller.tanim=roller.tanim or t()
  roller.x+=1
  roller.sprite=anifrm(rwalkani,5,roller.tanim)
  roller.sprflip=false

 else
  player.sprite=1
  player.sprflip=false

  roller.sprite=17
  roller.tanim=false
 end
end

function _draw()
 cls()
 spr(player.sprite,player.x,player.y,1,1,player.sprflip)
 spr(roller.sprite,roller.x,roller.y,1,1,roller.sprflip)

end



[Please log in to post a comment]