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!
The code
--animated sprite functions --by snjo -- --create new sprite using aspr: --myspr=aspr:new({start=16,frames=4}) --use anim() to draw and --progress animation --use playspr() and stopspr() --to freeze or unfreeze anim function anim(s,x,y) --s: an aspr, x/y: screen position --draws a sprite and updates its animation --frame increases based on sprite width mult = s.frtbl and 1 or s.w local fr = s.start+(flr(s.frame)*mult) --if rest frame is defined, go to that when stopped if s.restfr and not s.playing then fr = s.restfr end flx,fly=s.flpx,s.flpy --anim frames in table: --this next section can be --removed if not used in game if s.frtbl then local t = s.frtbl[fr] if type(t)=="table" then flx,fly = 1==t[2]&1,2==t[2]&2 usefr = t[1] else usefr = t end else usefr = fr end --draw to screen spr(usefr,x,y,s.w,s.h,flx,fly) --progress sprite's frame if s.playing then s.frame = (s.frame+s.spd)%s.frames end if not s.loop then if fr>=s.start+s.frames-1 then stopspr(s) end end end function playspr(s,rst) --s: an aspr, rst:go to first frame? s.playing = true --if reset or sprite is on --the rest frame --if s.frame == s.restfr or rst then if rst then s.frame = s.start end end function stopspr(s,rst) --s: an aspr, rst:go to first frame? s.playing = false --if reset, go to start frame --if (rst) s.frame = 0 if restfr then s.frame = s.restfr elseif rst then s.frame = s.start end end function togglespr(s,rst) --this function is optional if s.playing then stopspr(s,rst) else playspr(s,rst) end end --metatable definition aspr={ --animated sprite --to create a sprite: --myspr=aspr:new({start=16,frames=4}) --required attributes: 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 --more attributes, can be trimmed with code change: frtbl=nil, --use frame table instead of count --each frame can itself a table --{frame,flip} where 1:x, 2:y -- 3:x and y flipped --example: frtbl(16,{16,1}) --(second frame is flipped x) 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: x=0, y=0, --return the new sprite object new=function(self,t) return setmetatable(t or {},{__index=self}) end } |
[Please log in to post a comment]