Skip to content

Commit b32312d

Browse files
committed
Animation.setFrame allows you to set the animation to a specific frame (thanks @adamholdenyall, phaserjs#706)
1 parent 7410814 commit b32312d

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Version 2.0.4 - "Mos Shirare" - in development
135135
* 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.
136136
* Timer.timeCap is a new setting allowing your Timers to protect against unexpectedly large delta timers (such as raf de-vis or CPU grind).
137137
* Camera.unfollow allows you to easily unfollow a tracked object (thanks @alvinsight, #755)
138+
* Animation.setFrame allows you to set the animation to a specific frame (thanks @adamholdenyall, #706)
138139

139140

140141
### Bug Fixes

src/animation/Animation.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,64 @@ Phaser.Animation.prototype = {
223223

224224
},
225225

226+
/**
227+
* Sets this animations playback to a given frame with the given ID.
228+
*
229+
* @method Phaser.Animation#setFrame
230+
* @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.
231+
* @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.
232+
*/
233+
setFrame: function(frameId, useLocalFrameIndex) {
234+
235+
var frameIndex;
236+
237+
if (typeof useLocalFrameIndex === 'undefined')
238+
{
239+
useLocalFrameIndex = false;
240+
}
241+
242+
// Find the index to the desired frame.
243+
if (typeof frameId === "string")
244+
{
245+
for (var i = 0; i < this._frames.length; i++)
246+
{
247+
if (this._frameData.getFrame(this._frames[i]).name === frameId)
248+
{
249+
frameIndex = i;
250+
}
251+
}
252+
}
253+
else if (typeof frameId === "number")
254+
{
255+
if (useLocalFrameIndex)
256+
{
257+
frameIndex = frameId;
258+
}
259+
else
260+
{
261+
for (var i = 0; i < this._frames.length; i++)
262+
{
263+
if (this.frames[i] === frameIndex)
264+
{
265+
frameIndex = i;
266+
}
267+
}
268+
}
269+
}
270+
271+
if (frameIndex)
272+
{
273+
// Set the current frame index to the found index. Subtract 1 so that it animates to the desired frame on update.
274+
this._frameIndex = frameIndex - 1;
275+
276+
// Make the animation update at next update
277+
this._timeNextFrame = this.game.time.now;
278+
279+
this.update();
280+
}
281+
282+
},
283+
226284
/**
227285
* 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.
228286
* If `dispatchComplete` is true it will dispatch the complete events, otherwise they'll be ignored.

src/animation/AnimationManager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Phaser.AnimationManager = function (sprite) {
3030
*/
3131
this.currentFrame = null;
3232

33+
/**
34+
* @property {Phaser.Animation} currentAnim - The currently displayed animation, if any.
35+
* @default
36+
*/
37+
this.currentAnim = null;
38+
3339
/**
3440
* @property {boolean} updateIfVisible - Should the animation data continue to update even if the Sprite.visible is set to false.
3541
* @default

0 commit comments

Comments
 (0)