Skip to content

Commit 32119e9

Browse files
committed
Animation.onUpdate is a new event that is dispatched each time the animation frame changes. Due to its intensive nature it is disabled by default. Enable it with Animation.enableUpdate = true (phaserjs#902)
1 parent 9055fc7 commit 32119e9

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Version 2.1.0 - "Cairhien" - -in development-
9696
* SoundManager.destroy is a new method that will destroy all current sounds and reset any callbacks.
9797
* StateManager.clearCurrentState now handles the process of clearing down the current state and is now called if the Game is destroyed.
9898
* Game.destroy now clears the current state, activating its shutdown callback if it had one. It also now destroys the SoundManager, stopping any currently running sounds (#1092)
99+
* Animation.onUpdate is a new event that is dispatched each time the animation frame changes. Due to its intensive nature it is disabled by default. Enable it with `Animation.enableUpdate = true` (#902)
99100

100101
### Updates
101102

src/animation/Animation.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ Phaser.Animation = function (game, parent, name, frameData, frames, delay, loop)
126126
*/
127127
this.onStart = new Phaser.Signal();
128128

129+
/**
130+
* @property {Phaser.Signal|null} onUpdate - This event is dispatched when the Animation changes frame. By default this event is disabled due to its intensive nature. Enable it with: `Animation.enableUpdate = true`.
131+
* @default
132+
*/
133+
this.onUpdate = null;
134+
129135
/**
130136
* @property {Phaser.Signal} onComplete - This event is dispatched when this Animation completes playback. If the animation is set to loop this is never fired, listen for onAnimationLoop instead.
131137
*/
@@ -404,6 +410,11 @@ Phaser.Animation.prototype = {
404410
this._parent.__tilePattern = false;
405411
this._parent.tilingTexture = false;
406412
}
413+
414+
if (this.onUpdate)
415+
{
416+
this.onUpdate.dispatch(this, this.currentFrame);
417+
}
407418
}
408419

409420
return true;
@@ -453,6 +464,11 @@ Phaser.Animation.prototype = {
453464
this._parent.tilingTexture = false;
454465
}
455466
}
467+
468+
if (this.onUpdate)
469+
{
470+
this.onUpdate.dispatch(this, this.currentFrame);
471+
}
456472
}
457473

458474
},
@@ -497,6 +513,11 @@ Phaser.Animation.prototype = {
497513
this._parent.tilingTexture = false;
498514
}
499515
}
516+
517+
if (this.onUpdate)
518+
{
519+
this.onUpdate.dispatch(this, this.currentFrame);
520+
}
500521
}
501522

502523
},
@@ -535,6 +556,11 @@ Phaser.Animation.prototype = {
535556
this.onLoop.dispose();
536557
this.onComplete.dispose();
537558

559+
if (this.onUpdate)
560+
{
561+
this.onUpdate.dispose();
562+
}
563+
538564
},
539565

540566
/**
@@ -638,6 +664,11 @@ Object.defineProperty(Phaser.Animation.prototype, 'frame', {
638664
{
639665
this._frameIndex = value;
640666
this._parent.setFrame(this.currentFrame);
667+
668+
if (this.onUpdate)
669+
{
670+
this.onUpdate.dispatch(this, this.currentFrame);
671+
}
641672
}
642673

643674
}
@@ -667,6 +698,34 @@ Object.defineProperty(Phaser.Animation.prototype, 'speed', {
667698

668699
});
669700

701+
/**
702+
* @name Phaser.Animation#enableUpdate
703+
* @property {boolean} enableUpdate - Gets or sets if this animation will dispatch the onUpdate events upon changing frame.
704+
*/
705+
Object.defineProperty(Phaser.Animation.prototype, 'enableUpdate', {
706+
707+
get: function () {
708+
709+
return (this.onUpdate !== null);
710+
711+
},
712+
713+
set: function (value) {
714+
715+
if (value && this.onUpdate === null)
716+
{
717+
this.onUpdate = new Phaser.Signal();
718+
}
719+
else if (!value && this.onUpdate !== null)
720+
{
721+
this.onUpdate.dispose();
722+
this.onUpdate = null;
723+
}
724+
725+
}
726+
727+
});
728+
670729
/**
671730
* Really handy function for when you are creating arrays of animation data but it's using frame names and not numbers.
672731
* For example imagine you've got 30 frames named: 'explosion_0001-large' to 'explosion_0030-large'

0 commit comments

Comments
 (0)