Animator

The Animator component plays sprite animations. It holds a map of named animations and controls which one is playing, its speed, and its playback state. It works together with a SpriteRenderer on the same entity.

Options

OptionTypeDefaultDescription
animationsMap<string, Animation>emptyNamed animations available to the component.
animationstringThe name of the animation to play.
speednumber1Playback speed multiplier.
playingbooleanfalseWhether the animation is playing.
ignoreTimeScalebooleanfalseIf true, playback ignores the time scale.

Read-only properties

PropertyTypeDescription
currentFramenumberThe current frame of the playing animation.
currentTimenumberThe elapsed time of the playing animation.

Methods

MethodDescription
play(animation?)Plays the animation. If a name is given, switches to it and resets.
pause()Pauses playback, keeping the current frame.
stop()Stops playback and resets to the first frame.

Animation

Each entry in animations is an Animation, which describes the source image and frame sequence:

OptionTypeDefaultDescription
imageHTMLImageElement | HTMLImageElement[] | string | string[]The source image(s) or asset name(s).
slice{ size: Vector2; offset?: Vector2; padding?: Vector2 }How to cut frames out of a sprite sheet.
framesnumber[][]The sequence of frame indices to play.
fpsnumber12Frames per second.
loopbooleanfalseWhether the animation repeats.

Example

import { Transform, SpriteRenderer, Animator, Animation, Vector2 } from "angry-pixel";

const idle = new Animation({
    image: "player.png",
    slice: { size: new Vector2(32, 32) },
    frames: [0, 1, 2, 3],
    fps: 10,
    loop: true,
});

this.entityManager.createEntity([
    new Transform(),
    new SpriteRenderer(),
    new Animator({
        animations: new Map([["idle", idle]]),
        animation: "idle",
        playing: true,
    }),
]);