Spritesheet animation
A character that runs, a coin that spins, a flame that flickers: frame-by-frame animation from a spritesheet.
engine.animation() makes an animated sprite, a sprite that steps through frames on
game time.
await engine.load({ hero: 'art/hero.json' })const hero = stage.add(engine.animation('hero/run', { fps: 12, x: 200, y: 500, anchor: 0.5 }))Where the frames come from
Section titled “Where the frames come from”engine.animation(frames, props) takes:
- an animation of a loaded atlas, as
'atlas/name':- the
animationsof a TexturePacker or PixiJS atlas ("animations": { "run": ["run_0.png", "run_1.png", …] }); - the tags of an Aseprite export (
meta.frameTags), with their direction (forward, reverse, ping-pong) and each frame’s own duration;
- the
- a prefix: every loaded or baked frame whose name starts with it, in natural order (
run_2beforerun_10):engine.animation('hero/run_'); - a list of frame names (or
Frameobjects):engine.animation(['coin/0', 'coin/1', 'coin/2', 'coin/1']).
A name that is neither an animation nor a prefix of any frame throws no animation or frames "name…".
Aseprite: export the sheet as JSON (hash or array) with tags, without rotation. Each tag is an animation named after it.
Options
Section titled “Options”The sprite’s own fields (x, y, anchor, scale, slice, …) and:
| Option | Default | What it is |
|---|---|---|
fps |
12 |
Frames per second. Frames with their own durations (Aseprite) use those, unless fps is given |
loop |
true |
Start again after the last frame; false: stop on it and call onComplete |
playing |
true |
Start playing at once |
speed |
1 |
Time scale: 2 twice as fast, 0.5 half, -1 backwards (from the last frame) |
onComplete |
Called when an animation that doesn’t loop reaches its end |
Control
Section titled “Control”| What it does | |
|---|---|
play() |
Plays from where it is; a finished animation that doesn’t loop starts again |
stop() |
Stops on the current frame |
gotoFrame(i) |
Shows frame i (from 0), playing or not |
gotoAndPlay(i), gotoAndStop(i) |
Shows frame i, then plays or stops |
frameIndex, frameCount, playing |
Where it is |
fps, loop, speed and onComplete are fields too: change them while it plays.
const pop = stage.add(engine.animation('fx/pop', { loop: false, x: star.x, y: star.y, anchor: 0.5 }))pop.onComplete = () => pop.destroy()It plays on game time: it freezes while the game is paused or Fabi Play shows a break. When the game hitches it skips frames to stay on time, rather than slowing down.
An animated sprite’s frames are fixed when it is made. For a character with several animations (run, jump, idle),
make one animated sprite for each in a group, move the group, and show one at a time
(visible).