Skip to content

Commit 11805b3

Browse files
author
John Doe
committed
Added a reverse functionality to animations.
1 parent 9f28d06 commit 11805b3

3 files changed

Lines changed: 55 additions & 9 deletions

File tree

src/animation/Animation.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* An Animation instance contains a single animation and the controls to play it.
9-
*
9+
*
1010
* It is created by the AnimationManager, consists of Animation.Frame objects and belongs to a single Game Object such as a Sprite.
1111
*
1212
* @class Phaser.Animation
@@ -130,10 +130,10 @@ Phaser.Animation = function (game, parent, name, frameData, frames, frameRate, l
130130
this.onStart = new Phaser.Signal();
131131

132132
/**
133-
* This event is dispatched when the Animation changes frame.
133+
* This event is dispatched when the Animation changes frame.
134134
* By default this event is disabled due to its intensive nature. Enable it with: `Animation.enableUpdate = true`.
135135
* Note that the event is only dispatched with the current frame. In a low-FPS environment Animations
136-
* will automatically frame-skip to try and claw back time, so do not base your code on expecting to
136+
* will automatically frame-skip to try and claw back time, so do not base your code on expecting to
137137
* receive a perfectly sequential set of frames from this event.
138138
* @property {Phaser.Signal|null} onUpdate
139139
* @default
@@ -150,6 +150,12 @@ Phaser.Animation = function (game, parent, name, frameData, frames, frameRate, l
150150
*/
151151
this.onLoop = new Phaser.Signal();
152152

153+
/**
154+
* @property {boolean} isReversed - Indicates if the animation will play backwards.
155+
* @default
156+
*/
157+
this.isReversed = false;
158+
153159
// Set-up some event listeners
154160
this.game.onPause.add(this.onPause, this);
155161
this.game.onResume.add(this.onResume, this);
@@ -385,14 +391,23 @@ Phaser.Animation.prototype = {
385391
// And what's left now?
386392
this._timeNextFrame = this.game.time.time + (this.delay - this._frameDiff);
387393

388-
this._frameIndex += this._frameSkip;
394+
if (this.isReversed){
395+
this._frameIndex -= this._frameSkip;
396+
}else{
397+
this._frameIndex += this._frameSkip;
398+
}
389399

390-
if (this._frameIndex >= this._frames.length)
400+
if (!this.isReversed && this._frameIndex >= this._frames.length || this.isReversed && this._frameIndex <= 0)
391401
{
392402
if (this.loop)
393403
{
394404
// Update current state before event callback
395-
this._frameIndex %= this._frames.length;
405+
this._frameIndex = Math.abs(this._frameIndex) % this._frames.length;
406+
407+
if (this.isReversed){
408+
this._frameIndex = this._frames.length - 1 - this._frameIndex;
409+
}
410+
396411
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
397412

398413
// Instead of calling updateCurrentFrame we do it here instead
@@ -454,7 +469,7 @@ Phaser.Animation.prototype = {
454469
// The animation is already destroyed, probably from a callback
455470
return false;
456471
}
457-
472+
458473
// Previous index
459474
var idx = this.currentFrame.index;
460475

@@ -654,6 +669,26 @@ Object.defineProperty(Phaser.Animation.prototype, 'paused', {
654669

655670
});
656671

672+
/**
673+
* @name Phaser.Animation#reversed
674+
* @property {boolean} reversed - Gets and sets the isReversed state of this Animation.
675+
*/
676+
Object.defineProperty(Phaser.Animation.prototype, 'reversed', {
677+
678+
get: function () {
679+
680+
return this.isReversed;
681+
682+
},
683+
684+
set: function (value) {
685+
686+
this.isReversed = value;
687+
688+
}
689+
690+
});
691+
657692
/**
658693
* @name Phaser.Animation#frameTotal
659694
* @property {number} frameTotal - The total number of frames in the currently loaded FrameData, or -1 if no FrameData is loaded.

typescript/phaser.comments.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ declare module Phaser {
129129
*/
130130
isPlaying: boolean;
131131

132+
/**
133+
* The playing direction of the Animation.
134+
*/
135+
isReversed: boolean;
136+
132137
/**
133138
* Should the parent of this Animation be killed when the animation completes?
134139
*/
@@ -178,6 +183,11 @@ declare module Phaser {
178183
*/
179184
paused: boolean;
180185

186+
/**
187+
* Gets and sets the reversed state of this Animation.
188+
*/
189+
reversed: boolean;
190+
181191
/**
182192
* Gets or sets the current speed of the animation in frames per second. Changing this in a playing animation will take effect from the next frame. Minimum value is 1.
183193
*/

typescript/phaser.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ declare module Phaser {
8181
onStart: Phaser.Signal;
8282
onUpdate: Phaser.Signal;
8383
paused: boolean;
84+
reversed: boolean;
8485
speed: number;
8586

8687
complete(): void;
@@ -3527,8 +3528,8 @@ declare module Phaser {
35273528
}
35283529

35293530
module Plugin {
3530-
3531-
class SaveCPU extends Phaser.Plugin {
3531+
3532+
class SaveCPU extends Phaser.Plugin {
35323533

35333534
renderOnFPS: number;
35343535
renderOnPointerChange: boolean;

0 commit comments

Comments
 (0)