From 14109aeeb1711e18b9afdd965b282560acd29318 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Thu, 3 Jul 2014 16:23:36 +0100 Subject: [PATCH] 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()`. --- README.md | 2 + build/phaser.d.ts | 4 ++ src/animation/Animation.js | 88 +++++++++++++++++++++++++++++++ src/animation/AnimationManager.js | 32 +++++++++++ 4 files changed, 126 insertions(+) diff --git a/README.md b/README.md index de1d728fd..d7a7ea44d 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,8 @@ Version 2.0.6 - "Jornhill" - -in development- * 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. * Camera.updateTarget has had a make-over and now is a lot smoother under certain conditions (thanks @tjkopena, fix #966) * Signal.removeAll now has a new `context` parameter. If specified only listeners matching the given context are removed (thanks @lucbloom for the idea, #880) +* 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()`. ### Bug Fixes diff --git a/build/phaser.d.ts b/build/phaser.d.ts index b92469afe..3357ef97a 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -1111,9 +1111,11 @@ declare module Phaser { complete(): void; destroy(): void; static generateFrameNames(prefix: string, start: number, stop: number, suffix?: string, zeroPad?: number): string[]; + next(quantity?: number): void; onPause(): void; onResume(): void; play(frameRate?: number, loop?: boolean, killOnComplete?: boolean): Phaser.Animation; + previous(quantity?: number): void; restart(): void; setFrame(frameId?: any, useLocalFrameIndex?: boolean): void; stop(resetFrame?: boolean, dispatchComplete?: boolean): void; @@ -1139,7 +1141,9 @@ declare module Phaser { add(name: string, frames?: any[], frameRate?: number, loop?: boolean, useNumericIndex?: boolean): Phaser.Animation; destroy(): void; getAnimation(name: string): Phaser.Animation; + next(quantity?: number): void; play(name: string, frameRate?: number, loop?: boolean, killOnComplete?: boolean): Phaser.Animation; + previous(quantity?: number): void; refreshFrame(); stop(name?: string, resetFrame?: boolean): void; update(): boolean; diff --git a/src/animation/Animation.js b/src/animation/Animation.js index c9feedc4b..3d885bf5e 100644 --- a/src/animation/Animation.js +++ b/src/animation/Animation.js @@ -413,6 +413,94 @@ Phaser.Animation.prototype = { }, + /** + * Advances by the given number of frames in the Animation, taking the loop value into consideration. + * + * @method Phaser.Animation#next + * @param {number} [quantity=1] - The number of frames to advance. + */ + next: function (quantity) { + + if (typeof quantity === 'undefined') { quantity = 1; } + + var frame = this._frameIndex + quantity; + + if (frame >= this._frames.length) + { + if (this.loop) + { + frame %= this._frames.length; + } + else + { + frame = this._frames.length - 1; + } + } + + if (frame !== this._frameIndex) + { + this._frameIndex = frame; + + this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]); + + if (this.currentFrame) + { + this._parent.setFrame(this.currentFrame); + + if (this._parent.__tilePattern) + { + this._parent.__tilePattern = false; + this._parent.tilingTexture = false; + } + } + } + + }, + + /** + * Moves backwards the given number of frames in the Animation, taking the loop value into consideration. + * + * @method Phaser.Animation#previous + * @param {number} [quantity=1] - The number of frames to move back. + */ + previous: function (quantity) { + + if (typeof quantity === 'undefined') { quantity = 1; } + + var frame = this._frameIndex - quantity; + + if (frame < 0) + { + if (this.loop) + { + frame = this._frames.length + frame; + } + else + { + frame++; + } + } + + if (frame !== this._frameIndex) + { + this._frameIndex = frame; + + this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]); + + if (this.currentFrame) + { + this._parent.setFrame(this.currentFrame); + + if (this._parent.__tilePattern) + { + this._parent.__tilePattern = false; + this._parent.tilingTexture = false; + } + } + } + + }, + /** * Cleans up this animation ready for deletion. Nulls all values and references. * diff --git a/src/animation/AnimationManager.js b/src/animation/AnimationManager.js index 09b9ff089..5fa1f89a2 100644 --- a/src/animation/AnimationManager.js +++ b/src/animation/AnimationManager.js @@ -298,6 +298,38 @@ Phaser.AnimationManager.prototype = { }, + /** + * Advances by the given number of frames in the current animation, taking the loop value into consideration. + * + * @method Phaser.AnimationManager#next + * @param {number} [quantity=1] - The number of frames to advance. + */ + next: function (quantity) { + + if (this.currentAnim) + { + this.currentAnim.next(quantity); + this.currentFrame = this.currentAnim.currentFrame; + } + + }, + + /** + * Moves backwards the given number of frames in the current animation, taking the loop value into consideration. + * + * @method Phaser.AnimationManager#previous + * @param {number} [quantity=1] - The number of frames to move back. + */ + previous: function (quantity) { + + if (this.currentAnim) + { + this.currentAnim.previous(quantity); + this.currentFrame = this.currentAnim.currentFrame; + } + + }, + /** * Returns an animation that was previously added by name. *