Skip to content

Commit 8da8fbe

Browse files
committed
fix animations with yoyo mode (issue: phaserjs#3837)
1 parent bb17c82 commit 8da8fbe

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

src/animations/Animation.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -585,16 +585,20 @@ var Animation = new Class({
585585
if (component._yoyo)
586586
{
587587
component.forward = false;
588-
589-
component.updateFrame(frame.prevFrame);
590-
591-
// Delay for the current frame
592-
this.getNextTick(component);
588+
this._updateAndGetNextTick(component, frame.prevFrame);
593589
}
594590
else if (component.repeatCounter > 0)
595591
{
596592
// Repeat (happens before complete)
597-
this.repeatAnimation(component);
593+
594+
if(component._reverse && component.forward)
595+
{
596+
component.forward = false;
597+
}
598+
else
599+
{
600+
this.repeatAnimation(component);
601+
}
598602
}
599603
else
600604
{
@@ -603,9 +607,7 @@ var Animation = new Class({
603607
}
604608
else
605609
{
606-
component.updateFrame(frame.nextFrame);
607-
608-
this.getNextTick(component);
610+
this._updateAndGetNextTick(component, frame.nextFrame);
609611
}
610612
},
611613

@@ -639,18 +641,22 @@ var Animation = new Class({
639641
{
640642
// We're at the start of the animation
641643

642-
if (component.repeatCounter > 0)
644+
if (component._yoyo)
645+
{
646+
component.forward = true;
647+
this._updateAndGetNextTick(component, frame.nextFrame);
648+
}
649+
else if (component.repeatCounter > 0)
643650
{
644-
if(!component.forward)
651+
if(component._reverse && !component.forward)
645652
{
646653
component.currentFrame = this.getLastFrame();
647-
648-
component.updateFrame(component.currentFrame);
649-
this.getNextTick(component);
654+
this._updateAndGetNextTick(component, component.currentFrame);
650655
}
651656
else
652657
{
653658
// Repeat (happens before complete)
659+
component.forward = true;
654660
this.repeatAnimation(component);
655661
}
656662
}
@@ -661,12 +667,24 @@ var Animation = new Class({
661667
}
662668
else
663669
{
664-
component.updateFrame(frame.prevFrame);
665-
666-
this.getNextTick(component);
670+
this._updateAndGetNextTick(component, frame.prevFrame);
667671
}
668672
},
669673

674+
/**
675+
* Update Frame and Wait next tick
676+
*
677+
* @method Phaser.Animations.Animation#_updateAndGetNextTick
678+
*
679+
* @param {Phaser.Animations.AnimationFrame} frame - An Animation frame
680+
*
681+
*/
682+
_updateAndGetNextTick: function (component, frame)
683+
{
684+
component.updateFrame(frame);
685+
this.getNextTick(component);
686+
},
687+
670688
/**
671689
* [description]
672690
*

src/gameobjects/components/Animation.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ var Animation = new Class({
208208
this._yoyo = false;
209209

210210
/**
211-
* Will the playhead move forwards (`true`) or in reverse (`false`)
211+
* Will the playhead move forwards (`true`) or in reverse (`false`).
212212
*
213213
* @name Phaser.GameObjects.Components.Animation#forward
214214
* @type {boolean}
@@ -217,6 +217,16 @@ var Animation = new Class({
217217
*/
218218
this.forward = true;
219219

220+
/**
221+
* An Internal trigger that's play the animation in reverse mode ('true') or not ('false'),
222+
* needed because forward can be changed by yoyo feature.
223+
*
224+
* @name Phaser.GameObjects.Components.Animation#forward
225+
* @type {boolean}
226+
* @default false
227+
*/
228+
this._reverse = false;
229+
220230
/**
221231
* Internal time overflow accumulator.
222232
*
@@ -497,6 +507,7 @@ var Animation = new Class({
497507
}
498508

499509
this.forward = true;
510+
this._reverse = false;
500511
return this._startAnimation(key, startFrame);
501512
},
502513

@@ -523,6 +534,7 @@ var Animation = new Class({
523534
}
524535

525536
this.forward = false;
537+
this._reverse = true;
526538
return this._startAnimation(key, startFrame);
527539
},
528540

@@ -575,6 +587,7 @@ var Animation = new Class({
575587
revert: function (key)
576588
{
577589
if (!this.isPlaying || this.currentAnim.key !== key) { return this.parent; }
590+
this._reverse = !this._reverse;
578591
this.forward = !this.forward;
579592

580593
return this.parent;

0 commit comments

Comments
 (0)