Skip to content

Commit 777325c

Browse files
committed
Removed callbacks, added events and added new stop methods
1 parent c2edab2 commit 777325c

1 file changed

Lines changed: 91 additions & 40 deletions

File tree

src/gameobjects/components/Animation.js

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,29 @@ var Animation = new Class({
246246
this._wasPlaying = false;
247247

248248
/**
249-
* Container for the callback arguments.
249+
* Internal property tracking if this Animation is waiting to stop.
250+
*
251+
* 0 = No
252+
* 1 = Waiting for ms to pass
253+
* 2 = Waiting for repeat
254+
* 3 = Waiting for specific frame
250255
*
251-
* @name Phaser.GameObjects.Components.Animation#_callbackArgs
252-
* @type {array}
256+
* @name Phaser.GameObjects.Components.Animation#_pendingStop
257+
* @type {integer}
253258
* @private
254-
* @since 3.0.0
259+
* @since 3.4.0
255260
*/
256-
this._callbackArgs = [ parent, null ];
261+
this._pendingStop = 0;
257262

258263
/**
259-
* Container for the update arguments.
264+
* Internal property used by _pendingStop.
260265
*
261-
* @name Phaser.GameObjects.Components.Animation#_updateParams
262-
* @type {array}
266+
* @name Phaser.GameObjects.Components.Animation#_pendingStopValue
267+
* @type {any}
263268
* @private
264-
* @since 3.0.0
269+
* @since 3.4.0
265270
*/
266-
this._updateParams = [];
271+
this._pendingStopValue;
267272
},
268273

269274
/**
@@ -475,10 +480,7 @@ var Animation = new Class({
475480
gameObject.visible = true;
476481
}
477482

478-
if (anim.onStart)
479-
{
480-
anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams));
481-
}
483+
gameObject.emit('animationstart', this.currentAnim, this.currentFrame);
482484

483485
return gameObject;
484486
},
@@ -647,27 +649,74 @@ var Animation = new Class({
647649
},
648650

649651
/**
650-
* Stops the current animation from playing and optionally dispatches any onComplete callbacks.
652+
* Immediately stops the current animation from playing and dispatches the `animationcomplete` event.
651653
*
652654
* @method Phaser.GameObjects.Components.Animation#stop
653655
* @since 3.0.0
654656
*
655-
* @param {boolean} [dispatchCallbacks=false] - [description]
656-
*
657657
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
658658
*/
659-
stop: function (dispatchCallbacks)
659+
stop: function ()
660660
{
661-
if (dispatchCallbacks === undefined) { dispatchCallbacks = false; }
661+
this._pendingStop = 0;
662662

663663
this.isPlaying = false;
664664

665-
var anim = this.currentAnim;
665+
var gameObject = this.parent;
666666

667-
if (dispatchCallbacks && anim.onComplete)
668-
{
669-
anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams));
670-
}
667+
gameObject.emit('animtioncomplete', this.currentAnim, this.currentFrame);
668+
669+
return gameObject;
670+
},
671+
672+
/**
673+
* Stops the current animation from playing after the specified time delay, given in milliseconds.
674+
*
675+
* @method Phaser.GameObjects.Components.Animation#stopAfterDelay
676+
* @since 3.4.0
677+
*
678+
* @param {integer} delay - The number of miliseconds to wait before stopping this animation.
679+
*
680+
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
681+
*/
682+
stopAfterDelay: function (delay)
683+
{
684+
this._pendingStop = 1;
685+
this._pendingStopValue = delay;
686+
687+
return this.parent;
688+
},
689+
690+
/**
691+
* Stops the current animation from playing when it next repeats.
692+
*
693+
* @method Phaser.GameObjects.Components.Animation#stopOnRepeat
694+
* @since 3.4.0
695+
*
696+
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
697+
*/
698+
stopOnRepeat: function ()
699+
{
700+
this._pendingStop = 2;
701+
702+
return this.parent;
703+
},
704+
705+
/**
706+
* Stops the current animation from playing when it next sets the given frame.
707+
* If this frame doesn't exist within the animation it will not stop it from playing.
708+
*
709+
* @method Phaser.GameObjects.Components.Animation#stopOnFrame
710+
* @since 3.4.0
711+
*
712+
* @param {Phaser.Animations.AnimationFrame} delay - The frame to check before stopping this animation.
713+
*
714+
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
715+
*/
716+
stopOnFrame: function (frame)
717+
{
718+
this._pendingStop = 3;
719+
this._pendingStopValue = frame;
671720

672721
return this.parent;
673722
},
@@ -729,16 +778,24 @@ var Animation = new Class({
729778
*/
730779
update: function (timestamp, delta)
731780
{
732-
if (!this.isPlaying || this.currentAnim.paused)
781+
if (this.isPlaying || !this.currentAnim.paused)
733782
{
734-
return;
735-
}
783+
this.accumulator += delta * this._timeScale;
736784

737-
this.accumulator += delta * this._timeScale;
785+
if (this._pendingStop === 1)
786+
{
787+
this._pendingStopValue -= delta;
738788

739-
if (this.accumulator >= this.nextTick)
740-
{
741-
this.currentAnim.setFrame(this);
789+
if (this._pendingStopValue <= 0)
790+
{
791+
return this.currentAnim.completeAnimation(this);
792+
}
793+
}
794+
795+
if (this.accumulator >= this.nextTick)
796+
{
797+
this.currentAnim.setFrame(this);
798+
}
742799
}
743800
},
744801

@@ -798,14 +855,11 @@ var Animation = new Class({
798855

799856
var anim = this.currentAnim;
800857

801-
if (anim.onUpdate)
802-
{
803-
anim.onUpdate.apply(anim.callbackScope, this._updateParams);
804-
}
858+
gameObject.emit('animationupdate', anim, animationFrame);
805859

806-
if (animationFrame.onUpdate)
860+
if (this._pendingStop === 3 && this._pendingStopValue === animationFrame)
807861
{
808-
animationFrame.onUpdate(gameObject, animationFrame);
862+
this.currentAnim.completeAnimation(this);
809863
}
810864
}
811865
},
@@ -859,9 +913,6 @@ var Animation = new Class({
859913

860914
this.currentAnim = null;
861915
this.currentFrame = null;
862-
863-
this._callbackArgs = [];
864-
this._updateParams = [];
865916
}
866917

867918
});

0 commit comments

Comments
 (0)