Class: Animation

Phaser.GameObjects.Components. Animation

A Game Object Animation Controller.

This controller lives as an instance within a Game Object, accessible as sprite.anims.


new Animation(parent)

Parameters:
Name Type Description
parent Phaser.GameObjects.GameObject

The Game Object to which this animation controller belongs.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 66)

Members


accumulator :number

Internal time overflow accumulator.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/components/Animation.js (Line 254)

animationManager :Phaser.Animations.AnimationManager

A reference to the global Animation Manager.

Type:
Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 94)

<nullable> currentAnim :Phaser.Animations.Animation

The current Animation loaded into this Animation Controller.

Type:
Since: 3.0.0
Default Value:
  • null
Source: src/gameobjects/components/Animation.js (Line 115)

<nullable> currentFrame :Phaser.Animations.AnimationFrame

The current AnimationFrame being displayed by this Animation Controller.

Type:
Since: 3.0.0
Default Value:
  • null
Source: src/gameobjects/components/Animation.js (Line 125)

duration :number

How long the animation should play for, in milliseconds.
If the frameRate property has been set then it overrides this value,
otherwise the frameRate is derived from duration.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/components/Animation.js (Line 157)

forward :boolean

An Internal trigger that's play the animation in reverse mode ('true') or not ('false'),
needed because forward can be changed by yoyo feature.

Type:
  • boolean
Since: 3.12.0
Default Value:
  • false
Source: src/gameobjects/components/Animation.js (Line 243)

forward :boolean

Will the playhead move forwards (true) or in reverse (false).

Type:
  • boolean
Since: 3.0.0
Default Value:
  • true
Source: src/gameobjects/components/Animation.js (Line 233)

frameRate :number

The frame rate of playback in frames per second.
The default is 24 if the duration property is null.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/components/Animation.js (Line 146)

<readonly> isPaused :boolean

true if the current animation is paused, otherwise false.

Type:
  • boolean
Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 493)

isPlaying :boolean

Is an animation currently playing or not?

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/gameobjects/components/Animation.js (Line 105)

msPerFrame :number

ms per frame, not including frame specific modifiers that may be present in the Animation data.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/components/Animation.js (Line 169)

nextTick :number

The time point at which the next animation frame will change.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/components/Animation.js (Line 264)

parent :Phaser.GameObjects.GameObject

The Game Object to which this animation controller belongs.

Type:
Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 85)

pendingRepeat :boolean

An internal flag keeping track of pending repeats.

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/gameobjects/components/Animation.js (Line 284)

repeatCounter :number

An internal counter keeping track of how many repeats are left to play.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/components/Animation.js (Line 274)

skipMissedFrames :boolean

Skip frames if the time lags, or always advanced anyway?

Type:
  • boolean
Since: 3.0.0
Default Value:
  • true
Source: src/gameobjects/components/Animation.js (Line 179)

Methods


_startAnimation(key [, startFrame])

Load an Animation and fires 'onStartEvent' event,
extracted from 'play' method

Parameters:
Name Type Argument Default Description
key string

The string-based key of the animation to play, as defined previously in the Animation Manager.

startFrame integer <optional>
0

Optionally start the animation playing from this frame index.

Since: 3.12.0
Source: src/gameobjects/components/Animation.js (Line 568)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

delayedPlay(delay, key [, startFrame])

Waits for the specified delay, in milliseconds, then starts playback of the requested animation.

Parameters:
Name Type Argument Default Description
delay integer

The delay, in milliseconds, to wait before starting the animation playing.

key string

The key of the animation to play.

startFrame integer <optional>
0

The frame of the animation to start from.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 374)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

destroy()

Destroy this Animation component.

Unregisters event listeners and cleans up its references.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 1056)

getCurrentKey()

Returns the key of the animation currently loaded into this component.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 395)
Returns:

The key of the Animation loaded into this component.

Type
string

getDelay()

Gets the amount of time, in milliseconds that the animation will be delayed before starting playback.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 361)
Returns:

The amount of time, in milliseconds, the Animation will wait before starting playback.

Type
integer

getProgress()

Returns a value between 0 and 1 indicating how far this animation is through, ignoring repeats and yoyos.
If the animation has a non-zero repeat defined, getProgress and getTotalProgress will be different
because getProgress doesn't include any repeats or repeat delays, whereas getTotalProgress does.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 625)
Returns:

The progress of the current animation, between 0 and 1.

Type
number

getRepeat()

Gets the number of times that the animation will repeat
after its first iteration. For example, if returns 1, the animation will
play a total of twice (the initial play plus 1 repeat).
A value of -1 means the animation will repeat indefinitely.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 691)
Returns:

The number of times that the animation will repeat.

Type
integer

getRepeatDelay()

Gets the amount of delay between repeats, if any.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 729)
Returns:

The delay between repeats.

Type
number

getTimeScale()

Gets the Time Scale factor.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 891)
Returns:

The Time Scale value.

Type
number

getTotalFrames()

Returns the total number of frames in this animation.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 904)
Returns:

The total number of frames in this animation.

Type
integer

getYoyo()

Gets if the current Animation will yoyo when it reaches the end.
A yoyo'ing animation will play through consecutively, and then reverse-play back to the start again.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 1042)
Returns:

true if the animation is set to yoyo, false if not.

Type
boolean

<protected> load(key [, startFrame])

Internal method used to load an animation into this component.

Parameters:
Name Type Argument Default Description
key string

The key of the animation to load.

startFrame integer <optional>
0

The start frame of the animation to load.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 411)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

pause( [atFrame])

Pause the current animation and set the isPlaying property to false.
You can optionally pause it at a specific frame.

Parameters:
Name Type Argument Description
atFrame Phaser.Animations.AnimationFrame <optional>

An optional frame to set after pausing the animation.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 438)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

play(key [, ignoreIfPlaying] [, startFrame])

Plays an Animation on the Game Object that owns this Animation Component.

Parameters:
Name Type Argument Default Description
key string

The string-based key of the animation to play, as defined previously in the Animation Manager.

ignoreIfPlaying boolean <optional>
false

If an animation is already playing then ignore this call.

startFrame integer <optional>
0

Optionally start the animation playing from this frame index.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 510)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

playReverse(key [, ignoreIfPlaying] [, startFrame])

Plays an Animation (in reverse mode) on the Game Object that owns this Animation Component.

Parameters:
Name Type Argument Default Description
key string

The string-based key of the animation to play, as defined previously in the Animation Manager.

ignoreIfPlaying boolean <optional>
false

If an animation is already playing then ignore this call.

startFrame integer <optional>
0

Optionally start the animation playing from this frame index.

Since: 3.12.0
Source: src/gameobjects/components/Animation.js (Line 539)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

remove( [key] [, animation])

Handle the removal of an animation from the Animation Manager.

Parameters:
Name Type Argument Description
key string <optional>

The key of the removed Animation.

animation Phaser.Animations.Animation <optional>

The removed Animation.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 670)

restart( [includeDelay])

Restarts the current animation from its beginning, optionally including its delay value.

Parameters:
Name Type Argument Default Description
includeDelay boolean <optional>
false

Whether to include the delay value of the animation when restarting.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 762)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

resume( [fromFrame])

Resumes playback of a paused animation and sets the isPlaying property to true.
You can optionally tell it to start playback from a specific frame.

Parameters:
Name Type Argument Description
fromFrame Phaser.Animations.AnimationFrame <optional>

An optional frame to set before restarting playback.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 466)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

reverse(key)

Reverse an Animation that is already playing on the Game Object.

Parameters:
Name Type Description
key string

The string-based key of the animation to play, as defined previously in the Animation Manager.

Since: 3.12.0
Source: src/gameobjects/components/Animation.js (Line 606)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

setCurrentFrame(animationFrame)

Sets the given Animation Frame as being the current frame
and applies it to the parent Game Object, adjusting its size and origin as needed.

Parameters:
Name Type Description
animationFrame Phaser.Animations.AnimationFrame

The Animation Frame to set as being current.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 951)
Returns:

The Game Object this Animation Component belongs to.

Type
Phaser.GameObjects.GameObject

setDelay( [value])

Sets the amount of time, in milliseconds, that the animation will be delayed before starting playback.

Parameters:
Name Type Argument Default Description
value integer <optional>
0

The amount of time, in milliseconds, to wait before starting playback.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 342)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

setProgress( [value])

Takes a value between 0 and 1 and uses it to set how far this animation is through playback.
Does not factor in repeats or yoyos, but does handle playing forwards or backwards.

Parameters:
Name Type Argument Default Description
value number <optional>
0

The progress value, between 0 and 1.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 647)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

setRepeat(value)

Sets the number of times that the animation should repeat
after its first iteration. For example, if repeat is 1, the animation will
play a total of twice (the initial play plus 1 repeat).
To repeat indefinitely, use -1. repeat should always be an integer.

Parameters:
Name Type Description
value integer

The number of times that the animation should repeat.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 707)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

setRepeatDelay(value)

Sets the amount of time in seconds between repeats.
For example, if repeat is 2 and repeatDelay is 10, the animation will play initially,
then wait for 10 seconds before repeating, then play again, then wait another 10 seconds
before doing its final repeat.

Parameters:
Name Type Description
value number

The delay to wait between repeats, in seconds.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 742)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

setTimeScale( [value])

Sets the Time Scale factor, allowing you to make the animation go go faster or slower than default.
Where 1 = normal speed (the default), 0.5 = half speed, 2 = double speed, etc.

Parameters:
Name Type Argument Default Description
value number <optional>
1

The time scale factor, where 1 is no change, 0.5 is half speed, etc.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 871)
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

setYoyo( [value])

Sets if the current Animation will yoyo when it reaches the end.
A yoyo'ing animation will play through consecutively, and then reverse-play back to the start again.

Parameters:
Name Type Argument Default Description
value boolean <optional>
false

true if the animation should yoyo, false to not.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 1022)
Returns:

The Game Object this Animation Component belongs to.

Type
Phaser.GameObjects.GameObject

stop()

Immediately stops the current animation from playing and dispatches the animationcomplete event.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 794)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

stopAfterDelay(delay)

Stops the current animation from playing after the specified time delay, given in milliseconds.

Parameters:
Name Type Description
delay integer

The number of milliseconds to wait before stopping this animation.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 816)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

stopOnFrame(delay)

Stops the current animation from playing when it next sets the given frame.
If this frame doesn't exist within the animation it will not stop it from playing.

Parameters:
Name Type Description
delay Phaser.Animations.AnimationFrame

The frame to check before stopping this animation.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 851)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

stopOnRepeat()

Stops the current animation from playing when it next repeats.

Since: 3.4.0
Source: src/gameobjects/components/Animation.js (Line 835)
Fires:
Returns:

The Game Object that owns this Animation Component.

Type
Phaser.GameObjects.GameObject

update(time, delta)

The internal update loop for the Animation Component.

Parameters:
Name Type Description
time number

The current timestamp.

delta number

The delta time, in ms, elapsed since the last frame.

Since: 3.0.0
Source: src/gameobjects/components/Animation.js (Line 917)

Events


onCompleteEvent

This event is dispatched when an animation completes playing, either naturally or via Animation.stop.

Listen for it on the Game Object: sprite.on('animationcomplete', listener)

Parameters:
Name Type Description
animation Phaser.Animations.Animation

Reference to the currently playing animation.

frame Phaser.Animations.AnimationFrame

Reference to the current Animation Frame.

gameObject Phaser.GameObjects.Sprite

Reference to the Game Object on which the event occurred.

Source: src/gameobjects/components/Animation.js (Line 55)

onRepeatEvent

This event is dispatched when an animation repeats.

Listen for it on the Game Object: sprite.on('animationrepeat', listener)

Parameters:
Name Type Description
animation Phaser.Animations.Animation

Reference to the currently playing animation.

frame Phaser.Animations.AnimationFrame

Reference to the current Animation Frame.

repeatCount integer

The number of times this animation has repeated.

gameObject Phaser.GameObjects.Sprite

Reference to the Game Object on which the event occurred.

Source: src/gameobjects/components/Animation.js (Line 31)

onRestartEvent

This event is dispatched when an animation restarts.

Listen for it on the Game Object: sprite.on('animationrestart', listener)

Parameters:
Name Type Description
animation Phaser.Animations.Animation

Reference to the currently playing animation.

frame Phaser.Animations.AnimationFrame

Reference to the current Animation Frame.

gameObject Phaser.GameObjects.Sprite

Reference to the Game Object on which the event occurred.

Source: src/gameobjects/components/Animation.js (Line 20)

onStartEvent

This event is dispatched when an animation starts playing.

Listen for it on the Game Object: sprite.on('animationstart', listener)

Parameters:
Name Type Description
animation Phaser.Animations.Animation

Reference to the currently playing animation.

frame Phaser.Animations.AnimationFrame

Reference to the current Animation Frame.

gameObject Phaser.GameObjects.Sprite

Reference to the Game Object on which the event occurred.

Source: src/gameobjects/components/Animation.js (Line 9)

onUpdateEvent

This event is dispatched when an animation updates. This happens when the animation frame changes,
based on the animation frame rate and other factors like timeScale and delay.

Listen for it on the Game Object: sprite.on('animationupdate', listener)

Parameters:
Name Type Description
animation Phaser.Animations.Animation

Reference to the currently playing animation.

frame Phaser.Animations.AnimationFrame

Reference to the current Animation Frame.

gameObject Phaser.GameObjects.Sprite

Reference to the Game Object on which the event occurred.

Source: src/gameobjects/components/Animation.js (Line 43)