mirror of
https://github.com/wassname/phaser.git
synced 2026-08-19 12:30:07 +08:00
AnimationManager done and in, need to fix texture update bug, otherwise finished.
This commit is contained in:
+206
-2
@@ -1,3 +1,207 @@
|
||||
Phaser.Animation = {
|
||||
/**
|
||||
* Animation
|
||||
*
|
||||
* An Animation instance contains a single animation and the controls to play it.
|
||||
* It is created by the AnimationManager and belongs to Game Objects such as Sprite.
|
||||
*
|
||||
* @package Phaser.Animation
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
||||
*
|
||||
* @param parent {Sprite} Owner sprite of this animation.
|
||||
* @param frameData {FrameData} The FrameData object contains animation data.
|
||||
* @param name {string} Unique name of this animation.
|
||||
* @param frames {number[]/string[]} An array of numbers or strings indicating what frames to play in what order.
|
||||
* @param delay {number} Time between frames in ms.
|
||||
* @param looped {bool} Whether or not the animation is looped or just plays once.
|
||||
*/
|
||||
Phaser.Animation = function (game, parent, frameData, name, frames, delay, looped) {
|
||||
|
||||
this.game = game;
|
||||
this._parent = parent;
|
||||
this._frames = frames;
|
||||
this._frameData = frameData;
|
||||
this.name = name;
|
||||
this.delay = 1000 / delay;
|
||||
this.looped = looped;
|
||||
this.isFinished = false;
|
||||
this.isPlaying = false;
|
||||
this._frameIndex = 0;
|
||||
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
Phaser.Animation.prototype = {
|
||||
|
||||
/**
|
||||
* Play this animation.
|
||||
* @param frameRate {number} FrameRate you want to specify instead of using default.
|
||||
* @param loop {bool} Whether or not the animation is looped or just plays once.
|
||||
*/
|
||||
play: function (frameRate, loop) {
|
||||
|
||||
if (typeof frameRate === "undefined") { frameRate = null; }
|
||||
if (typeof loop === "undefined") { loop = null; }
|
||||
|
||||
if (frameRate !== null)
|
||||
{
|
||||
this.delay = 1000 / frameRate;
|
||||
}
|
||||
|
||||
if (loop !== null)
|
||||
{
|
||||
// If they set a new loop value then use it, otherwise use the default set on creation
|
||||
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]);
|
||||
// this._parent.events.onAnimationStart.dispatch(this._parent, this);
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Play this animation from the first frame.
|
||||
*/
|
||||
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]);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop playing animation and set it finished.
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
this.isPlaying = false;
|
||||
this.isFinished = true;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Update animation frames.
|
||||
*/
|
||||
update: function () {
|
||||
|
||||
if (this.isPlaying == true && this.game.time.now >= this._timeNextFrame)
|
||||
{
|
||||
this._frameIndex++;
|
||||
|
||||
if (this._frameIndex == this._frames.length)
|
||||
{
|
||||
if (this.looped)
|
||||
{
|
||||
this._frameIndex = 0;
|
||||
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
||||
// this._parent.events.onAnimationLoop.dispatch(this._parent, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.onComplete();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
|
||||
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
||||
}
|
||||
|
||||
this._timeLastFrame = this.game.time.now;
|
||||
this._timeNextFrame = this.game.time.now + this.delay;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Clean up animation memory.
|
||||
*/
|
||||
destroy: function () {
|
||||
|
||||
this.game = null;
|
||||
this._parent = null;
|
||||
this._frames = null;
|
||||
this._frameData = null;
|
||||
this.currentFrame = null;
|
||||
this.isPlaying = false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Animation complete callback method.
|
||||
*/
|
||||
onComplete: function () {
|
||||
|
||||
this.isPlaying = false;
|
||||
this.isFinished = true;
|
||||
// this._parent.events.onAnimationComplete.dispatch(this._parent, this);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Object.defineProperty(Phaser.Animation.prototype, "frameTotal", {
|
||||
|
||||
get: function () {
|
||||
return this._frames.length;
|
||||
},
|
||||
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
|
||||
});
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
|
||||
});
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
/**
|
||||
* AnimationManager
|
||||
*
|
||||
* Any Game Object that supports animation contains a single AnimationManager instance. It is used to add,
|
||||
* play and update Phaser.Animation objects.
|
||||
*
|
||||
* @package Phaser.Components.AnimationManager
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
||||
*/
|
||||
Phaser.AnimationManager = function (parent) {
|
||||
|
||||
/**
|
||||
* Data contains animation frames.
|
||||
* @type {FrameData}
|
||||
*/
|
||||
this._frameData = null;
|
||||
|
||||
/**
|
||||
* Keeps track of the current frame of animation.
|
||||
*/
|
||||
this.currentFrame = null;
|
||||
|
||||
this._parent = parent;
|
||||
|
||||
this.game = parent.game;
|
||||
|
||||
this._anims = {};
|
||||
|
||||
};
|
||||
|
||||
Phaser.AnimationManager.prototype = {
|
||||
|
||||
/**
|
||||
* Load animation frame data.
|
||||
* @param frameData Data to be loaded.
|
||||
*/
|
||||
loadFrameData: function (frameData) {
|
||||
|
||||
this._frameData = frameData;
|
||||
this.frame = 0;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a new animation.
|
||||
* @param name {string} What this animation should be called (e.g. "run").
|
||||
* @param frames {any[]} An array of numbers/strings indicating what frames to play in what order (e.g. [1, 2, 3] or ['run0', 'run1', run2]).
|
||||
* @param frameRate {number} The speed in frames per second that the animation should play at (e.g. 60 fps).
|
||||
* @param loop {bool} Whether or not the animation is looped or just plays once.
|
||||
* @param useNumericIndex {bool} Use number indexes instead of string indexes?
|
||||
* @return {Animation} The Animation that was created
|
||||
*/
|
||||
add: function (name, frames, frameRate, loop, useNumericIndex) {
|
||||
|
||||
if (typeof frames === "undefined") { frames = null; }
|
||||
if (typeof frameRate === "undefined") { frameRate = 60; }
|
||||
if (typeof loop === "undefined") { loop = false; }
|
||||
if (typeof useNumericIndex === "undefined") { useNumericIndex = true; }
|
||||
|
||||
if (this._frameData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the signals the AnimationManager will emit
|
||||
if (this._parent.events.onAnimationStart == null)
|
||||
{
|
||||
// this._parent.events.onAnimationStart = new Phaser.Signal();
|
||||
// this._parent.events.onAnimationComplete = new Phaser.Signal();
|
||||
// this._parent.events.onAnimationLoop = new Phaser.Signal();
|
||||
}
|
||||
|
||||
if (frames == null)
|
||||
{
|
||||
frames = this._frameData.getFrameIndexes();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.validateFrames(frames, useNumericIndex) == false)
|
||||
{
|
||||
console.warn('Invalid frames given to Phaser.Animation ' + name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (useNumericIndex == false)
|
||||
{
|
||||
frames = this._frameData.getFrameIndexesByName(frames);
|
||||
}
|
||||
|
||||
this._anims[name] = new Phaser.Animation(this.game, this._parent, this._frameData, name, frames, frameRate, loop);
|
||||
this.currentAnim = this._anims[name];
|
||||
this.currentFrame = this.currentAnim.currentFrame;
|
||||
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
||||
|
||||
return this._anims[name];
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Check whether the frames is valid.
|
||||
* @param frames {any[]} Frames to be validated.
|
||||
* @param useNumericIndex {bool} Does these frames use number indexes or string indexes?
|
||||
* @return {bool} True if they're valid, otherwise return false.
|
||||
*/
|
||||
validateFrames: function (frames, useNumericIndex) {
|
||||
|
||||
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;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Play animation with specific name.
|
||||
* @param name {string} The string name of the animation you want to play.
|
||||
* @param frameRate {number} FrameRate you want to specify instead of using default.
|
||||
* @param loop {bool} Whether or not the animation is looped or just plays once.
|
||||
*/
|
||||
play: function (name, frameRate, loop) {
|
||||
|
||||
if (typeof frameRate === "undefined") { frameRate = null; }
|
||||
if (typeof loop === "undefined") { loop = null; }
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop animation by name.
|
||||
* Current animation will be automatically set to the stopped one.
|
||||
*/
|
||||
stop: function (name) {
|
||||
|
||||
if (this._anims[name])
|
||||
{
|
||||
this.currentAnim = this._anims[name];
|
||||
this.currentAnim.stop();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Update animation and parent sprite's bounds.
|
||||
*/
|
||||
update: function () {
|
||||
|
||||
if (this.currentAnim && this.currentAnim.update() == true)
|
||||
{
|
||||
this.currentFrame = this.currentAnim.currentFrame;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes all related references
|
||||
*/
|
||||
destroy: function () {
|
||||
|
||||
this._anims = {};
|
||||
this._frameData = null;
|
||||
this._frameIndex = 0;
|
||||
this.currentAnim = null;
|
||||
this.currentFrame = null;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Object.defineProperty(Phaser.AnimationManager.prototype, "frameData", {
|
||||
|
||||
get: function () {
|
||||
return this._frameData;
|
||||
},
|
||||
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.AnimationManager.prototype, "frameTotal", {
|
||||
|
||||
get: function () {
|
||||
|
||||
if (this._frameData)
|
||||
{
|
||||
return this._frameData.total;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
},
|
||||
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
|
||||
|
||||
get: function () {
|
||||
return this._frameIndex;
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
set: function (value) {
|
||||
|
||||
if (this._frameData && this._frameData.getFrame(value) !== null)
|
||||
{
|
||||
this.currentFrame = this._frameData.getFrame(value);
|
||||
this._frameIndex = value;
|
||||
console.log('AM set frame', value, this.currentFrame);
|
||||
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.AnimationManager.prototype, "frameName", {
|
||||
|
||||
get: function () {
|
||||
return this.currentFrame.name;
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
if (this._frameData && this._frameData.getFrameByName(value))
|
||||
{
|
||||
this.currentFrame = this._frameData.getFrameByName(value);
|
||||
this._frameIndex = this.currentFrame.index;
|
||||
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn("Cannot set frameName: " + value);
|
||||
}
|
||||
},
|
||||
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Reference in New Issue
Block a user