Skip to content

Commit 2e6af02

Browse files
committed
AnimationManager.add no longer sets the currentFrame property when just adding an Animation to a Sprite. The currentFrame property is now only set when the animation begins playing. This avoids the Sprite.frame and Sprite.frameName properties from returning incorrect results after adding (but not playing) an Animation. It also allows very short animations (2 frames) to play correctly without needing to loop.
1 parent 42dfe21 commit 2e6af02

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

src/animation/Animation.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Phaser.Animation.prototype = {
192192
this._timeNextFrame = this.game.time.time + this.delay;
193193

194194
this._frameIndex = 0;
195-
this.updateCurrentFrame(false);
195+
this.updateCurrentFrame(false, true);
196196

197197
this._parent.events.onAnimationStart$dispatch(this._parent, this);
198198

@@ -381,6 +381,7 @@ Phaser.Animation.prototype = {
381381
// And what's left now?
382382
this._timeNextFrame = this.game.time.time + (this.delay - this._frameDiff);
383383

384+
var fi = this._frameIndex;
384385
this._frameIndex += this._frameSkip;
385386

386387
if (this._frameIndex >= this._frames.length)
@@ -418,24 +419,39 @@ Phaser.Animation.prototype = {
418419
* Returns true if the current frame update was 'successful', false otherwise.
419420
*
420421
* @method Phaser.Animation#updateCurrentFrame
421-
* @param {bool} signalUpdate - If true th onUpdate signal will be triggered.
422+
* @param {boolean} signalUpdate - If true the `Animation.onUpdate` signal will be dispatched.
423+
* @param {boolean} fromPlay - Was this call made from the playing of a new animation?
422424
* @private
423425
*/
424-
updateCurrentFrame: function (signalUpdate) {
426+
updateCurrentFrame: function (signalUpdate, fromPlay) {
427+
428+
if (typeof fromPlay === 'undefined') { fromPlay = false; }
425429

426430
if (!this._frameData)
427431
{
428432
// The animation is already destroyed, probably from a callback
429433
return false;
430434
}
431435

432-
var idx = this.currentFrame.index;
433-
434-
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
436+
if (fromPlay)
437+
{
438+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
435439

436-
if (this.currentFrame && idx !== this.currentFrame.index)
440+
if (this.currentFrame && idx !== this.currentFrame.index)
441+
{
442+
this._parent.setFrame(this.currentFrame);
443+
}
444+
}
445+
else
437446
{
438-
this._parent.setFrame(this.currentFrame);
447+
var idx = this.currentFrame.index;
448+
449+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
450+
451+
if (this.currentFrame && idx !== this.currentFrame.index)
452+
{
453+
this._parent.setFrame(this.currentFrame);
454+
}
439455
}
440456

441457
if (this.onUpdate && signalUpdate)

src/animation/AnimationManager.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ Phaser.AnimationManager = function (sprite) {
2525
this.game = sprite.game;
2626

2727
/**
28-
* @property {Phaser.Frame} currentFrame - The currently displayed Frame of animation, if any.
28+
* The currently displayed Frame of animation, if any.
29+
* This property is only set once an Animation starts playing. Until that point it remains set as `null`.
30+
*
31+
* @property {Phaser.Frame} currentFrame
2932
* @default
3033
*/
3134
this.currentFrame = null;
@@ -196,14 +199,16 @@ Phaser.AnimationManager.prototype = {
196199
}
197200
}
198201

199-
this._outputFrames.length = 0;
202+
this._outputFrames = [];
200203

201204
this._frameData.getFrameIndexes(frames, useNumericIndex, this._outputFrames);
202205

203206
this._anims[name] = new Phaser.Animation(this.game, this.sprite, name, this._frameData, this._outputFrames, frameRate, loop);
204207

205208
this.currentAnim = this._anims[name];
206-
this.currentFrame = this.currentAnim.currentFrame;
209+
210+
// This shouldn't be set until the Animation is played, surely?
211+
// this.currentFrame = this.currentAnim.currentFrame;
207212

208213
if (this.sprite.tilingTexture)
209214
{
@@ -272,6 +277,7 @@ Phaser.AnimationManager.prototype = {
272277
this.currentAnim.paused = false;
273278
return this.currentAnim.play(frameRate, loop, killOnComplete);
274279
}
280+
275281
return this.currentAnim;
276282
}
277283
else

0 commit comments

Comments
 (0)