2013-08-30 03:20:14 +00:00
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
* @module Phaser.AnimationManager
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The AnimationManager is used to add, play and update Phaser Animations.
|
|
|
|
* Any Game Object such as Phaser.Sprite that supports animation contains a single AnimationManager instance.
|
2013-08-30 03:20:14 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.AnimationManager
|
2013-09-17 15:28:59 +00:00
|
|
|
* @constructor
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Sprite} sprite - A reference to the Game Object that owns this AnimationManager.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
Phaser.AnimationManager = function (sprite) {
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @property {Phaser.Sprite} sprite - A reference to the parent Sprite that owns this AnimationManager.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
|
|
|
this.sprite = sprite;
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
|
|
|
this.game = sprite.game;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Animation.Frame} currentFrame - The currently displayed Frame of animation, if any.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.currentFrame = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} updateIfVisible - Should the animation data continue to update even if the Sprite.visible is set to false.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
this.updateIfVisible = true;
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Animation.FrameData} _frameData - A temp. var for holding the currently playing Animations FrameData.
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
this._frameData = null;
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {object} _anims - An internal object that stores all of the Animation instances.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this._anims = {};
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {object} _outputFrames - An internal object to help avoid gc.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
this._outputFrames = [];
|
2013-09-10 19:40:34 +00:00
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.AnimationManager.prototype = {
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Loads FrameData into the internal temporary vars and resets the frame index to zero.
|
|
|
|
* This is called automatically when a new Sprite is created.
|
|
|
|
*
|
|
|
|
* @method loadFrameData
|
|
|
|
* @private
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Animation.FrameData} frameData - The FrameData set to load.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
loadFrameData: function (frameData) {
|
|
|
|
|
|
|
|
this._frameData = frameData;
|
|
|
|
this.frame = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* Adds a new animation under the given key. Optionally set the frames, frame rate and loop.
|
|
|
|
* Animations added in this way are played back with the play function.
|
|
|
|
*
|
|
|
|
* @method add
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {string} name - The unique (within this Sprite) name for the animation, i.e. "run", "fire", "walk".
|
|
|
|
* @param {Array} [frames=null] - An array of numbers/strings that correspond to the frames to add to this animation and in which order. e.g. [1, 2, 3] or ['run0', 'run1', run2]). If null then all frames will be used.
|
|
|
|
* @param {number} [frameRate=60] - The speed at which the animation should play. The speed is given in frames per second.
|
|
|
|
* @param {boolean} [loop=false] {boolean} - Whether or not the animation is looped or just plays once.
|
|
|
|
* @param {boolean} [useNumericIndex=true] - Are the given frames using numeric indexes (default) or strings?
|
2013-09-17 15:28:59 +00:00
|
|
|
* @return {Phaser.Animation} The Animation object that was created.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
add: function (name, frames, frameRate, loop, useNumericIndex) {
|
|
|
|
|
|
|
|
if (this._frameData == null)
|
|
|
|
{
|
2013-09-17 15:28:59 +00:00
|
|
|
console.warn('No FrameData available for Phaser.Animation ' + name);
|
2013-08-30 03:20:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
frameRate = frameRate || 60;
|
|
|
|
|
|
|
|
if (typeof loop === 'undefined') { loop = false; }
|
|
|
|
if (typeof useNumericIndex === 'undefined') { useNumericIndex = true; }
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
// Create the signals the AnimationManager will emit
|
2013-09-10 19:40:34 +00:00
|
|
|
if (this.sprite.events.onAnimationStart == null)
|
|
|
|
{
|
|
|
|
this.sprite.events.onAnimationStart = new Phaser.Signal();
|
|
|
|
this.sprite.events.onAnimationComplete = new Phaser.Signal();
|
|
|
|
this.sprite.events.onAnimationLoop = new Phaser.Signal();
|
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
this._outputFrames.length = 0;
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
this._frameData.getFrameIndexes(frames, useNumericIndex, this._outputFrames);
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
this._anims[name] = new Phaser.Animation(this.game, this.sprite, name, this._frameData, this._outputFrames, frameRate, loop);
|
2013-08-30 03:20:14 +00:00
|
|
|
this.currentAnim = this._anims[name];
|
|
|
|
this.currentFrame = this.currentAnim.currentFrame;
|
2013-09-10 19:40:34 +00:00
|
|
|
this.sprite.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
return this._anims[name];
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* Check whether the frames in the given array are valid and exist.
|
|
|
|
*
|
|
|
|
* @method validateFrames
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Array} frames - An array of frames to be validated.
|
|
|
|
* @param {boolean} [useNumericIndex=true] - Validate the frames based on their numeric index (true) or string index (false)
|
|
|
|
* @return {boolean} True if all given Frames are valid, otherwise false.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
validateFrames: function (frames, useNumericIndex) {
|
|
|
|
|
2013-09-16 00:52:15 +00:00
|
|
|
if (typeof useNumericIndex == 'undefined') { useNumericIndex = true; }
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
for (var i = 0; i < frames.length; i++)
|
|
|
|
{
|
|
|
|
if (useNumericIndex == true)
|
|
|
|
{
|
|
|
|
if (frames[i] > this._frameData.total)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this._frameData.checkFrameName(frames[i]) == false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* Play an animation based on the given key. The animation should previously have been added via sprite.animations.add()
|
|
|
|
* If the requested animation is already playing this request will be ignored. If you need to reset an already running animation do so directly on the Animation object itself.
|
|
|
|
*
|
|
|
|
* @method play
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {string} name - The name of the animation to be played, e.g. "fire", "walk", "jump".
|
|
|
|
* @param {number} [frameRate=null] - The framerate to play the animation at. The speed is given in frames per second. If not provided the previously set frameRate of the Animation is used.
|
|
|
|
* @param {boolean} [loop=null] - Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
|
2013-09-17 15:28:59 +00:00
|
|
|
* @return {Phaser.Animation} A reference to playing Animation instance.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
play: function (name, frameRate, loop) {
|
|
|
|
|
|
|
|
if (this._anims[name])
|
|
|
|
{
|
|
|
|
if (this.currentAnim == this._anims[name])
|
|
|
|
{
|
|
|
|
if (this.currentAnim.isPlaying == false)
|
|
|
|
{
|
|
|
|
return this.currentAnim.play(frameRate, loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.currentAnim = this._anims[name];
|
|
|
|
return this.currentAnim.play(frameRate, loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* Stop playback of an animation. If a name is given that specific animation is stopped, otherwise the current animation is stopped.
|
|
|
|
* The currentAnim property of the AnimationManager is automatically set to the animation given.
|
|
|
|
*
|
|
|
|
* @method stop
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {string} [name=null] - The name of the animation to be stopped, e.g. "fire". If none is given the currently running animation is stopped.
|
|
|
|
* @param {boolean} [resetFrame=false] - When the animation is stopped should the currentFrame be set to the first frame of the animation (true) or paused on the last frame displayed (false)
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
2013-09-15 19:45:00 +00:00
|
|
|
stop: function (name, resetFrame) {
|
|
|
|
|
|
|
|
if (typeof resetFrame == 'undefined') { resetFrame = false; }
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-15 02:58:38 +00:00
|
|
|
if (typeof name == 'string')
|
2013-08-30 03:20:14 +00:00
|
|
|
{
|
2013-09-15 02:58:38 +00:00
|
|
|
if (this._anims[name])
|
|
|
|
{
|
|
|
|
this.currentAnim = this._anims[name];
|
2013-09-15 19:45:00 +00:00
|
|
|
this.currentAnim.stop(resetFrame);
|
2013-09-15 02:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.currentAnim)
|
|
|
|
{
|
2013-09-15 19:45:00 +00:00
|
|
|
this.currentAnim.stop(resetFrame);
|
2013-09-15 02:58:38 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* The main update function is called by the Sprites update loop. It's responsible for updating animation frames and firing related events.
|
|
|
|
*
|
|
|
|
* @method update
|
|
|
|
* @protected
|
2013-10-01 12:54:29 +00:00
|
|
|
* @return {boolean} True if a new animation frame has been set, otherwise false.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
update: function () {
|
|
|
|
|
2013-09-10 19:40:34 +00:00
|
|
|
if (this.updateIfVisible && this.sprite.visible == false)
|
2013-09-02 22:22:24 +00:00
|
|
|
{
|
2013-09-03 14:35:40 +00:00
|
|
|
return false;
|
2013-09-02 22:22:24 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
if (this.currentAnim && this.currentAnim.update() == true)
|
|
|
|
{
|
|
|
|
this.currentFrame = this.currentAnim.currentFrame;
|
2013-09-10 19:40:34 +00:00
|
|
|
this.sprite.currentFrame = this.currentFrame;
|
2013-09-03 14:35:40 +00:00
|
|
|
return true;
|
2013-08-30 03:20:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
return false;
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Destroys all references this AnimationManager contains. Sets the _anims to a new object and nulls the current animation.
|
|
|
|
*
|
|
|
|
* @method destroy
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this._anims = {};
|
|
|
|
this._frameData = null;
|
|
|
|
this._frameIndex = 0;
|
|
|
|
this.currentAnim = null;
|
|
|
|
this.currentFrame = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @return {Phaser.Animation.FrameData} Returns the FrameData of the current animation.
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
Object.defineProperty(Phaser.AnimationManager.prototype, "frameData", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this._frameData;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @return {number} Returns the total number of frames in the loaded FrameData, or -1 if no FrameData is loaded.
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
Object.defineProperty(Phaser.AnimationManager.prototype, "frameTotal", {
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
get: function () {
|
|
|
|
|
|
|
|
if (this._frameData)
|
|
|
|
{
|
|
|
|
return this._frameData.total;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @return {boolean} Returns the paused state of the current animation.
|
|
|
|
*//**
|
|
|
|
* @param {boolean} value - Sets the paused state of the current animation.
|
|
|
|
*/
|
2013-09-30 10:15:50 +00:00
|
|
|
Object.defineProperty(Phaser.AnimationManager.prototype, "paused", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.currentAnim.isPaused;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.currentAnim.paused = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @return {number} Returns the index of the current frame.
|
|
|
|
*//**
|
|
|
|
* @param {number} value - Sets the current frame on the Sprite and updates the texture cache for display.
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
|
|
|
|
|
|
|
|
get: function () {
|
2013-09-02 22:22:24 +00:00
|
|
|
|
|
|
|
if (this.currentFrame)
|
|
|
|
{
|
|
|
|
return this._frameIndex;
|
|
|
|
}
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (this._frameData && this._frameData.getFrame(value) !== null)
|
|
|
|
{
|
|
|
|
this.currentFrame = this._frameData.getFrame(value);
|
|
|
|
this._frameIndex = value;
|
2013-09-10 19:40:34 +00:00
|
|
|
this.sprite.currentFrame = this.currentFrame;
|
|
|
|
this.sprite.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
2013-08-30 03:20:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @return {string} Returns the name of the current frame if it has one.
|
|
|
|
*//**
|
|
|
|
* @param {string} value - Sets the current frame on the Sprite and updates the texture cache for display.
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
Object.defineProperty(Phaser.AnimationManager.prototype, "frameName", {
|
|
|
|
|
|
|
|
get: function () {
|
2013-09-02 22:22:24 +00:00
|
|
|
|
|
|
|
if (this.currentFrame)
|
|
|
|
{
|
|
|
|
return this.currentFrame.name;
|
|
|
|
}
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
2013-09-18 05:34:56 +00:00
|
|
|
if (this._frameData && this._frameData.getFrameByName(value) !== null)
|
2013-08-30 03:20:14 +00:00
|
|
|
{
|
|
|
|
this.currentFrame = this._frameData.getFrameByName(value);
|
|
|
|
this._frameIndex = this.currentFrame.index;
|
2013-09-10 19:40:34 +00:00
|
|
|
this.sprite.currentFrame = this.currentFrame;
|
|
|
|
this.sprite.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
2013-08-30 03:20:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.warn("Cannot set frameName: " + value);
|
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
});
|