SpriteAnimation
CPU-side frame sequencer for spritesheet animations. Produces Rect values to pass as sourceRect to SpriteBatch.draw().
Factory methods
Section titled “Factory methods”static fromGrid(texture, options)
Section titled “static fromGrid(texture, options)”Create an animation from a uniform grid spritesheet.
const anim = SpriteAnimation.fromGrid(texture, { columns: 8, rows: 4, frameCount: 30, frameDuration: 0.1, mode: 'loop',})| Option | Type | Default | Description |
|---|---|---|---|
columns | number | required | Grid columns |
rows | number | required | Grid rows |
frameCount | number | columns × rows | Total frames |
frameDuration | number | required | Seconds per frame |
mode | AnimationMode | 'loop' | 'loop', 'once', or 'pingPong' |
static fromRects(rects, options)
Section titled “static fromRects(rects, options)”Create an animation from arbitrary source rectangles.
const anim = SpriteAnimation.fromRects(myRects, { frameDuration: 0.1, mode: 'loop',})Sub-animations
Section titled “Sub-animations”slice(start, end, options?)
Section titled “slice(start, end, options?)”Extract a contiguous range of frames.
const idle = sheet.slice(0, 8)pick(indices, options?)
Section titled “pick(indices, options?)”Pick specific frames in any order.
const attack = sheet.pick([16, 17, 18, 19, 18, 17])Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
currentFrame | Rect | Source rectangle for the current frame |
frameIndex | number | Current frame index |
frameCount | number | Total frames |
isComplete | boolean | true when mode is 'once' and finished |
isPlaying | boolean | Whether playing |
speed | number | Playback speed multiplier |
mode | AnimationMode | Playback mode |
frameDuration | number | Seconds per frame |
Methods
Section titled “Methods”| Method | Description |
|---|---|
update(dt) | Advance by dt seconds |
play() | Resume (resets if complete) |
pause() | Pause at current frame |
reset() | Rewind to frame 0 and start playing |