2016-11-25 00:34:37 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
|
|
|
|
*
|
|
|
|
* @class Phaser.RequestAnimationFrame
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
* @param {boolean} [forceSetTimeOut=false] - Tell Phaser to use setTimeOut even if raf is available.
|
|
|
|
*/
|
|
|
|
function RequestAnimationFrame (game)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - The currently running game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} isRunning - true if RequestAnimationFrame is running, otherwise false.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.isRunning = false;
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
this.tick = 0;
|
|
|
|
|
2016-11-25 00:34:37 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} isSetTimeOut - True if the browser is using setTimeout instead of rAf.
|
|
|
|
*/
|
|
|
|
this.isSetTimeOut = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} timeOutID - The callback setTimeout or rAf callback ID used when calling cancel.
|
|
|
|
*/
|
|
|
|
this.timeOutID = null;
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2016-11-25 00:34:37 +00:00
|
|
|
// timestamp = DOMHighResTimeStamp
|
2016-11-25 01:37:14 +00:00
|
|
|
var step = function (timestamp)
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
2016-11-25 01:37:14 +00:00
|
|
|
_this.tick = timestamp;
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
_this.timeOutID = window.requestAnimationFrame(step);
|
|
|
|
|
|
|
|
_this.game.update(timestamp);
|
2016-11-25 00:34:37 +00:00
|
|
|
};
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
var stepTimeout = function ()
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
2016-11-25 01:37:14 +00:00
|
|
|
_this.tick = Date.now();
|
|
|
|
|
|
|
|
// _this.game.update(_this.tick);
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
// _this.timeOutID = window.setTimeout(stepTimeout, _this.game.time.timeToCall);
|
2016-11-25 00:34:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the requestAnimationFrame running or setTimeout if unavailable in browser
|
|
|
|
* @method Phaser.RequestAnimationFrame#start
|
|
|
|
*/
|
|
|
|
this.start = function ()
|
|
|
|
{
|
|
|
|
this.isRunning = true;
|
|
|
|
|
|
|
|
if (this.game.config.forceSetTimeOut)
|
|
|
|
{
|
|
|
|
this.isSetTimeOut = true;
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
this.timeOutID = window.setTimeout(stepTimeout, 0);
|
2016-11-25 00:34:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.isSetTimeOut = false;
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
this.timeOutID = window.requestAnimationFrame(step);
|
2016-11-25 00:34:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the requestAnimationFrame from running.
|
|
|
|
* @method Phaser.RequestAnimationFrame#stop
|
|
|
|
*/
|
|
|
|
this.stop = function ()
|
|
|
|
{
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
if (this.isSetTimeOut)
|
|
|
|
{
|
|
|
|
clearTimeout(this.timeOutID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.cancelAnimationFrame(this.timeOutID);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.destroy = function ()
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
|
|
|
|
this.game = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestAnimationFrame.prototype.constructor = RequestAnimationFrame;
|
|
|
|
|
|
|
|
module.exports = RequestAnimationFrame;
|