Skip to content

Commit ebc9d8d

Browse files
committed
Add a 'revert' function that can revert the flow of a animation at any time (issue phaserjs#3837)
1 parent fb4f28b commit ebc9d8d

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/animations/Animation.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,14 @@ var Animation = new Class({
536536
component._yoyo = this.yoyo;
537537
}
538538

539-
component.updateFrame(this.frames[startFrame]);
539+
var frame = this.frames[startFrame];
540+
541+
if(startFrame === 0 && !component.forward)
542+
{
543+
frame = this.getLastFrame();
544+
}
545+
546+
component.updateFrame(frame);
540547
},
541548

542549
/**
@@ -602,6 +609,18 @@ var Animation = new Class({
602609
}
603610
},
604611

612+
/**
613+
* Returns the animation last frame.
614+
*
615+
* @method Phaser.Animations.Animation#getLastFrame
616+
*
617+
* @return {Phaser.Animations.AnimationFrame} component - The Animation Last Frame.
618+
*/
619+
getLastFrame: function ()
620+
{
621+
return this.frames[this.frames.length - 1];
622+
},
623+
605624
/**
606625
* [description]
607626
*
@@ -622,8 +641,18 @@ var Animation = new Class({
622641

623642
if (component.repeatCounter > 0)
624643
{
625-
// Repeat (happens before complete)
626-
this.repeatAnimation(component);
644+
if(!component.forward)
645+
{
646+
component.currentFrame = this.getLastFrame();
647+
648+
component.updateFrame(component.currentFrame);
649+
this.getNextTick(component);
650+
}
651+
else
652+
{
653+
// Repeat (happens before complete)
654+
this.repeatAnimation(component);
655+
}
627656
}
628657
else
629658
{
@@ -705,9 +734,7 @@ var Animation = new Class({
705734
{
706735
component.repeatCounter--;
707736

708-
component.forward = true;
709-
710-
component.updateFrame(component.currentFrame.nextFrame);
737+
component.updateFrame(component.currentFrame[(component.forward) ? 'nextFrame' : 'prevFrame']);
711738

712739
if (component.isPlaying)
713740
{

src/gameobjects/components/Animation.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,23 @@ var Animation = new Class({
520520
return gameObject;
521521
},
522522

523+
/**
524+
* Revert an Animation that is already playing on the Game Object.
525+
*
526+
* @method Phaser.GameObjects.Components.Animation#revert
527+
*
528+
* @param {string} key - The string-based key of the animation to play, as defined previously in the Animation Manager.
529+
*
530+
* @return {Phaser.GameObjects.GameObject} The Game Object that owns this Animation Component.
531+
*/
532+
revert: function (key)
533+
{
534+
if (!this.isPlaying || this.currentAnim.key !== key) { return this.parent; }
535+
this.forward = !this.forward;
536+
537+
return this.parent;
538+
},
539+
523540
/**
524541
* Returns a value between 0 and 1 indicating how far this animation is through, ignoring repeats and yoyos.
525542
* If the animation has a non-zero repeat defined, `getProgress` and `getTotalProgress` will be different

0 commit comments

Comments
 (0)