Log In  


Cart [#19159#] | Copy | Code | 2016-03-10 | Link

This is a very simple animation function that anyone can use in their games. It's a bare-bones demo to keep everything as simple as possible. The function itself is only 14 lines and 74 tokens, and works like this:

anim(object, start frame, number of frames, speed (in frames per second), [flip])

In the demo, the object is the player, but it can be anything. Start frame is where the animation to be played begins (so in this case, moving left/right starts on frame 6, so we have anim(player,6,[..])). Number of frames is how many frames in that animation to play. We have 3, so anim(player,6,3,[..]). Speed is how fast the animation should play in frames per second (I have it set to 10 here). Lastly, flip is whether or not to flip the sprite horizontally (this parameter is optional, and the default is false).

To use it in your project, you only need to copy the following code, and call it as described above:

function anim(o,sf,nf,sp,fl)
  if(not o.a_ct) o.a_ct=0
  if(not o.a_st) o.a_st=0


  if(o.a_ct%(30/sp)==0) then
    if(o.a_st==nf) o.a_st=0


I hope this helps some of you who have had questions about how to animate in Pico-8. Good luck and have fun! :)

P#19160 2016-03-10 09:50


Just wanted to say, thank you for that.
I have used this post as a base for my own function. One that is little more capable, but not as advanced as your advanced animation (which I was having a hard time wrapping my head around). I call it intermediate animation! Here it is:

First. We setup the animations for our actor inside the table, similar to your advanced function:

  sprite={65,81}, --composed of two sprites

--animations //this is where you will store all your animations in order, each animation is its own table

  --down animation
    {function() p.sprite[2]=81 p.spflip[2]=false end,
    function() p.sprite[2]=97 end,
    function() p.sprite[2]=81 end,
    function() p.sprite[2]=97 p.spflip[2]=true end} 
 --each step is a function that will change the frame/tile and let it be flipped independently.

   --another animation
    {another set of functions}

The advantage of this method is that you can call any tile, not only the one next to each other. It also let's you reuse tiles. In my example I used 2 tiles to create a 4 frame animation. Now to the animation function itself:

--table that holds the actor, animation index(number), how many ticks per frame 
function ani(actor,var,speed) 
 if(not a_ct) a_ct=0 --animation counter
 if(not a_step) a_step=1 --current animation step

 a_ct+=1 --add one to the count each frame

 if(a_ct%speed==0) then
  if(a_step>#actor.ani[var]) a_step=1

 actor.ani[var][a_step]() --call the function corresponding to current step

This code does not draw the sprite. I draw the sprites in a separate function, because I also am doing other things with it as I am calling it (drawing outline, dropping shadow). However, a spr call could be put at the end of the function.

I hope that this can be useful to someone. Please, bear in mind, I am still a newbie when it comes to coding, so my solution might not be very efficient. If anyone sees any way to improve on it, please let me know!

Once again, thanks Scathe for the tutorial that led me to this solution!

P#35508 2017-01-12 01:06


What does the 'st' and the 'fr' stand for?

P#44811 2017-10-01 08:21

Log in to post a comment


New User | Account Help
:: New User
About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2017-10-24 00:18 | 0.174s | 1572k | Q:19