Class: AnimationManager

Phaser.Animations. AnimationManager

The Animation Manager.

Animations are managed by the global Animation Manager. This is a singleton class that is responsible for creating and delivering animations and their corresponding data to all Game Objects. Unlike plugins it is owned by the Game instance, not the Scene.

Sprites and other Game Objects get the data they need from the AnimationManager.


new AnimationManager(game)

Parameters:
Name Type Description
game Phaser.Game

A reference to the Phaser.Game instance.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 18)

Extends

Members


<protected> anims :Phaser.Structs.Map.<string, Phaser.Animations.Animation>

The Animations registered in the Animation Manager.

This map should be modified with the #add and #create methods of the Animation Manager.

Type:
Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 78)

<protected> game :Phaser.Game

A reference to the Phaser.Game instance.

Type:
Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 46)

globalTimeScale :number

The global time scale of the Animation Manager.

This scales the time delta between two frames, thus influencing the speed of time for the Animation Manager.

Type:
  • number
Since: 3.0.0
Default Value:
  • 1
Source: src/animations/AnimationManager.js (Line 66)

mixes :Phaser.Structs.Map.<string, Phaser.Animations.Animation>

A list of animation mix times.

See the #setMix method for more details.

Type:
Since: 3.50.0
Source: src/animations/AnimationManager.js (Line 90)

name :string

The name of this Animation Manager.

Type:
  • string
Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 111)

paused :boolean

Whether the Animation Manager is paused along with all of its Animations.

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/animations/AnimationManager.js (Line 101)

<protected> textureManager :Phaser.Textures.TextureManager

A reference to the Texture Manager.

Type:
Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 56)

Methods


add(key, animation)

Adds an existing Animation to the Animation Manager.

Parameters:
Name Type Description
key string

The key under which the Animation should be added. The Animation will be updated with it. Must be unique.

animation Phaser.Animations.Animation

The Animation which should be added to the Animation Manager.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 275)
Fires:
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

addListener(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 111)
Returns:

this.

Type
Phaser.Animations.AnimationManager

addMix(animA, animB, delay)

Adds a mix between two animations.

Mixing allows you to specify a unique delay between a pairing of animations.

When playing Animation A on a Game Object, if you then play Animation B, and a mix exists, it will wait for the specified delay to be over before playing Animation B.

This allows you to customise smoothing between different types of animation, such as blending between an idle and a walk state, or a running and a firing state.

Note that mixing is only applied if you use the Sprite.play method. If you opt to use playAfterRepeat or playAfterDelay instead, those will take pririty and the mix delay will not be used.

To update an existing mix, just call this method with the new delay.

To remove a mix pairing, see the removeMix method.

Parameters:
Name Type Description
animA string | Phaser.Animations.Animation

The string-based key, or instance of, Animation A.

animB string | Phaser.Animations.Animation

The string-based key, or instance of, Animation B.

delay number

The delay, in milliseconds, to wait when transitioning from Animation A to B.

Since: 3.50.0
Source: src/animations/AnimationManager.js (Line 137)
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

boot()

Registers event listeners after the Game boots.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 123)
Listens to Events:

create(config)

Creates a new Animation and adds it to the Animation Manager.

Animations are global. Once created, you can use them in any Scene in your game. They are not Scene specific.

If an invalid key is given this method will return false.

If you pass the key of an animation that already exists in the Animation Manager, that animation will be returned.

A brand new animation is only created if the key is valid and not already in use.

If you wish to re-use an existing key, call AnimationManager.remove first, then this method.

Parameters:
Name Type Description
config Phaser.Types.Animations.Animation

The configuration settings for the Animation.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 494)
Fires:
Returns:

The Animation that was created, or false if the key is already in use.

Type
Phaser.Animations.Animation | false

createFromAseprite(key [, tags])

Create one, or more animations from a loaded Aseprite JSON file.

Aseprite is a powerful animated sprite editor and pixel art tool.

You can find more details at https://www.aseprite.org/

To export a compatible JSON file in Aseprite, please do the following:

  1. Go to "File - Export Sprite Sheet"

  2. On the Layout tab: 2a. Set the "Sheet type" to "Packed" 2b. Set the "Constraints" to "None" 2c. Check the "Merge Duplicates" checkbox

  3. On the Sprite tab: 3a. Set "Layers" to "Visible layers" 3b. Set "Frames" to "All frames", unless you only wish to export a sub-set of tags

  4. On the Borders tab: 4a. Check the "Trim Sprite" and "Trim Cells" options 4b. Ensure "Border Padding", "Spacing" and "Inner Padding" are all > 0 (1 is usually enough)

  5. On the Output tab: 5a. Check "Output File", give your image a name and make sure you choose "png files" as the file type 5b. Check "JSON Data" and give your json file a name 5c. The JSON Data type can be either a Hash or Array, Phaser doesn't mind. 5d. Make sure "Tags" is checked in the Meta options 5e. In the "Item Filename" input box, make sure it says just "{frame}" and nothing more.

  6. Click export

This was tested with Aseprite 1.2.25.

This will export a png and json file which you can load using the Aseprite Loader, i.e.:

function preload ()
{
    this.load.path = 'assets/animations/aseprite/';
    this.load.aseprite('paladin', 'paladin.png', 'paladin.json');
}

Once loaded, you can call this method from within a Scene with the 'atlas' key:

this.anims.createFromAseprite('paladin');

Any animations defined in the JSON will now be available to use in Phaser and you play them via their Tag name. For example, if you have an animation called 'War Cry' on your Aseprite timeline, you can play it in Phaser using that Tag name:

this.add.sprite(400, 300).play('War Cry');

When calling this method you can optionally provide an array of tag names, and only those animations will be created. For example:

this.anims.createFromAseprite('paladin', [ 'step', 'War Cry', 'Magnum Break' ]);

This will only create the 3 animations defined. Note that the tag names are case-sensitive.

Parameters:
Name Type Argument Description
key string

The key of the loaded Aseprite atlas. It must have been loaded prior to calling this method.

tags Array.<string> <optional>

An array of Tag names. If provided, only animations found in this array will be created.

Since: 3.50.0
Source: src/animations/AnimationManager.js (Line 322)
Returns:

An array of Animation instances that were successfully created.

Type
Array.<Phaser.Animations.Animation>

destroy()

Destroy this Animation Manager and clean up animation definitions and references to other objects. This method should not be called directly. It will be called automatically as a response to a destroy event from the Phaser.Game instance.

Since: 3.0.0
Overrides:
Source: src/animations/AnimationManager.js (Line 984)

emit(event [, args])

Calls each of the listeners registered for a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

args * <optional>
<repeatable>

Additional arguments that will be passed to the event handler.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 86)
Returns:

true if the event had listeners, else false.

Type
boolean

eventNames()

Return an array listing the events for which the emitter has registered listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 55)
Returns:
Type
Array.<(string|symbol)>

exists(key)

Checks to see if the given key is already in use within the Animation Manager or not.

Animations are global. Keys created in one scene can be used from any other Scene in your game. They are not Scene specific.

Parameters:
Name Type Description
key string

The key of the Animation to check.

Since: 3.16.0
Source: src/animations/AnimationManager.js (Line 305)
Returns:

true if the Animation already exists in the Animation Manager, or false if the key is available.

Type
boolean

fromJSON(data [, clearCurrentAnimations])

Loads this Animation Manager's Animations and settings from a JSON object.

Parameters:
Name Type Argument Default Description
data string | Phaser.Types.Animations.JSONAnimations | Phaser.Types.Animations.JSONAnimation

The JSON object to parse.

clearCurrentAnimations boolean <optional>
false

If set to true, the current animations will be removed (anims.clear()). If set to false (default), the animations in data will be added.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 538)
Returns:

An array containing all of the Animation objects that were created as a result of this call.

Type
Array.<Phaser.Animations.Animation>

generateFrameNames(key [, config])

Generate an array of Phaser.Types.Animations.AnimationFrame objects from a texture key and configuration object.

Generates objects with string based frame names, as configured by the given Phaser.Types.Animations.GenerateFrameNames.

It's a helper method, designed to make it easier for you to extract all of the frame names from texture atlases. If you're working with a sprite sheet, see the generateFrameNumbers method instead.

Example:

If you have a texture atlases loaded called gems and it contains 6 frames called ruby_0001, ruby_0002, and so on, then you can call this method using: this.anims.generateFrameNames('gems', { prefix: 'ruby_', end: 6, zeroPad: 4 }).

The end value tells it to look for 6 frames, incrementally numbered, all starting with the prefix ruby_. The zeroPad value tells it how many zeroes pad out the numbers. To create an animation using this method, you can do:

this.anims.create({
  key: 'ruby',
  repeat: -1,
  frames: this.anims.generateFrameNames('gems', {
    prefix: 'ruby_',
    end: 6,
    zeroPad: 4
  })
});

Please see the animation examples for further details.

Parameters:
Name Type Argument Description
key string

The key for the texture containing the animation frames.

config Phaser.Types.Animations.GenerateFrameNames <optional>

The configuration object for the animation frame names.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 587)
Returns:
Type
Array.<Phaser.Types.Animations.AnimationFrame>

generateFrameNumbers(key, config)

Generate an array of Phaser.Types.Animations.AnimationFrame objects from a texture key and configuration object.

Generates objects with numbered frame names, as configured by the given Phaser.Types.Animations.GenerateFrameNumbers.

If you're working with a texture atlas, see the generateFrameNames method instead.

It's a helper method, designed to make it easier for you to extract frames from sprite sheets. If you're working with a texture atlas, see the generateFrameNames method instead.

Example:

If you have a sprite sheet loaded called explosion and it contains 12 frames, then you can call this method using: this.anims.generateFrameNumbers('explosion', { start: 0, end: 12 }).

The end value tells it to stop after 12 frames. To create an animation using this method, you can do:

this.anims.create({
  key: 'boom',
  frames: this.anims.generateFrameNames('explosion', {
    start: 0,
    end: 12
  })
});

Note that start is optional and you don't need to include it if the animation starts from frame 0.

To specify an animation in reverse, swap the start and end values.

If the frames are not sequential, you may pass an array of frame numbers instead, for example:

this.anims.generateFrameNumbers('explosion', { frames: [ 0, 1, 2, 1, 2, 3, 4, 0, 1, 2 ] })

Please see the animation examples and GenerateFrameNumbers config docs for further details.

Parameters:
Name Type Description
key string

The key for the texture containing the animation frames.

config Phaser.Types.Animations.GenerateFrameNumbers

The configuration object for the animation frames.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 679)
Returns:
Type
Array.<Phaser.Types.Animations.AnimationFrame>

get(key)

Get an Animation.

Parameters:
Name Type Description
key string

The key of the Animation to retrieve.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 772)
Returns:

The Animation.

Type
Phaser.Animations.Animation

getMix(animA, animB)

Returns the mix delay between two animations.

If no mix has been set-up, this method will return zero.

If you wish to create, or update, a new mix, call the addMix method. If you wish to remove a mix, call the removeMix method.

Parameters:
Name Type Description
animA string | Phaser.Animations.Animation

The string-based key, or instance of, Animation A.

animB string | Phaser.Animations.Animation

The string-based key, or instance of, Animation B.

Since: 3.50.0
Source: src/animations/AnimationManager.js (Line 240)
Returns:

The mix duration, or zero if no mix exists.

Type
number

listenerCount(event)

Return the number of listeners listening to a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 75)
Returns:

The number of listeners.

Type
number

listeners(event)

Return the listeners registered for a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 64)
Returns:

The registered listeners.

Type
Array.<function()>

off(event [, fn] [, context] [, once])

Remove the listeners of a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 151)
Returns:

this.

Type
Phaser.Animations.AnimationManager

on(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 98)
Returns:

this.

Type
Phaser.Animations.AnimationManager

once(event, fn [, context])

Add a one-time listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 124)
Returns:

this.

Type
Phaser.Animations.AnimationManager

pauseAll()

Pause all animations.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 787)
Fires:
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

play(key, children)

Play an animation on the given Game Objects that have an Animation Component.

Parameters:
Name Type Description
key string | Phaser.Animations.Animation | Phaser.Types.Animations.PlayAnimationConfig

The string-based key of the animation to play, or an Animation instance, or a PlayAnimationConfig object.

children Phaser.GameObjects.GameObject | Array.<Phaser.GameObjects.GameObject>

An array of Game Objects to play the animation on. They must have an Animation Component.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 808)
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

remove(key)

Removes an Animation from this Animation Manager, based on the given key.

This is a global action. Once an Animation has been removed, no Game Objects can carry on using it.

Parameters:
Name Type Description
key string

The key of the animation to remove.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 900)
Fires:
Returns:

The Animation instance that was removed from the Animation Manager.

Type
Phaser.Animations.Animation

removeAllListeners( [event])

Remove all listeners, or those of the specified event.

Parameters:
Name Type Argument Description
event string | symbol <optional>

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 165)
Returns:

this.

Type
Phaser.Animations.AnimationManager

removeListener(event [, fn] [, context] [, once])

Remove the listeners of a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 137)
Returns:

this.

Type
Phaser.Animations.AnimationManager

removeMix(animA [, animB])

Removes a mix between two animations.

Mixing allows you to specify a unique delay between a pairing of animations.

Calling this method lets you remove those pairings. You can either remove it between animA and animB, or if you do not provide the animB parameter, it will remove all animA mixes.

If you wish to update an existing mix instead, call the addMix method with the new delay.

Parameters:
Name Type Argument Description
animA string | Phaser.Animations.Animation

The string-based key, or instance of, Animation A.

animB string | Phaser.Animations.Animation <optional>

The string-based key, or instance of, Animation B. If not given, all mixes for Animation A will be removed.

Since: 3.50.0
Source: src/animations/AnimationManager.js (Line 190)
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

resumeAll()

Resume all paused animations.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 930)
Fires:
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

shutdown()

Removes all listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 31)

staggerPlay(key, children, stagger [, staggerFirst])

Takes an array of Game Objects that have an Animation Component and then starts the given animation playing on them. The start time of each Game Object is offset, incrementally, by the stagger amount.

For example, if you pass an array with 4 children and a stagger time of 1000, the delays will be:

child 1: 1000ms delay child 2: 2000ms delay child 3: 3000ms delay child 4: 4000ms delay

If you set the staggerFirst parameter to false they would be:

child 1: 0ms delay child 2: 1000ms delay child 3: 2000ms delay child 4: 3000ms delay

You can also set stagger to be a negative value. If it was -1000, the above would be:

child 1: 3000ms delay child 2: 2000ms delay child 3: 1000ms delay child 4: 0ms delay

Parameters:
Name Type Argument Default Description
key string | Phaser.Animations.Animation | Phaser.Types.Animations.PlayAnimationConfig

The string-based key of the animation to play, or an Animation instance, or a PlayAnimationConfig object.

children Phaser.GameObjects.GameObject | Array.<Phaser.GameObjects.GameObject>

An array of Game Objects to play the animation on. They must have an Animation Component.

stagger number

The amount of time, in milliseconds, to offset each play time by. If a negative value is given, it's applied to the children in reverse order.

staggerFirst boolean <optional>
true

Should the first child be staggered as well?

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 834)
Returns:

This Animation Manager.

Type
Phaser.Animations.AnimationManager

toJSON( [key])

Returns the Animation data as JavaScript object based on the given key. Or, if not key is defined, it will return the data of all animations as array of objects.

Parameters:
Name Type Argument Description
key string <optional>

The animation to get the JSONAnimation data from. If not provided, all animations are returned as an array.

Since: 3.0.0
Source: src/animations/AnimationManager.js (Line 951)
Returns:

The resulting JSONAnimations formatted object.

Type
Phaser.Types.Animations.JSONAnimations