Log In  


Cart #anim_sprite_snjo-0 | 2025-07-06 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

A simple system for animating through sprites for things like walk cycles.

  • start or stop
  • loop or play once
  • return to a rest sprite if stopped (standing still instead of mid stride)
  • table w/ sprite sequence can define flip x/y to reuse spite
  • large sprites supported (w/h)
  • adjustable speed

check code tab 1 for importable code and tab 2 for example use.

Setup

Using a range of continuous frames

Simple example

guy = aspr:new({start=1,frames=6,spd=0.2,restfr=7})

  • start : the sprite number of the first animation frame
  • frames : number of frames in the loop (if not using a frame table place them in sequence on the sheet)
  • spd : the animation speed. added to the frame count each tick, a value of 1 would switch frames every tick.
  • restfr : an optional sprite number or frame table index to display when the animation is stopped

Any other variable from the aspr variable list can be added here. If not, their default values are used.

Using a table of frames

Using a table of frames, start is 1 since frtbl contains the the frame numbers.
The Rest frame must be in the table, but can be outside the animated range in the table.

The frame table can have a series of sprite numbers mixed with sub-tables to define pairs of frame number and flip state.
frtbl{16,17,18}
or
frtbl{16,{16,1},18} where the sub-table {16,1} means use sprite 16, but flip it horizontally

flip values:
0 : no flip
1 : flip x
2 : flip y
3 : flip x and y

Example

bigdown = aspr:new({frames=2,spd=0.1,w=2,h=2,restfr=3,frtbl={14,{14,1},12}})

Playback functions

function anim(s,x,y)
Show the sprite on screen
s : the aspr object to animate
x/y: position on screen

function playspr(s,rst)
Start the animation.
rst is optional, if True, it moves the animation at the first frame

function stopspr(s,rst)
Stop the animation.
rst is optional, if True, it moves the animation at the first frame

function togglespr(s,rst)
Toggles betwwen playing and stopping the animation.

aspr variables and default values

 start=1       --sprite index in the sheet or table
 frames=1      --number of frames in the loop
 frame=0       --current playing/starting frame
 spd = 0.1     --added to frame count each tick, higher is faster

 frtbl=nil,    -- if nil, use normal `start` and `frames` logic to loop through

 playing=true  --when true, frame increases by speed every time playspr is called
 loop = true   --if false stops when reaching end
 restfr=nil    --optional idle frame when not playing
 w=1           --width in sprite tiles
 h=1           --height in sprite tiles
 flpx=false    --flips the sprite horizontally
 flpy=false    --flips the sprite vertically

--fully optional variables, included for convenience:
 x=0
 y=0

Feedback

If you find this useful or have any helpful feedback, please let me know.
I'm reinventing a lot of wheels here of course, but it's fun!




[Please log in to post a comment]