Finish document for AnimationManager, CameraManager, SoundManager and TweenManager class.

This commit is contained in:
Sean
2013-05-03 19:22:04 +08:00
committed by Richard Davey
parent 98693cb418
commit 9da64e6baf
4 changed files with 182 additions and 1 deletions
+55
View File
@@ -16,6 +16,13 @@ module Phaser {
export class AnimationManager {
/**
* AnimationManager constructor
*
* Create a new <code>AnimationManager</code>.
*
* @param parent Owner sprite of this manager.
*/
constructor(game: Game, parent: Sprite) {
this._game = game;
@@ -24,16 +31,43 @@ module Phaser {
}
/**
* Local private reference to game.
*/
private _game: Game;
/**
* Local private reference to its owner sprite.
*/
private _parent: Sprite;
/**
* Animation key-value container.
*/
private _anims: {};
/**
* Index of current frame.
* @type {number}
*/
private _frameIndex: number;
/**
* Data contains animation frames.
* @type {FrameData}
*/
private _frameData: FrameData = null;
/**
* Keeps track of the current animation being played.
*/
public currentAnim: Animation;
/**
* Keeps track of the current frame of animation.
*/
public currentFrame: Frame = null;
/**
* Load animation frame data.
* @param frameData Data to be loaded.
*/
public loadFrameData(frameData: FrameData) {
this._frameData = frameData;
@@ -42,6 +76,14 @@ module Phaser {
}
/**
* Add a new animation.
* @param name What this animation should be called (e.g. "run").
* @param frames An array of numbers/strings indicating what frames to play in what order (e.g. [1, 2, 3] or ['run0', 'run1', run2]).
* @param frameRate The speed in frames per second that the animation should play at (e.g. 60 fps).
* @param loop Whether or not the animation is looped or just plays once.
* @param useNumericIndex Use number indexes instead of string indexes?
*/
public add(name: string, frames: any[] = null, frameRate: number = 60, loop: bool = false, useNumericIndex: bool = true) {
if (this._frameData == null)
@@ -98,6 +140,12 @@ module Phaser {
}
/**
* Play animation with specific name.
* @param name The string name of the animation you want to play.
* @param frameRate FrameRate you want to specify instead of using default.
* @param loop Whether or not the animation is looped or just plays once.
*/
public play(name: string, frameRate?: number = null, loop?: bool) {
if (this._anims[name])
@@ -118,6 +166,10 @@ module Phaser {
}
/**
* Stop animation by name.
* Current animation will be automatically set to the stopped one.
*/
public stop(name: string) {
if (this._anims[name])
@@ -128,6 +180,9 @@ module Phaser {
}
/**
* Update animation and parent sprite's bounds.
*/
public update() {
if (this.currentAnim && this.currentAnim.update() == true)
+50
View File
@@ -15,6 +15,15 @@ module Phaser {
export class CameraManager {
/**
* CameraManager constructor
* This will create a new <code>Camera</code> with position and size.
*
* @param x X Position of the created camera.
* @param y y Position of the created camera.
* @param width Width of the created camera.
* @param height Height of the created camera.
*/
constructor(game: Game, x: number, y: number, width: number, height: number) {
this._game = game;
@@ -25,24 +34,56 @@ module Phaser {
}
/**
* Local private reference to Game.
*/
private _game: Game;
/**
* Local container for storing camera.
*/
private _cameras: Camera[];
/**
* Local helper stores index of next created camera.
*/
private _cameraInstance: number = 0;
/**
* Currently used camera.
*/
public current: Camera;
/**
* Get all the cameras.
*
* @returns {array} An array contains all the cameras.
*/
public getAll(): Camera[] {
return this._cameras;
}
/**
* Update cameras.
*/
public update() {
this._cameras.forEach((camera) => camera.update());
}
/**
* Render cameras.
*/
public render() {
this._cameras.forEach((camera) => camera.render());
}
/**
* Create a new camera with specific position and size.
*
* @param x X position of the new camera.
* @param y Y position of the new camera.
* @param width Width of the new camera.
* @param height Height of the new camera.
* @returns {Camera=} The newly created camera object.
*/
public addCamera(x: number, y: number, width: number, height: number): Camera {
var newCam: Camera = new Camera(this._game, this._cameraInstance, x, y, width, height);
@@ -55,6 +96,12 @@ module Phaser {
}
/**
* Remove a new camera with its id.
*
* @param id ID of the camera you want to remove.
* @returns {boolean} True if successfully removed the camera, otherwise return false.
*/
public removeCamera(id: number): bool {
for (var c = 0; c < this._cameras.length; c++)
@@ -76,6 +123,9 @@ module Phaser {
}
/**
* Clean up memory.
*/
public destroy() {
this._cameras.length = 0;
+37
View File
@@ -11,6 +11,11 @@ module Phaser {
export class SoundManager {
/**
* SoundManager constructor
*
* Create a new <code>SoundManager</code>.
*/
constructor(game: Game) {
this._game = game;
@@ -36,18 +41,37 @@ module Phaser {
}
/**
* Local private reference to game.
*/
private _game: Game;
/**
* Reference to AudioContext instance.
*/
private _context = null;
/**
* Gain node created from audio context.
*/
private _gainNode;
/**
* Volume of sounds.
* @type {number}
*/
private _volume: number;
/**
* Mute sounds.
*/
public mute() {
this._gainNode.gain.value = 0;
}
/**
* Enable sounds.
*/
public unmute() {
this._gainNode.gain.value = this._volume;
@@ -65,6 +89,12 @@ module Phaser {
return this._volume;
}
/**
* Decode a sound with its assets key.
* @param key Assets key of the sound to be decoded.
* @param callback This will be invoked when finished decoding.
* @param sound Optional, its bufer will be set to decoded data.
*/
public decode(key: string, callback = null, sound?: Sound = null) {
var soundData = this._game.cache.getSound(key);
@@ -90,6 +120,13 @@ module Phaser {
}
/**
* Play a sound with its assets key.
* @param key Assets key of the sound you want to play.
* @param volume Optional, volume of the sound you want to play.
* @param loop Optional, loop when it finished playing? (Default to false)
* @return {Sound=} The playing sound object.
*/
public play(key: string, volume?: number = 1, loop?: bool = false): Sound {
if (this._context === null)
+40 -1
View File
@@ -7,7 +7,7 @@
* The Game has a single instance of the TweenManager through which all Tween objects are created and updated.
* Tweens are hooked into the game clock and pause system, adjusting based on the game state.
* TweenManager is based heavily on tween.js by sole (http://soledadpenades.com).
* I converted it to TypeScript, swapped the callbacks for signals and patched a few issues with regard
* I converted it to TypeScript, swapped the callbacks for signals and patched a few issues with regard
* to properties and completion errors. Please see https://github.com/sole/tween.js for a full list of contributors.
*/
@@ -15,6 +15,10 @@ module Phaser {
export class TweenManager {
/**
* TweenManager constructor
* @param game A reference to the current Game.
*/
constructor(game: Phaser.Game) {
this._game = game;
@@ -22,27 +26,52 @@ module Phaser {
}
/**
* Local private reference to Game
*/
private _game: Phaser.Game;
/**
* Local private array which is the container of all tween objects.
*/
private _tweens: Phaser.Tween[];
/**
* Get all the tween objects in an array.
* @return {array=} Array with all tween objects.
*/
public getAll() {
return this._tweens;
}
/**
* Remove all tween objects.
*/
public removeAll() {
this._tweens.length = 0;
}
/**
* Create a tween object for a specific object.
*
* @param object Object you wish the tween will affect.
* @return {Phaser.Tween=} The newly created tween object.
*/
public create(object): Phaser.Tween {
return new Phaser.Tween(object, this._game);
}
/**
* Add an exist tween object to the manager.
*
* @param tween The tween object you want to add.
* @return {Phaser.Tween} The tween object you added to the manager.
*/
public add(tween: Phaser.Tween) {
tween.parent = this._game;
@@ -53,6 +82,11 @@ module Phaser {
}
/**
* Remove a tween from this manager.
*
* @param tween The tween object you want to remove.
*/
public remove(tween: Phaser.Tween) {
var i = this._tweens.indexOf(tween);
@@ -64,6 +98,11 @@ module Phaser {
}
/**
* Update all the tween objects you added to this manager.
*
* @return {boolean} Return false if there's no tween to update, otherwise return true.
*/
public update() {
if (this._tweens.length === 0)