Skip to content

Commit 9e78553

Browse files
committed
reverse no longer needs the argument. play and playReverse can now take an Animation instance as an argument, not just a key.
1 parent 8955c50 commit 9e78553

1 file changed

Lines changed: 69 additions & 18 deletions

File tree

src/gameobjects/components/Animation.js

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var BaseAnimation = require('../../animations/Animation');
78
var Class = require('../../utils/Class');
89

910
/**
1011
* This event is dispatched when an animation starts playing.
1112
*
1213
* Listen for it on the Game Object: `sprite.on('animationstart', listener)`
14+
*
15+
* You can also listen for a specific animation by appending a hyphen and its key to the event name. For example,
16+
* if you have an animation called `explode`, you can listen for `sprite.on('animationstart-explode', listener)`.
1317
*
1418
* @event Phaser.GameObjects.Components.Animation#onStartEvent
1519
* @param {Phaser.Animations.Animation} animation - Reference to the currently playing animation.
@@ -21,6 +25,9 @@ var Class = require('../../utils/Class');
2125
* This event is dispatched when an animation restarts.
2226
*
2327
* Listen for it on the Game Object: `sprite.on('animationrestart', listener)`
28+
*
29+
* You can also listen for a specific animation by appending a hyphen and its key to the event name. For example,
30+
* if you have an animation called `explode`, you can listen for `sprite.on('animationrestart-explode', listener)`.
2431
*
2532
* @event Phaser.GameObjects.Components.Animation#onRestartEvent
2633
* @param {Phaser.Animations.Animation} animation - Reference to the currently playing animation.
@@ -32,6 +39,9 @@ var Class = require('../../utils/Class');
3239
* This event is dispatched when an animation repeats.
3340
*
3441
* Listen for it on the Game Object: `sprite.on('animationrepeat', listener)`
42+
*
43+
* You can also listen for a specific animation by appending a hyphen and its key to the event name. For example,
44+
* if you have an animation called `explode`, you can listen for `sprite.on('animationrepeat-explode', listener)`.
3545
*
3646
* @event Phaser.GameObjects.Components.Animation#onRepeatEvent
3747
* @param {Phaser.Animations.Animation} animation - Reference to the currently playing animation.
@@ -45,6 +55,9 @@ var Class = require('../../utils/Class');
4555
* based on the animation frame rate and other factors like timeScale and delay.
4656
*
4757
* Listen for it on the Game Object: `sprite.on('animationupdate', listener)`
58+
*
59+
* You can also listen for a specific animation by appending a hyphen and its key to the event name. For example,
60+
* if you have an animation called `explode`, you can listen for `sprite.on('animationupdate-explode', listener)`.
4861
*
4962
* @event Phaser.GameObjects.Components.Animation#onUpdateEvent
5063
* @param {Phaser.Animations.Animation} animation - Reference to the currently playing animation.
@@ -56,6 +69,9 @@ var Class = require('../../utils/Class');
5669
* This event is dispatched when an animation completes playing, either naturally or via Animation.stop.
5770
*
5871
* Listen for it on the Game Object: `sprite.on('animationcomplete', listener)`
72+
*
73+
* You can also listen for a specific animation by appending a hyphen and its key to the event name. For example,
74+
* if you have an animation called `explode`, you can listen for `sprite.on('animationcomplete-explode', listener)`.
5975
*
6076
* @event Phaser.GameObjects.Components.Animation#onCompleteEvent
6177
* @param {Phaser.Animations.Animation} animation - Reference to the currently playing animation.
@@ -508,13 +524,15 @@ var Animation = new Class({
508524
},
509525

510526
/**
511-
* Plays an Animation on the Game Object that owns this Animation Component.
527+
* Plays an Animation on a Game Object that has the Animation component, such as a Sprite.
528+
*
529+
* Animations are stored in the global Animation Manager and are referenced by a unique string-based key.
512530
*
513531
* @method Phaser.GameObjects.Components.Animation#play
514532
* @fires Phaser.GameObjects.Components.Animation#onStartEvent
515533
* @since 3.0.0
516534
*
517-
* @param {string} key - The string-based key of the animation to play, as defined previously in the Animation Manager.
535+
* @param {(string|Phaser.Animations.Animation)} key - The string-based key of the animation to play, as defined previously in the Animation Manager. Or an Animation instance.
518536
* @param {boolean} [ignoreIfPlaying=false] - If an animation is already playing then ignore this call.
519537
* @param {integer} [startFrame=0] - Optionally start the animation playing from this frame index.
520538
*
@@ -525,6 +543,11 @@ var Animation = new Class({
525543
if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; }
526544
if (startFrame === undefined) { startFrame = 0; }
527545

546+
if (key instanceof BaseAnimation)
547+
{
548+
key = key.key;
549+
}
550+
528551
if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key)
529552
{
530553
return this.parent;
@@ -543,7 +566,7 @@ var Animation = new Class({
543566
* @fires Phaser.GameObjects.Components.Animation#onStartEvent
544567
* @since 3.12.0
545568
*
546-
* @param {string} key - The string-based key of the animation to play, as defined previously in the Animation Manager.
569+
* @param {(string|Phaser.Animations.Animation)} key - The string-based key of the animation to play, as defined previously in the Animation Manager. Or an Animation instance.
547570
* @param {boolean} [ignoreIfPlaying=false] - If an animation is already playing then ignore this call.
548571
* @param {integer} [startFrame=0] - Optionally start the animation playing from this frame index.
549572
*
@@ -554,6 +577,11 @@ var Animation = new Class({
554577
if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; }
555578
if (startFrame === undefined) { startFrame = 0; }
556579

580+
if (key instanceof BaseAnimation)
581+
{
582+
key = key.key;
583+
}
584+
557585
if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key)
558586
{
559587
return this.parent;
@@ -566,8 +594,7 @@ var Animation = new Class({
566594
},
567595

568596
/**
569-
* Load an Animation and fires 'onStartEvent' event,
570-
* extracted from 'play' method
597+
* Load an Animation and fires 'onStartEvent' event, extracted from 'play' method.
571598
*
572599
* @method Phaser.GameObjects.Components.Animation#_startAnimation
573600
* @fires Phaser.GameObjects.Components.Animation#onStartEvent
@@ -598,26 +625,33 @@ var Animation = new Class({
598625
gameObject.visible = true;
599626
}
600627

601-
gameObject.emit('animationstart', this.currentAnim, this.currentFrame, gameObject);
628+
var frame = this.currentFrame;
629+
630+
anim.emit('start', anim, frame);
631+
632+
gameObject.emit('animationstart-' + key, anim, frame, gameObject);
633+
634+
gameObject.emit('animationstart', anim, frame, gameObject);
602635

603636
return gameObject;
604637
},
605638

606639
/**
607-
* Reverse an Animation that is already playing on the Game Object.
640+
* Reverse the Animation that is already playing on the Game Object.
608641
*
609642
* @method Phaser.GameObjects.Components.Animation#reverse
610643
* @since 3.12.0
611644
*
612-
* @param {string} key - The string-based key of the animation to play, as defined previously in the Animation Manager.
613-
*
614645
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
615646
*/
616-
reverse: function (key)
647+
reverse: function ()
617648
{
618-
if (!this.isPlaying || this.currentAnim.key !== key) { return this.parent; }
619-
this._reverse = !this._reverse;
620-
this.forward = !this.forward;
649+
if (this.isPlaying)
650+
{
651+
this._reverse = !this._reverse;
652+
653+
this.forward = !this.forward;
654+
}
621655

622656
return this.parent;
623657
},
@@ -774,19 +808,26 @@ var Animation = new Class({
774808
{
775809
if (includeDelay === undefined) { includeDelay = false; }
776810

777-
this.currentAnim.getFirstTick(this, includeDelay);
811+
var anim = this.currentAnim;
812+
813+
anim.getFirstTick(this, includeDelay);
778814

779815
this.forward = true;
780816
this.isPlaying = true;
781817
this.pendingRepeat = false;
782818
this._paused = false;
783819

784820
// Set frame
785-
this.updateFrame(this.currentAnim.frames[0]);
821+
this.updateFrame(anim.frames[0]);
786822

787823
var gameObject = this.parent;
824+
var frame = this.currentFrame;
825+
826+
anim.emit('restart', anim, frame);
788827

789-
gameObject.emit('animationrestart', this.currentAnim, this.currentFrame, gameObject);
828+
gameObject.emit('animationrestart-' + anim.key, anim, frame, gameObject);
829+
830+
gameObject.emit('animationrestart', anim, frame, gameObject);
790831

791832
return this.parent;
792833
},
@@ -807,8 +848,14 @@ var Animation = new Class({
807848
this.isPlaying = false;
808849

809850
var gameObject = this.parent;
851+
var anim = this.currentAnim;
852+
var frame = this.currentFrame;
810853

811-
gameObject.emit('animationcomplete', this.currentAnim, this.currentFrame, gameObject);
854+
anim.on('complete', anim, frame);
855+
856+
gameObject.emit('animationcomplete-' + anim.key, anim, frame, gameObject);
857+
858+
gameObject.emit('animationcomplete', anim, frame, gameObject);
812859

813860
return gameObject;
814861
},
@@ -856,7 +903,7 @@ var Animation = new Class({
856903
* @fires Phaser.GameObjects.Components.Animation#onCompleteEvent
857904
* @since 3.4.0
858905
*
859-
* @param {Phaser.Animations.AnimationFrame} delay - The frame to check before stopping this animation.
906+
* @param {Phaser.Animations.AnimationFrame} frame - The frame to check before stopping this animation.
860907
*
861908
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
862909
*/
@@ -1010,6 +1057,10 @@ var Animation = new Class({
10101057

10111058
var anim = this.currentAnim;
10121059

1060+
anim.emit('update', anim, animationFrame);
1061+
1062+
gameObject.emit('animationupdate-' + anim.key, anim, animationFrame, gameObject);
1063+
10131064
gameObject.emit('animationupdate', anim, animationFrame, gameObject);
10141065

10151066
if (this._pendingStop === 3 && this._pendingStopValue === animationFrame)

0 commit comments

Comments
 (0)