Log In  

Cart #19159 | 2016-03-10 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
23

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

  o.a_ct+=1

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

  o.a_fr=sf+o.a_st
  spr(o.a_fr,o.x,o.y,1,1,fl)
end

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 ( Edited 2018-04-21 04:34)

1

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:

actor={
  x=60,
  y=60,
  sprite={65,81}, --composed of two sprites
  spflip={false,false},
  speed=1,

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

  --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
  a_step+=1
  if(a_step>#actor.ani[var]) a_step=1
 end

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

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 ( Edited 2017-01-12 06:09)

it dosent work for me

P#51828 2018-04-21 00:34 ( Edited 2018-04-21 04:34)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 15:49:19 | 0.011s | Q:15