2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - RequestAnimationFrame
|
|
|
|
*
|
|
|
|
* Abstracts away the use of RAF or setTimeOut for the core game update loop. The callback can be re-mapped on the fly.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
module Phaser {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
export class RequestAnimationFrame {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param {Any} callback
|
|
|
|
* @return {RequestAnimationFrame} This object.
|
|
|
|
*/
|
2013-05-14 02:37:38 +00:00
|
|
|
constructor(game: Game, callback) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
this._game = game;
|
|
|
|
this.callback = callback;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var vendors = ['ms', 'moz', 'webkit', 'o'];
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; x++)
|
|
|
|
{
|
|
|
|
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
|
|
|
|
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'];
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.start();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* Local private reference to game.
|
|
|
|
*/
|
|
|
|
private _game: Game;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* The function to be called each frame. Will be called in the context of _game
|
|
|
|
* @property callback
|
|
|
|
* @type Any
|
2013-04-18 13:16:18 +00:00
|
|
|
**/
|
2013-05-14 02:37:38 +00:00
|
|
|
public callback;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property _timeOutID
|
|
|
|
* @type Any
|
|
|
|
* @private
|
|
|
|
**/
|
|
|
|
private _timeOutID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property _isSetTimeOut
|
|
|
|
* @type Boolean
|
|
|
|
* @private
|
|
|
|
**/
|
|
|
|
private _isSetTimeOut: bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @method usingSetTimeOut
|
|
|
|
* @return Boolean
|
|
|
|
**/
|
|
|
|
public isUsingSetTimeOut(): bool {
|
|
|
|
|
|
|
|
return this._isSetTimeOut;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @method usingRAF
|
|
|
|
* @return Boolean
|
|
|
|
**/
|
|
|
|
public isUsingRAF(): bool {
|
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
return this._isSetTimeOut === true;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property isRunning
|
|
|
|
* @type Boolean
|
|
|
|
**/
|
|
|
|
public isRunning: bool = false;
|
|
|
|
|
2013-08-05 02:43:20 +00:00
|
|
|
/**
|
|
|
|
* A reference to the RAF/setTimeout to avoid constant anonymous function creation
|
|
|
|
*/
|
|
|
|
public _onLoop;
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* Starts the requestAnimatioFrame running or setTimeout if unavailable in browser
|
2013-04-18 13:16:18 +00:00
|
|
|
* @method start
|
|
|
|
* @param {Any} [callback]
|
|
|
|
**/
|
|
|
|
public start(callback? = null) {
|
|
|
|
|
|
|
|
if (callback)
|
|
|
|
{
|
2013-05-14 02:37:38 +00:00
|
|
|
this.callback = callback;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!window.requestAnimationFrame)
|
|
|
|
{
|
|
|
|
this._isSetTimeOut = true;
|
2013-08-05 02:43:20 +00:00
|
|
|
this._onLoop = () => this.SetTimeoutUpdate();
|
|
|
|
this._timeOutID = window.setTimeout(this._onLoop, 0);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._isSetTimeOut = false;
|
2013-08-05 02:43:20 +00:00
|
|
|
this._onLoop = () => this.RAFUpdate(0);
|
|
|
|
window.requestAnimationFrame(this._onLoop);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.isRunning = true;
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* Stops the requestAnimationFrame from running
|
2013-04-18 13:16:18 +00:00
|
|
|
* @method stop
|
|
|
|
**/
|
|
|
|
public stop() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._isSetTimeOut)
|
|
|
|
{
|
|
|
|
clearTimeout(this._timeOutID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.cancelAnimationFrame;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.isRunning = false;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
/**
|
|
|
|
* The update method for the requestAnimationFrame
|
|
|
|
* @method RAFUpdate
|
|
|
|
**/
|
|
|
|
public RAFUpdate(time: number) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
this._game.time.update(time);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
if (this.callback)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-14 02:37:38 +00:00
|
|
|
this.callback.call(this._game);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-05 02:43:20 +00:00
|
|
|
this._onLoop = (time) => this.RAFUpdate(time);
|
|
|
|
|
|
|
|
window.requestAnimationFrame(this._onLoop);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-14 02:37:38 +00:00
|
|
|
* The update method for the setTimeout
|
2013-04-18 13:16:18 +00:00
|
|
|
* @method SetTimeoutUpdate
|
|
|
|
**/
|
|
|
|
public SetTimeoutUpdate() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-14 02:37:38 +00:00
|
|
|
this._game.time.update(Date.now());
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-05 02:43:20 +00:00
|
|
|
this._onLoop = () => this.SetTimeoutUpdate();
|
|
|
|
|
|
|
|
this._timeOutID = window.setTimeout(this._onLoop, 16);
|
2013-05-14 02:37:38 +00:00
|
|
|
|
|
|
|
if (this.callback)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-14 02:37:38 +00:00
|
|
|
this.callback.call(this._game);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|