mirror of
https://github.com/photonstorm/phaser
synced 2024-11-10 07:04:31 +00:00
Finish document for Time, Tween, Animation, AnimationLoader, Frame.
This commit is contained in:
parent
e88c48641d
commit
00ef200914
5 changed files with 251 additions and 50 deletions
107
Phaser/Time.ts
107
Phaser/Time.ts
|
@ -10,6 +10,12 @@ module Phaser {
|
|||
|
||||
export class Time {
|
||||
|
||||
/**
|
||||
* Time constructor
|
||||
* Create a new <code>Time</code>.
|
||||
*
|
||||
* @param game {Phaser.Game} Current game instance.
|
||||
*/
|
||||
constructor(game: Game) {
|
||||
|
||||
this._started = Date.now();
|
||||
|
@ -18,32 +24,47 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Local private reference to game.
|
||||
*/
|
||||
private _game: Game;
|
||||
/**
|
||||
* Time when this object created.
|
||||
* @param {number}
|
||||
*/
|
||||
private _started: number;
|
||||
|
||||
|
||||
/**
|
||||
* Time scale factor.
|
||||
* Set it to 0.5 for slow motion, to 2.0 makes game twice faster.
|
||||
* @type {number}
|
||||
*/
|
||||
public timeScale: number = 1.0;
|
||||
/**
|
||||
* Elapsed since last frame.
|
||||
* @type {number}
|
||||
*/
|
||||
public elapsed: number = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property time
|
||||
* @type Number
|
||||
*/
|
||||
* Game time counter.
|
||||
* @property time
|
||||
* @type {number}
|
||||
*/
|
||||
public time: number = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property now
|
||||
* @type Number
|
||||
*/
|
||||
* Time of current frame.
|
||||
* @property now
|
||||
* @type {number}
|
||||
*/
|
||||
public now: number = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property delta
|
||||
* @type Number
|
||||
*/
|
||||
* Elapsed time since last frame.
|
||||
* @property delta
|
||||
* @type {number}
|
||||
*/
|
||||
public delta: number = 0;
|
||||
|
||||
/**
|
||||
|
@ -57,19 +78,47 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Frames per second.
|
||||
* @type {number}
|
||||
*/
|
||||
public fps: number = 0;
|
||||
/**
|
||||
* Minimal fps.
|
||||
* @type {number}
|
||||
*/
|
||||
public fpsMin: number = 1000;
|
||||
/**
|
||||
* Maximal fps.
|
||||
* @type {number}
|
||||
*/
|
||||
public fpsMax: number = 0;
|
||||
/**
|
||||
* Mininal duration between 2 frames.
|
||||
* @type {number}
|
||||
*/
|
||||
public msMin: number = 1000;
|
||||
/**
|
||||
* Maximal duration between 2 frames.
|
||||
* @type {number}
|
||||
*/
|
||||
public msMax: number = 0;
|
||||
/**
|
||||
* How many frames in last second.
|
||||
* @type {number}
|
||||
*/
|
||||
public frames: number = 0;
|
||||
|
||||
/**
|
||||
* Time of last second.
|
||||
* @type {number}
|
||||
*/
|
||||
private _timeLastSecond: number = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @method update
|
||||
*/
|
||||
* Update clock and calc fps.
|
||||
* @method update
|
||||
*/
|
||||
public update() {
|
||||
|
||||
// Can we use performance.now() ?
|
||||
|
@ -102,11 +151,11 @@ module Phaser {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @method elapsedSince
|
||||
* @param {Number} since
|
||||
* @return {Number}
|
||||
*/
|
||||
* How long has passed since given time.
|
||||
* @method elapsedSince
|
||||
* @param {number} since The time you want to measure.
|
||||
* @return {number} Duration between given time and now.
|
||||
*/
|
||||
public elapsedSince(since: number): number {
|
||||
|
||||
return this.now - since;
|
||||
|
@ -114,11 +163,11 @@ module Phaser {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @method elapsedSecondsSince
|
||||
* @param {Number} since
|
||||
* @return {Number}
|
||||
*/
|
||||
* How long has passed since give time (in seconds).
|
||||
* @method elapsedSecondsSince
|
||||
* @param {number} since The time you want to measure (in seconds).
|
||||
* @return {number} Duration between given time and now (in seconds).
|
||||
*/
|
||||
public elapsedSecondsSince(since: number): number {
|
||||
|
||||
return (this.now - since) * 0.001;
|
||||
|
@ -126,9 +175,9 @@ module Phaser {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @method reset
|
||||
*/
|
||||
* Set the start time to now.
|
||||
* @method reset
|
||||
*/
|
||||
public reset() {
|
||||
|
||||
this._started = this.now;
|
||||
|
|
|
@ -21,6 +21,13 @@ module Phaser {
|
|||
|
||||
export class Tween {
|
||||
|
||||
/**
|
||||
* Tween constructor
|
||||
* Create a new <code>Tween</code>.
|
||||
*
|
||||
* @param object {object} Target object will be affected by this tween.
|
||||
* @param game {Phaser.Game} Current game instance.
|
||||
*/
|
||||
constructor(object, game:Phaser.Game) {
|
||||
|
||||
this._object = object;
|
||||
|
@ -37,24 +44,75 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Local private reference to game.
|
||||
*/
|
||||
private _game: Phaser.Game;
|
||||
/**
|
||||
* Manager of this tween.
|
||||
* @type {Phaser.TweenManager}
|
||||
*/
|
||||
private _manager: Phaser.TweenManager;
|
||||
/**
|
||||
* Reference to the target object.
|
||||
* @type {object}
|
||||
*/
|
||||
private _object = null;
|
||||
private _pausedTime: number = 0;
|
||||
|
||||
/**
|
||||
* Start values container.
|
||||
* @type {object}
|
||||
*/
|
||||
private _valuesStart = {};
|
||||
/**
|
||||
* End values container.
|
||||
* @type {object}
|
||||
*/
|
||||
private _valuesEnd = {};
|
||||
/**
|
||||
* How long this tween will perform.
|
||||
* @type {number}
|
||||
*/
|
||||
private _duration = 1000;
|
||||
private _delayTime = 0;
|
||||
private _startTime = null;
|
||||
/**
|
||||
* Easing function which actually updating this tween.
|
||||
* @type {function}
|
||||
*/
|
||||
private _easingFunction;
|
||||
private _interpolationFunction;
|
||||
/**
|
||||
* Contains chained tweens.
|
||||
* @type {Tweens[]}
|
||||
*/
|
||||
private _chainedTweens = [];
|
||||
|
||||
/**
|
||||
* Signal to be dispatched when this tween start.
|
||||
* @type {Phaser.Signal}
|
||||
*/
|
||||
public onStart: Phaser.Signal;
|
||||
/**
|
||||
* Signal to be dispatched when this tween updating.
|
||||
* @type {Phaser.Signal}
|
||||
*/
|
||||
public onUpdate: Phaser.Signal;
|
||||
/**
|
||||
* Signal to be dispatched when this tween completed.
|
||||
* @type {Phaser.Signal}
|
||||
*/
|
||||
public onComplete: Phaser.Signal;
|
||||
|
||||
/**
|
||||
* Config the tween result.
|
||||
* @param properties {object} Propertis you want to tween.
|
||||
* @param duration {number} Optional, duration of this tween.
|
||||
* @param ease {any} Easing function.
|
||||
* @param autoStart {boolean} Whether this tween will start automatically or not.
|
||||
* @return {Tween} Itself.
|
||||
*/
|
||||
public to(properties, duration?: number = 1000, ease?: any = null, autoStart?: bool = false) {
|
||||
|
||||
this._duration = duration;
|
||||
|
@ -78,6 +136,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Start to tween.
|
||||
*/
|
||||
public start() {
|
||||
|
||||
if (this._game === null || this._object === null)
|
||||
|
@ -120,6 +181,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop tweening.
|
||||
*/
|
||||
public stop() {
|
||||
|
||||
if (this._manager !== null)
|
||||
|
@ -164,6 +228,11 @@ module Phaser {
|
|||
return this._interpolationFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add another chained tween, which will start automatically when the one before it completes.
|
||||
* @param tween {Phaser.Tween} Tween object you want to chain with this.
|
||||
* @return {Phaser.Tween} Itselfe.
|
||||
*/
|
||||
public chain(tween:Phaser.Tween) {
|
||||
|
||||
this._chainedTweens.push(tween);
|
||||
|
@ -172,8 +241,16 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug value?
|
||||
*/
|
||||
public debugValue;
|
||||
|
||||
/**
|
||||
* Update tweening.
|
||||
* @param time {number} Current time from game clock.
|
||||
* @return {boolean} Return false if this completed and no need to update, otherwise return true.
|
||||
*/
|
||||
public update(time) {
|
||||
|
||||
if (this._game.paused == true)
|
||||
|
|
|
@ -14,8 +14,12 @@ module Phaser {
|
|||
* Animation constructor
|
||||
* Create a new <code>Animation</code>.
|
||||
*
|
||||
* @param width Width of the world bound.
|
||||
* @param height Height of the world bound.
|
||||
* @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 {boolean} Whether or not the animation is looped or just plays once.
|
||||
*/
|
||||
constructor(game: Game, parent: Sprite, frameData: FrameData, name: string, frames, delay: number, looped: bool) {
|
||||
|
||||
|
@ -36,21 +40,72 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Local private reference to game.
|
||||
*/
|
||||
private _game: Game;
|
||||
/**
|
||||
* Local private reference to its owner sprite.
|
||||
* @type {Sprite}
|
||||
*/
|
||||
private _parent: Sprite;
|
||||
/**
|
||||
* Animation frame container.
|
||||
* @type {number[]}
|
||||
*/
|
||||
private _frames: number[];
|
||||
/**
|
||||
* Frame data of this animation.(parsed from sprite sheet)
|
||||
* @type {FrameData}
|
||||
*/
|
||||
private _frameData: FrameData;
|
||||
/**
|
||||
* Index of current frame.
|
||||
* @type {number}
|
||||
*/
|
||||
private _frameIndex: number;
|
||||
|
||||
/**
|
||||
* Time when switched to last frame (in ms).
|
||||
* @type number
|
||||
*/
|
||||
private _timeLastFrame: number;
|
||||
/**
|
||||
* Time when this will switch to next frame (in ms).
|
||||
* @type number
|
||||
*/
|
||||
private _timeNextFrame: number;
|
||||
|
||||
/**
|
||||
* Name of this animation.
|
||||
* @type {string}
|
||||
*/
|
||||
public name: string;
|
||||
/**
|
||||
* Currently played frame instance.
|
||||
* @type {Frame}
|
||||
*/
|
||||
public currentFrame: Frame;
|
||||
|
||||
/**
|
||||
* Whether or not this animation finished playing.
|
||||
* @type {boolean}
|
||||
*/
|
||||
public isFinished: bool;
|
||||
/**
|
||||
* Whethor or not this animation is currently playing.
|
||||
* @type {boolean}
|
||||
*/
|
||||
public isPlaying: bool;
|
||||
/**
|
||||
* Whether or not the animation is looped.
|
||||
* @type {boolean}
|
||||
*/
|
||||
public looped: bool;
|
||||
/**
|
||||
* Time between frames in ms.
|
||||
* @type {number}
|
||||
*/
|
||||
public delay: number;
|
||||
|
||||
public get frameTotal(): number {
|
||||
|
@ -74,6 +129,11 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Play this animation.
|
||||
* @param frameRate {number} FrameRate you want to specify instead of using default.
|
||||
* @param loop {boolean} Whether or not the animation is looped or just plays once.
|
||||
*/
|
||||
public play(frameRate?: number = null, loop?: bool) {
|
||||
|
||||
if (frameRate !== null)
|
||||
|
@ -97,6 +157,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Play this animation from the first frame.
|
||||
*/
|
||||
public restart() {
|
||||
|
||||
this.isPlaying = true;
|
||||
|
@ -110,6 +173,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop playing animation and set it finished.
|
||||
*/
|
||||
public stop() {
|
||||
|
||||
this.isPlaying = false;
|
||||
|
@ -117,6 +183,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Update animation frames.
|
||||
*/
|
||||
public update(): bool {
|
||||
|
||||
if (this.isPlaying == true && this._game.time.now >= this._timeNextFrame)
|
||||
|
@ -150,6 +219,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up animation memory.
|
||||
*/
|
||||
public destroy() {
|
||||
|
||||
this._game = null;
|
||||
|
@ -161,6 +233,9 @@ module Phaser {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Animation complete callback method.
|
||||
*/
|
||||
private onComplete() {
|
||||
|
||||
this.isPlaying = false;
|
||||
|
|
|
@ -12,11 +12,11 @@ module Phaser {
|
|||
|
||||
/**
|
||||
* Parse a sprite sheet from asset data.
|
||||
* @param key Asset key for the sprite sheet data.
|
||||
* @param frameWidth Width of animation frame.
|
||||
* @param frameHeight Height of animation frame.
|
||||
* @param frameMax Number of animation frames.
|
||||
* @return {FrameData=} Generated FrameData object.
|
||||
* @param key {string} Asset key for the sprite sheet data.
|
||||
* @param frameWidth {number} Width of animation frame.
|
||||
* @param frameHeight {number} Height of animation frame.
|
||||
* @param frameMax {number} Number of animation frames.
|
||||
* @return {FrameData} Generated FrameData object.
|
||||
*/
|
||||
public static parseSpriteSheet(game: Game, key: string, frameWidth: number, frameHeight: number, frameMax: number): FrameData {
|
||||
|
||||
|
@ -73,8 +73,8 @@ module Phaser {
|
|||
|
||||
/**
|
||||
* Parse frame datas from json.
|
||||
* @param json Json data you want to parse.
|
||||
* @return {FrameData=} Generated FrameData object.
|
||||
* @param json {object} Json data you want to parse.
|
||||
* @return {FrameData} Generated FrameData object.
|
||||
*/
|
||||
public static parseJSONData(game: Game, json): FrameData {
|
||||
|
||||
|
|
|
@ -14,11 +14,11 @@ module Phaser {
|
|||
* Frame constructor
|
||||
* Create a new <code>Frame</code> with specific position, size and name.
|
||||
*
|
||||
* @param x X position within the image to cut from.
|
||||
* @param y Y position within the image to cut from.
|
||||
* @param width Width of the frame.
|
||||
* @param height Height of the frame.
|
||||
* @param name Name of this frame.
|
||||
* @param x {number} X position within the image to cut from.
|
||||
* @param y {number} Y position within the image to cut from.
|
||||
* @param width {number} Width of the frame.
|
||||
* @param height {number} Height of the frame.
|
||||
* @param name {string} Name of this frame.
|
||||
*/
|
||||
constructor(x: number, y: number, width: number, height: number, name: string) {
|
||||
|
||||
|
@ -122,13 +122,13 @@ module Phaser {
|
|||
|
||||
/**
|
||||
* Set trim of the frame.
|
||||
* @param trimmed Whether this frame trimmed or not.
|
||||
* @param actualWidth Actual width of this frame.
|
||||
* @param actualHeight Actual height of this frame.
|
||||
* @param destX Destiny x position.
|
||||
* @param destY Destiny y position.
|
||||
* @param destWidth Destiny draw width.
|
||||
* @param destHeight Destiny draw height.
|
||||
* @param trimmed {boolean} Whether this frame trimmed or not.
|
||||
* @param actualWidth {number} Actual width of this frame.
|
||||
* @param actualHeight {number} Actual height of this frame.
|
||||
* @param destX {number} Destiny x position.
|
||||
* @param destY {number} Destiny y position.
|
||||
* @param destWidth {number} Destiny draw width.
|
||||
* @param destHeight {number} Destiny draw height.
|
||||
*/
|
||||
public setTrim(trimmed: bool, actualWidth, actualHeight, destX, destY, destWidth, destHeight, ) {
|
||||
|
||||
|
|
Loading…
Reference in a new issue