1.0.3 release - fixed Text and Bitmap Fonts, Animation documentation and more examples

This commit is contained in:
Richard Davey
2013-09-17 16:28:59 +01:00
parent f4734b1143
commit 3c5ea01e09
29 changed files with 1253 additions and 392 deletions
+140 -75
View File
@@ -1,44 +1,89 @@
/**
* AnimationManager
* @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
*/
/**
* 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.
*
* 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
* @class AnimationManager
* @constructor
* @param {Phaser.Sprite} sprite A reference to the Game Object that owns this AnimationManager.
*/
Phaser.AnimationManager = function (sprite) {
/**
* Data contains animation frames.
* @type {FrameData}
*/
this._frameData = null;
/**
* Keeps track of the current frame of the animation.
*/
this.currentFrame = null;
/**
* A reference to the parent Sprite that owns this AnimationManager.
* @property sprite
* @public
* @type {Phaser.Sprite}
*/
this.sprite = sprite;
/**
* A reference to the currently running Game.
* @property game
* @public
* @type {Phaser.Game}
*/
this.game = sprite.game;
/**
* The currently displayed Frame of animation, if any.
* @property currentFrame
* @public
* @type {Phaser.Animation.Frame}
*/
this.currentFrame = null;
/**
* Should the animation data continue to update even if the Sprite.visible is set to false.
* @property updateIfVisible
* @public
* @type {Boolean}
* @default true
*/
this.updateIfVisible = true;
/**
* A temp. var for holding the currently playing Animations FrameData.
* @property _frameData
* @private
* @type {Phaser.Animation.FrameData}
*/
this._frameData = null;
/**
* An internal object that stores all of the Animation instances.
* @property _anims
* @private
* @type {Object}
*/
this._anims = {};
this.updateIfVisible = true;
/**
* An internal object to help avoid gc.
* @property _outputFrames
* @private
* @type {Object}
*/
this._outputFrames = [];
};
Phaser.AnimationManager.prototype = {
/**
* Load animation frame data.
* @param frameData Data to be loaded.
*/
/**
* 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
* @param {Phaser.Animation.FrameData} frameData The FrameData set to load.
*/
loadFrameData: function (frameData) {
this._frameData = frameData;
@@ -47,28 +92,30 @@ Phaser.AnimationManager.prototype = {
},
/**
* 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
* 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
* @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] {bool} 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? (false)
* @return {Phaser.Animation} The Animation object that was created.
*/
add: function (name, frames, frameRate, loop, useNumericIndex) {
frames = frames || null;
frameRate = frameRate || 60;
if (typeof loop == 'undefined') { loop = false; }
if (typeof useNumericIndex == 'undefined') { useNumericIndex = true; }
if (this._frameData == null)
{
console.warn('No frameData available for Phaser.Animation ' + name);
console.warn('No FrameData available for Phaser.Animation ' + name);
return;
}
frameRate = frameRate || 60;
if (typeof loop === 'undefined') { loop = false; }
if (typeof useNumericIndex === 'undefined') { useNumericIndex = true; }
// Create the signals the AnimationManager will emit
if (this.sprite.events.onAnimationStart == null)
{
@@ -77,25 +124,11 @@ Phaser.AnimationManager.prototype = {
this.sprite.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;
}
}
this._outputFrames.length = 0;
if (useNumericIndex == false)
{
frames = this._frameData.getFrameIndexesByName(frames);
}
this._frameData.getFrameIndexes(frames, useNumericIndex, this._outputFrames);
this._anims[name] = new Phaser.Animation(this.game, this.sprite, this._frameData, name, frames, frameRate, loop);
this._anims[name] = new Phaser.Animation(this.game, this.sprite, name, this._frameData, this._outputFrames, frameRate, loop);
this.currentAnim = this._anims[name];
this.currentFrame = this.currentAnim.currentFrame;
this.sprite.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
@@ -105,10 +138,12 @@ Phaser.AnimationManager.prototype = {
},
/**
* 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.
* Check whether the frames in the given array are valid and exist.
*
* @method validateFrames
* @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.
*/
validateFrames: function (frames, useNumericIndex) {
@@ -137,16 +172,17 @@ Phaser.AnimationManager.prototype = {
},
/**
* 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 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
* @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.
* @return {Phaser.Animation} A reference to playing Animation instance.
*/
play: function (name, frameRate, loop) {
frameRate = frameRate || null;
loop = loop || null;
if (this._anims[name])
{
if (this.currentAnim == this._anims[name])
@@ -166,8 +202,12 @@ Phaser.AnimationManager.prototype = {
},
/**
* Stop animation. If a name is given that specific animation is stopped, otherwise the current one is stopped.
* Current animation will be automatically set to the stopped one.
* 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
* @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)
*/
stop: function (name, resetFrame) {
@@ -192,8 +232,11 @@ Phaser.AnimationManager.prototype = {
},
/**
* Update animation and parent sprite's bounds.
* Returns true if a new frame has been set, otherwise false.
* 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
* @return {Boolean} True if a new animation frame has been set, otherwise false.
*/
update: function () {
@@ -213,8 +256,10 @@ Phaser.AnimationManager.prototype = {
},
/**
* Removes all related references
/**
* Destroys all references this AnimationManager contains. Sets the _anims to a new object and nulls the current animation.
*
* @method destroy
*/
destroy: function () {
@@ -230,6 +275,10 @@ Phaser.AnimationManager.prototype = {
Object.defineProperty(Phaser.AnimationManager.prototype, "frameData", {
/**
* @method frameData
* @return {Phaser.Animation.FrameData} Returns the FrameData of the current animation.
*/
get: function () {
return this._frameData;
}
@@ -238,6 +287,10 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frameData", {
Object.defineProperty(Phaser.AnimationManager.prototype, "frameTotal", {
/**
* @method frameTotal
* @return {Number} Returns the total number of frames in the loaded FrameData, or -1 if no FrameData is loaded.
*/
get: function () {
if (this._frameData)
@@ -254,6 +307,10 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frameTotal", {
Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
/**
* @method frame
* @return {Number} Returns the index of the current frame.
*/
get: function () {
if (this.currentFrame)
@@ -264,8 +321,8 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
},
/**
*
* @param value
* @method frame
* @param {Number} value Sets the current frame on the Sprite and updates the texture cache for display.
*/
set: function (value) {
@@ -283,6 +340,10 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
Object.defineProperty(Phaser.AnimationManager.prototype, "frameName", {
/**
* @method frameName
* @return {String} Returns the name of the current frame if it has one.
*/
get: function () {
if (this.currentFrame)
@@ -292,6 +353,10 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frameName", {
},
/**
* @method frameName
* @param {String} value Sets the current frame on the Sprite and updates the texture cache for display.
*/
set: function (value) {
if (this._frameData && this._frameData.getFrameByName(value))