Keys

Z - Next Step
X - Auto/Manual


Code

This is the code displayed in the tutorial. Feel free to copy/paste it for your own use.

Note: You can use as many or as few sprites as you want in the sp table, and in any order you want.

function _init()
 t,f,s=0,1,4 --tick,frame,step
 sp={1,2,3,4,5,6} --sprites
end
function _update()
 t=(t+1)%s --tick fwd
 if (t==0) f=f%#sp+1
end
function _draw()
 spr(sp[f],x,y)
end

Here's another implementation that assumes the tick, frame, and step variables and the sp table are all part of another table. (In this case, p, for player.)

function _init()
 p={}               --player
 p.t,p.f,p.s=0,1,4 --tick,frame,step
 p.sp={1,2,3,4,5,6} --sprites
end
function _update()
 animate(p)
end
function _draw()
 spr(p.sp[p.f],x,y)
end
--animate an object
function animate(o)
 o.t=(o.t+1)%o.s --tick fwd
 if (o.t==0) o.f=o.f%#o.sp+1
end
StatusReleased
PlatformsHTML5
Rating
Rated 5.0 out of 5 stars
(10 total ratings)
AuthorMBoffin (Dylan Bennett)
GenreSimulation, Educational
Made withPICO-8
TagsPICO-8

Development log

Comments

Log in with itch.io to leave a comment.

(+1)

I was struggling to animate 16x16 sprites until I used your code.

Massive Thanks!

Oh, hello! How to you animate 16x16 sprites? im trynig putting a "+1" on the spr function for wide and hight but prints more sprites on the spritesheet! (glitchy)
spr(p.sp[p.f],x,y,p.sp[p.f]+1,p.sp[p.f]+1)

You would just use spr(p.sp[p.f], x, y, 2, 2) where the 2,2 indicates the width and height (in units of 8x8 sprites) of the sprite you want to draw.

thank you so much!

(+2)

Thank you for this tutorial!

Also, there is a syntax error in line 3 (a dot before p.f).

(+1)

Very good explanation, thank you!

I think this could be made a bit smaller by making use of FLR() to divide the modulos. Since it's one-sprite, maybe don't need 2-variables to keep track of it ? Maybe ...

Exactly what I needed! Thank you very much!

(+1)

Modulus is totally the secret sauce.  You just reduced my 2 hour struggle to figure that out into a handful of code.  Thanks!

(+1)

this is excellent , and general purpose. thank you!