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.
|
|
|
|
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
|
|
|
* @module Phaser.Animation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-08-30 03:20:14 +00:00
|
|
|
* An Animation instance contains a single animation and the controls to play it.
|
2013-09-17 15:28:59 +00:00
|
|
|
* It is created by the AnimationManager, consists of Animation.Frame objects and belongs to a single Game Object such as a Sprite.
|
2013-08-30 03:20:14 +00:00
|
|
|
*
|
2013-09-19 22:47:50 +00:00
|
|
|
* @class Phaser.Animation
|
2013-09-17 15:28:59 +00:00
|
|
|
* @constructor
|
2013-09-19 22:47:50 +00:00
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
* @param {Phaser.Sprite} parent - A reference to the owner of this Animation.
|
|
|
|
* @param {string} name - The unique name for this animation, used in playback commands.
|
|
|
|
* @param {Phaser.Animation.FrameData} frameData - The FrameData object that contains all frames used by this Animation.
|
|
|
|
* @param {(Array.<number>|Array.<string>)} frames - An array of numbers or strings indicating which frames to play in which order.
|
|
|
|
* @param {number} delay - The time between each frame of the animation, given in ms.
|
|
|
|
* @param {boolean} looped - Should this animation loop or play through once.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
Phaser.Animation = function (game, parent, name, frameData, frames, delay, looped) {
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.game = game;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {Phaser.Sprite} _parent - A reference to the parent Sprite that owns this Animation.
|
2013-09-17 15:28:59 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this._parent = parent;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {Phaser.FrameData} _frameData - The FrameData the Animation uses.
|
2013-09-17 15:28:59 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._frameData = frameData;
|
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {string} name - The user defined name given to this Animation.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {object} _frames
|
2013-09-17 15:28:59 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2013-09-19 22:47:50 +00:00
|
|
|
this._frames = [];
|
|
|
|
this._frames = this._frames.concat(frames);
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {number} delay - The delay in ms between each frame of the Animation.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.delay = 1000 / delay;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {boolean} looped - The loop state of the Animation.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.looped = looped;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {boolean} isFinished - The finished state of the Animation. Set to true once playback completes, false during playback.
|
|
|
|
* @default
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.isFinished = false;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {boolean} isPlaying - The playing state of the Animation. Set to false once playback completes, true during playback.
|
|
|
|
* @default
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.isPlaying = false;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2013-09-27 08:57:08 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} isPaused - The paused state of the Animation.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.isPaused = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _pauseStartTime - The time the animation paused.
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._pauseStartTime = 0;
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {number} _frameIndex
|
2013-09-17 15:28:59 +00:00
|
|
|
* @private
|
2013-09-19 22:47:50 +00:00
|
|
|
* @default
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this._frameIndex = 0;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2013-09-24 14:28:29 +00:00
|
|
|
this._frameDiff = 0;
|
|
|
|
this._frameSkip = 1;
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
2013-09-19 22:47:50 +00:00
|
|
|
* @property {Phaser.Animation.Frame} currentFrame - The currently displayed frame of the Animation.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Animation.prototype = {
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Plays this animation.
|
|
|
|
*
|
|
|
|
* @method play
|
|
|
|
* @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.
|
|
|
|
* @return {Phaser.Animation} A reference to this Animation instance.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
play: function (frameRate, loop) {
|
|
|
|
|
2013-09-27 08:57:08 +00:00
|
|
|
if (typeof frameRate === 'number')
|
2013-08-30 03:20:14 +00:00
|
|
|
{
|
2013-09-27 08:57:08 +00:00
|
|
|
// If they set a new frame rate then use it, otherwise use the one set on creation
|
2013-09-18 05:41:41 +00:00
|
|
|
this.delay = 1000 / frameRate;
|
2013-08-30 03:20:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 08:57:08 +00:00
|
|
|
if (typeof loop === 'boolean')
|
2013-08-30 03:20:14 +00:00
|
|
|
{
|
2013-09-27 08:57:08 +00:00
|
|
|
// If they set a new loop value then use it, otherwise use the one set on creation
|
2013-08-30 03:20:14 +00:00
|
|
|
this.looped = loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isPlaying = true;
|
|
|
|
this.isFinished = false;
|
|
|
|
|
|
|
|
this._timeLastFrame = this.game.time.now;
|
|
|
|
this._timeNextFrame = this.game.time.now + this.delay;
|
|
|
|
|
|
|
|
this._frameIndex = 0;
|
|
|
|
|
|
|
|
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
|
|
|
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
if (this._parent.events)
|
|
|
|
{
|
|
|
|
this._parent.events.onAnimationStart.dispatch(this._parent, this);
|
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Sets this animation back to the first frame and restarts the animation.
|
|
|
|
*
|
|
|
|
* @method restart
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
restart: function () {
|
|
|
|
|
|
|
|
this.isPlaying = true;
|
|
|
|
this.isFinished = false;
|
|
|
|
|
|
|
|
this._timeLastFrame = this.game.time.now;
|
|
|
|
this._timeNextFrame = this.game.time.now + this.delay;
|
|
|
|
|
|
|
|
this._frameIndex = 0;
|
|
|
|
|
|
|
|
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @method stop
|
|
|
|
* @param {Boolean} [resetFrame=false] If true after the animation stops the currentFrame value will be set to the first frame in this animation.
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
2013-09-15 19:45:00 +00:00
|
|
|
stop: function (resetFrame) {
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
if (typeof resetFrame === 'undefined') { resetFrame = false; }
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
this.isPlaying = false;
|
|
|
|
this.isFinished = true;
|
|
|
|
|
2013-09-15 19:45:00 +00:00
|
|
|
if (resetFrame)
|
|
|
|
{
|
|
|
|
this.currentFrame = this._frameData.getFrame(this._frames[0]);
|
|
|
|
}
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Updates this animation. Called automatically by the AnimationManager.
|
|
|
|
*
|
|
|
|
* @method update
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
update: function () {
|
|
|
|
|
2013-09-27 08:57:08 +00:00
|
|
|
if (this.isPaused)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
if (this.isPlaying == true && this.game.time.now >= this._timeNextFrame)
|
|
|
|
{
|
2013-09-24 14:28:29 +00:00
|
|
|
this._frameSkip = 1;
|
|
|
|
|
|
|
|
// Lagging?
|
|
|
|
this._frameDiff = this.game.time.now - this._timeNextFrame;
|
2013-08-30 03:20:14 +00:00
|
|
|
|
2013-09-24 14:28:29 +00:00
|
|
|
this._timeLastFrame = this.game.time.now;
|
|
|
|
|
|
|
|
if (this._frameDiff > this.delay)
|
|
|
|
{
|
|
|
|
// We need to skip a frame, work out how many
|
|
|
|
this._frameSkip = Math.floor(this._frameDiff / this.delay);
|
|
|
|
|
|
|
|
this._frameDiff -= (this._frameSkip * this.delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And what's left now?
|
|
|
|
this._timeNextFrame = this.game.time.now + (this.delay - this._frameDiff);
|
|
|
|
|
|
|
|
this._frameIndex += this._frameSkip;
|
|
|
|
|
|
|
|
if (this._frameIndex >= this._frames.length)
|
2013-08-30 03:20:14 +00:00
|
|
|
{
|
|
|
|
if (this.looped)
|
|
|
|
{
|
2013-09-30 03:28:17 +00:00
|
|
|
this._frameIndex %= this._frames.length;
|
2013-08-30 03:20:14 +00:00
|
|
|
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
2013-09-27 08:57:08 +00:00
|
|
|
|
|
|
|
if (this.currentFrame)
|
|
|
|
{
|
|
|
|
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:40:34 +00:00
|
|
|
this._parent.events.onAnimationLoop.dispatch(this._parent, this);
|
2013-08-30 03:20:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.onComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
|
|
|
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Cleans up this animation ready for deletion. Nulls all values and references.
|
|
|
|
*
|
|
|
|
* @method destroy
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this.game = null;
|
|
|
|
this._parent = null;
|
|
|
|
this._frames = null;
|
|
|
|
this._frameData = null;
|
|
|
|
this.currentFrame = null;
|
|
|
|
this.isPlaying = false;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Called internally when the animation finishes playback. Sets the isPlaying and isFinished states and dispatches the onAnimationComplete event if it exists on the parent.
|
|
|
|
*
|
|
|
|
* @method onComplete
|
2013-08-30 03:20:14 +00:00
|
|
|
*/
|
|
|
|
onComplete: function () {
|
|
|
|
|
|
|
|
this.isPlaying = false;
|
|
|
|
this.isFinished = true;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
|
|
|
if (this._parent.events)
|
|
|
|
{
|
|
|
|
this._parent.events.onAnimationComplete.dispatch(this._parent, this);
|
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-09-27 08:57:08 +00:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Animation.prototype, "paused", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.isPaused;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.isPaused = value;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
// Paused
|
|
|
|
this._pauseStartTime = this.game.time.now;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Un-paused
|
|
|
|
if (this.isPlaying)
|
|
|
|
{
|
|
|
|
this._timeNextFrame = this.game.time.now + this.delay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-08-30 03:20:14 +00:00
|
|
|
Object.defineProperty(Phaser.Animation.prototype, "frameTotal", {
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* @method frameTotal
|
|
|
|
* @return {Number} The total number of frames in this animation.
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
get: function () {
|
|
|
|
return this._frames.length;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-24 14:28:29 +00:00
|
|
|
/**
|
|
|
|
* Sets the current frame to the given frame index and updates the texture cache.
|
|
|
|
* @param {number} value - The frame to display
|
|
|
|
*
|
|
|
|
*//**
|
|
|
|
*
|
|
|
|
* Returns the current frame, or if not set the index of the most recent frame.
|
|
|
|
* @returns {Animation.Frame}
|
|
|
|
*
|
|
|
|
*/
|
2013-08-30 03:20:14 +00:00
|
|
|
Object.defineProperty(Phaser.Animation.prototype, "frame", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
if (this.currentFrame !== null)
|
|
|
|
{
|
|
|
|
return this.currentFrame.index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this._frameIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.currentFrame = this._frameData.getFrame(value);
|
|
|
|
|
|
|
|
if (this.currentFrame !== null)
|
|
|
|
{
|
|
|
|
this._frameIndex = value;
|
|
|
|
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-30 03:20:14 +00:00
|
|
|
|
|
|
|
});
|
2013-09-27 08:57:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Really handy function for when you are creating arrays of animation data but it's using frame names and not numbers.
|
|
|
|
* For example imagine you've got 30 frames named: 'explosion_0001-large' to 'explosion_0030-large'
|
|
|
|
* You could use this function to generate those by doing: Phaser.Animation.generateFrameNames('explosion_', 1, 30, '-large', 4);
|
|
|
|
* The zeroPad value dictates how many zeroes to add in front of the numbers (if any)
|
|
|
|
*/
|
|
|
|
Phaser.Animation.generateFrameNames = function (prefix, min, max, suffix, zeroPad) {
|
|
|
|
|
|
|
|
if (typeof suffix == 'undefined') { suffix = ''; }
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
var frame = '';
|
|
|
|
|
|
|
|
for (var i = min; i <= max; i++)
|
|
|
|
{
|
|
|
|
if (typeof zeroPad == 'number')
|
|
|
|
{
|
|
|
|
// str, len, pad, dir
|
|
|
|
frame = Phaser.Utils.pad(i.toString(), zeroPad, '0', 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
frame = i.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = prefix + frame + suffix;
|
|
|
|
|
|
|
|
output.push(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
2013-09-30 03:04:29 +00:00
|
|
|
}
|