Skip to content

Commit

Permalink
Animation.setFrame allows you to set the animation to a specific fram…
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Apr 28, 2014
1 parent 7410814 commit b32312d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Version 2.0.4 - "Mos Shirare" - in development
* Color.HSLColorWheel will return an array with 360 color objects for each segment of an HSL color wheel, you can optionally set the saturation and lightness amounts.
* Timer.timeCap is a new setting allowing your Timers to protect against unexpectedly large delta timers (such as raf de-vis or CPU grind).
* Camera.unfollow allows you to easily unfollow a tracked object (thanks @alvinsight, #755)
* Animation.setFrame allows you to set the animation to a specific frame (thanks @adamholdenyall, #706)


### Bug Fixes
Expand Down
58 changes: 58 additions & 0 deletions src/animation/Animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,64 @@ Phaser.Animation.prototype = {

},

/**
* Sets this animations playback to a given frame with the given ID.
*
* @method Phaser.Animation#setFrame
* @param {string|number} [frameId] - The identifier of the frame to set. Can be the name of the frame, the sprite index of the frame, or the animation-local frame index.
* @param {boolean} [useLocalFrameIndex=false] - If you provide a number for frameId, should it use the numeric indexes of the frameData, or the 0-indexed frame index local to the animation.
*/
setFrame: function(frameId, useLocalFrameIndex) {

var frameIndex;

if (typeof useLocalFrameIndex === 'undefined')
{
useLocalFrameIndex = false;
}

// Find the index to the desired frame.
if (typeof frameId === "string")
{
for (var i = 0; i < this._frames.length; i++)
{
if (this._frameData.getFrame(this._frames[i]).name === frameId)
{
frameIndex = i;
}
}
}
else if (typeof frameId === "number")
{
if (useLocalFrameIndex)
{
frameIndex = frameId;
}
else
{
for (var i = 0; i < this._frames.length; i++)
{
if (this.frames[i] === frameIndex)
{
frameIndex = i;
}
}
}
}

if (frameIndex)
{
// Set the current frame index to the found index. Subtract 1 so that it animates to the desired frame on update.
this._frameIndex = frameIndex - 1;

// Make the animation update at next update
this._timeNextFrame = this.game.time.now;

this.update();
}

},

/**
* Stops playback of this animation and set it to a finished state. If a resetFrame is provided it will stop playback and set frame to the first in the animation.
* If `dispatchComplete` is true it will dispatch the complete events, otherwise they'll be ignored.
Expand Down
6 changes: 6 additions & 0 deletions src/animation/AnimationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Phaser.AnimationManager = function (sprite) {
*/
this.currentFrame = null;

/**
* @property {Phaser.Animation} currentAnim - The currently displayed animation, if any.
* @default
*/
this.currentAnim = null;

/**
* @property {boolean} updateIfVisible - Should the animation data continue to update even if the Sprite.visible is set to false.
* @default
Expand Down

0 comments on commit b32312d

Please sign in to comment.