Skip to content

Commit 14109ae

Browse files
committed
Animation.next will advance to the next frame in the animation, even if it's not currently playing. You can optionally define the number of frames to advance, but the default is 1. This is also aliased from the AnimationManager, so you can do Sprite.animations.next().
Animation.previous will rewind to the previous frame in the animation, even if it's not currently playing. You can optionally define the number of frames to rewind, but the default is 1. This is also aliased from the AnimationManager, so you can do `Sprite.animations.previous()`.
1 parent e4536f9 commit 14109ae

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ Version 2.0.6 - "Jornhill" - -in development-
128128
* Internal child movements in Group (such as bringToTop) now uses the new `silent` parameter to avoid the child emitting incorrect Group addition and deletion events.
129129
* Camera.updateTarget has had a make-over and now is a lot smoother under certain conditions (thanks @tjkopena, fix #966)
130130
* Signal.removeAll now has a new `context` parameter. If specified only listeners matching the given context are removed (thanks @lucbloom for the idea, #880)
131+
* Animation.next will advance to the next frame in the animation, even if it's not currently playing. You can optionally define the number of frames to advance, but the default is 1. This is also aliased from the AnimationManager, so you can do `Sprite.animations.next()`.
132+
* Animation.previous will rewind to the previous frame in the animation, even if it's not currently playing. You can optionally define the number of frames to rewind, but the default is 1. This is also aliased from the AnimationManager, so you can do `Sprite.animations.previous()`.
131133

132134

133135
### Bug Fixes

build/phaser.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,11 @@ declare module Phaser {
11111111
complete(): void;
11121112
destroy(): void;
11131113
static generateFrameNames(prefix: string, start: number, stop: number, suffix?: string, zeroPad?: number): string[];
1114+
next(quantity?: number): void;
11141115
onPause(): void;
11151116
onResume(): void;
11161117
play(frameRate?: number, loop?: boolean, killOnComplete?: boolean): Phaser.Animation;
1118+
previous(quantity?: number): void;
11171119
restart(): void;
11181120
setFrame(frameId?: any, useLocalFrameIndex?: boolean): void;
11191121
stop(resetFrame?: boolean, dispatchComplete?: boolean): void;
@@ -1139,7 +1141,9 @@ declare module Phaser {
11391141
add(name: string, frames?: any[], frameRate?: number, loop?: boolean, useNumericIndex?: boolean): Phaser.Animation;
11401142
destroy(): void;
11411143
getAnimation(name: string): Phaser.Animation;
1144+
next(quantity?: number): void;
11421145
play(name: string, frameRate?: number, loop?: boolean, killOnComplete?: boolean): Phaser.Animation;
1146+
previous(quantity?: number): void;
11431147
refreshFrame();
11441148
stop(name?: string, resetFrame?: boolean): void;
11451149
update(): boolean;

src/animation/Animation.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,94 @@ Phaser.Animation.prototype = {
413413

414414
},
415415

416+
/**
417+
* Advances by the given number of frames in the Animation, taking the loop value into consideration.
418+
*
419+
* @method Phaser.Animation#next
420+
* @param {number} [quantity=1] - The number of frames to advance.
421+
*/
422+
next: function (quantity) {
423+
424+
if (typeof quantity === 'undefined') { quantity = 1; }
425+
426+
var frame = this._frameIndex + quantity;
427+
428+
if (frame >= this._frames.length)
429+
{
430+
if (this.loop)
431+
{
432+
frame %= this._frames.length;
433+
}
434+
else
435+
{
436+
frame = this._frames.length - 1;
437+
}
438+
}
439+
440+
if (frame !== this._frameIndex)
441+
{
442+
this._frameIndex = frame;
443+
444+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
445+
446+
if (this.currentFrame)
447+
{
448+
this._parent.setFrame(this.currentFrame);
449+
450+
if (this._parent.__tilePattern)
451+
{
452+
this._parent.__tilePattern = false;
453+
this._parent.tilingTexture = false;
454+
}
455+
}
456+
}
457+
458+
},
459+
460+
/**
461+
* Moves backwards the given number of frames in the Animation, taking the loop value into consideration.
462+
*
463+
* @method Phaser.Animation#previous
464+
* @param {number} [quantity=1] - The number of frames to move back.
465+
*/
466+
previous: function (quantity) {
467+
468+
if (typeof quantity === 'undefined') { quantity = 1; }
469+
470+
var frame = this._frameIndex - quantity;
471+
472+
if (frame < 0)
473+
{
474+
if (this.loop)
475+
{
476+
frame = this._frames.length + frame;
477+
}
478+
else
479+
{
480+
frame++;
481+
}
482+
}
483+
484+
if (frame !== this._frameIndex)
485+
{
486+
this._frameIndex = frame;
487+
488+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
489+
490+
if (this.currentFrame)
491+
{
492+
this._parent.setFrame(this.currentFrame);
493+
494+
if (this._parent.__tilePattern)
495+
{
496+
this._parent.__tilePattern = false;
497+
this._parent.tilingTexture = false;
498+
}
499+
}
500+
}
501+
502+
},
503+
416504
/**
417505
* Cleans up this animation ready for deletion. Nulls all values and references.
418506
*

src/animation/AnimationManager.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,38 @@ Phaser.AnimationManager.prototype = {
298298

299299
},
300300

301+
/**
302+
* Advances by the given number of frames in the current animation, taking the loop value into consideration.
303+
*
304+
* @method Phaser.AnimationManager#next
305+
* @param {number} [quantity=1] - The number of frames to advance.
306+
*/
307+
next: function (quantity) {
308+
309+
if (this.currentAnim)
310+
{
311+
this.currentAnim.next(quantity);
312+
this.currentFrame = this.currentAnim.currentFrame;
313+
}
314+
315+
},
316+
317+
/**
318+
* Moves backwards the given number of frames in the current animation, taking the loop value into consideration.
319+
*
320+
* @method Phaser.AnimationManager#previous
321+
* @param {number} [quantity=1] - The number of frames to move back.
322+
*/
323+
previous: function (quantity) {
324+
325+
if (this.currentAnim)
326+
{
327+
this.currentAnim.previous(quantity);
328+
this.currentFrame = this.currentAnim.currentFrame;
329+
}
330+
331+
},
332+
301333
/**
302334
* Returns an animation that was previously added by name.
303335
*

0 commit comments

Comments
 (0)