Skip to content

Commit e6f5d01

Browse files
committed
Added Animation.chain method to queue an animation to start when the current one ends.
1 parent 96fab45 commit e6f5d01

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ one set of bindings ever created, which makes things a lot cleaner.
145145
* The Animation class now emits the `start` event when played (either forward, or in reverse) by any Game Object.
146146
* The Animation class now emits the `restart` event when it restarts playing on any Game Object.
147147
* The Animation class now emits the `complete` event when it finishes playing on any Game Object.
148+
* The Animation Component has a new method called `chain` which allows you to line-up another animation to start playing as soon as the current one stops, no matter how it stops (either by reaching its natural end, or directly by having stop called on it). You can chain a new animation at any point, including before the current one starts playing, during it, or when it ends (via its `animationcomplete` callback). Chained animations are specific to a Game Object, meaning different Game Objects can have different chained animations without impacting the global animation they're playing.
148149

149150
### Updates
150151

src/gameobjects/components/Animation.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ var Animation = new Class({
156156
*/
157157
this.currentFrame = null;
158158

159+
/**
160+
* The key of the next Animation to be loaded into this Animation Controller when the current animation completes.
161+
*
162+
* @name Phaser.GameObjects.Components.Animation#nextAnim
163+
* @type {?string}
164+
* @default null
165+
* @since 3.16.0
166+
*/
167+
this.nextAnim = null;
168+
159169
/**
160170
* Time scale factor.
161171
*
@@ -363,6 +373,34 @@ var Animation = new Class({
363373
this._pendingStopValue;
364374
},
365375

376+
/**
377+
* Sets an animation to be played immediately after the current one completes.
378+
*
379+
* The current animation must enter a 'completed' state for this to happen, i.e. finish all of its repeats, delays, etc.
380+
*
381+
* An animation set to repeat forever will never enter a completed state.
382+
*
383+
* Call this method with no arguments to reset the chained animation.
384+
*
385+
* @method Phaser.GameObjects.Components.Animation#chain
386+
* @since 3.16.0
387+
*
388+
* @param {(string|Phaser.Animations.Animation)} [key] - The string-based key of the animation to play next, as defined previously in the Animation Manager. Or an Animation instance.
389+
*
390+
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
391+
*/
392+
chain: function (key)
393+
{
394+
if (key instanceof BaseAnimation)
395+
{
396+
key = key.key;
397+
}
398+
399+
this.nextAnim = key;
400+
401+
return this.parent;
402+
},
403+
366404
/**
367405
* Sets the amount of time, in milliseconds, that the animation will be delayed before starting playback.
368406
*
@@ -842,6 +880,8 @@ var Animation = new Class({
842880

843881
/**
844882
* Immediately stops the current animation from playing and dispatches the `animationcomplete` event.
883+
*
884+
* If there is another animation queued (via the `chain` method) then it will start playing immediately.
845885
*
846886
* @method Phaser.GameObjects.Components.Animation#stop
847887
* @fires Phaser.GameObjects.Components.Animation#onCompleteEvent
@@ -865,6 +905,15 @@ var Animation = new Class({
865905

866906
gameObject.emit('animationcomplete', anim, frame, gameObject);
867907

908+
if (this.nextAnim)
909+
{
910+
var key = this.nextAnim;
911+
912+
this.nextAnim = null;
913+
914+
this.play(key);
915+
}
916+
868917
return gameObject;
869918
},
870919

0 commit comments

Comments
 (0)