Skip to content

SpriteAnimation

CPU-side frame sequencer for spritesheet animations. Produces Rect values to pass as sourceRect to SpriteBatch.draw().

Create an animation from a uniform grid spritesheet.

const anim = SpriteAnimation.fromGrid(texture, {
columns: 8,
rows: 4,
frameCount: 30,
frameDuration: 0.1,
mode: 'loop',
})
OptionTypeDefaultDescription
columnsnumberrequiredGrid columns
rowsnumberrequiredGrid rows
frameCountnumbercolumns × rowsTotal frames
frameDurationnumberrequiredSeconds per frame
modeAnimationMode'loop''loop', 'once', or 'pingPong'

Create an animation from arbitrary source rectangles.

const anim = SpriteAnimation.fromRects(myRects, {
frameDuration: 0.1,
mode: 'loop',
})

Extract a contiguous range of frames.

const idle = sheet.slice(0, 8)

Pick specific frames in any order.

const attack = sheet.pick([16, 17, 18, 19, 18, 17])
PropertyTypeDescription
currentFrameRectSource rectangle for the current frame
frameIndexnumberCurrent frame index
frameCountnumberTotal frames
isCompletebooleantrue when mode is 'once' and finished
isPlayingbooleanWhether playing
speednumberPlayback speed multiplier
modeAnimationModePlayback mode
frameDurationnumberSeconds per frame
MethodDescription
update(dt)Advance by dt seconds
play()Resume (resets if complete)
pause()Pause at current frame
reset()Rewind to frame 0 and start playing