Class: AnimatedSprite

AnimatedSprite

An AnimatedSprite is a simple way to display an animation depicted by a list of textures.

import { AnimatedSprite, Texture } from 'pixi.js';

const alienImages = [
    'image_sequence_01.png',
    'image_sequence_02.png',
    'image_sequence_03.png',
    'image_sequence_04.png',
];
const textureArray = [];

for (let i = 0; i < 4; i++)
{
    const texture = Texture.from(alienImages[i]);
    textureArray.push(texture);
}

const animatedSprite = new AnimatedSprite(textureArray);

The more efficient and simpler way to create an animated sprite is using a Spritesheet containing the animation definitions:

new AnimatedSprite (textures, autoUpdate)

Name Type Attributes Default Description
textures Texture[] | AnimatedSprite.FrameObject[]

An array of Texture or frame objects that make up the animation.

autoUpdate boolean <optional>
true

Whether to use Ticker.shared to auto update animation time.

Example

 import { AnimatedSprite, Assets } from 'pixi.js';

 const sheet = await Assets.load('assets/spritesheet.json');
 animatedSprite = new AnimatedSprite(sheet.animations['image_sequence']);

Extends

  • Sprite

Interface Definitions

FrameObject

Properties:
Name Type Description
texture Texture

The Texture of the frame.

time number

The duration of the frame, in milliseconds.

Members

animationSpeed number

The speed that the AnimatedSprite will play at. Higher is faster, lower is slower.

Default Value:
  • 1

autoUpdate boolean

Whether to use Ticker.shared to auto update animation time.

currentFrame number

The AnimatedSprite's current frame index.

loop boolean

Whether or not the animate sprite repeats after playing.

Default Value:
  • true

onComplete () => void

User-assigned function to call when an AnimatedSprite finishes playing.

Example
animation.onComplete = () => {
    // Finished!
};

onFrameChange (currentFrame: number) => void

User-assigned function to call when an AnimatedSprite changes which texture is being rendered.

Example
animation.onFrameChange = () => {
    // Updated!
};

onLoop () => void

User-assigned function to call when loop is true, and an AnimatedSprite is played and loops around to start again.

Example
animation.onLoop = () => {
    // Looped!
};

playing boolean readonly

Indicates if the AnimatedSprite is currently playing.

The array of textures used for this AnimatedSprite.

totalFrames number readonly

The total number of frames in the AnimatedSprite. This is the same as number of textures assigned to the AnimatedSprite.

Default Value:
  • 0

updateAnchor boolean

Update anchor to Texture's defaultAnchor when frame changes.

Useful with sprite sheet animations created with tools. Changing anchor for each frame allows to pin sprite origin to certain moving feature of the frame (e.g. left foot).

Note: Enabling this will override any previously set anchor on each frame change.

Default Value:
  • false

Methods

AnimatedSprite.fromFrames (frames) AnimatedSprite static

A short hand way of creating an AnimatedSprite from an array of frame ids.

Name Type Description
frames string[]

The array of frames ids the AnimatedSprite will use as its texture frames.

Returns:
Type Description
AnimatedSprite
  • The new animated sprite with the specified frames.

AnimatedSprite.fromImages (images) AnimatedSprite static

A short hand way of creating an AnimatedSprite from an array of image ids.

Name Type Description
images string[]

The array of image urls the AnimatedSprite will use as its texture frames.

Returns:
Type Description
AnimatedSprite The new animate sprite with the specified images as frames.

destroy () void

Stops the AnimatedSprite and destroys it.

gotoAndPlay (frameNumber) void

Goes to a specific frame and begins playing the AnimatedSprite.

Name Type Description
frameNumber number

Frame index to start at.

gotoAndStop (frameNumber) void

Stops the AnimatedSprite and goes to a specific frame.

Name Type Description
frameNumber number

Frame index to stop at.

play () void

Plays the AnimatedSprite.

stop () void

Stops the AnimatedSprite.

update (ticker) void

Updates the object transform for rendering.

Name Type Description
ticker Ticker

{Ticker} - current delta time

ticker.deltaTime

the delta time since the last tick