2013-08-29 02:52:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-08-29 02:52:59 +00:00
|
|
|
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.RequestAnimationFrame
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
Phaser.RequestAnimationFrame = function(game) {
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - The currently running game.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.game = game;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-01 15:39:39 +00:00
|
|
|
* @property {boolean} _isSetTimeOut - Description.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this._isSetTimeOut = false;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-01 15:39:39 +00:00
|
|
|
* @property {boolean} isRunning - Description.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
var vendors = [
|
|
|
|
'ms',
|
|
|
|
'moz',
|
|
|
|
'webkit',
|
|
|
|
'o'
|
|
|
|
];
|
|
|
|
|
2013-10-03 00:21:08 +00:00
|
|
|
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; x++)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
|
|
|
|
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'];
|
|
|
|
}
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
/**
|
|
|
|
* The function called by the update
|
2013-10-01 12:54:29 +00:00
|
|
|
* @property _onLoop
|
2013-08-29 06:06:16 +00:00
|
|
|
* @private
|
2013-10-03 00:21:08 +00:00
|
|
|
*/
|
|
|
|
this._onLoop = null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.RequestAnimationFrame.prototype = {
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
/**
|
|
|
|
* Starts the requestAnimatioFrame running or setTimeout if unavailable in browser
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#start
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
start: function () {
|
|
|
|
|
|
|
|
this.isRunning = true;
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
if (!window.requestAnimationFrame)
|
|
|
|
{
|
|
|
|
this._isSetTimeOut = true;
|
2013-08-29 06:06:16 +00:00
|
|
|
|
|
|
|
this._onLoop = function () {
|
|
|
|
return _this.updateSetTimeout();
|
|
|
|
};
|
|
|
|
|
|
|
|
this._timeOutID = window.setTimeout(this._onLoop, 0);
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._isSetTimeOut = false;
|
2013-08-29 06:06:16 +00:00
|
|
|
|
2013-08-30 01:00:30 +00:00
|
|
|
this._onLoop = function (time) {
|
|
|
|
return _this.updateRAF(time);
|
2013-08-29 06:06:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.requestAnimationFrame(this._onLoop);
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The update method for the requestAnimationFrame
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#updateRAF
|
|
|
|
* @param {number} time - A timestamp, either from RAF or setTimeOut
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
updateRAF: function (time) {
|
|
|
|
|
|
|
|
this.game.update(time);
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
window.requestAnimationFrame(this._onLoop);
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* The update method for the setTimeout.
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#updateSetTimeout
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
updateSetTimeout: function () {
|
|
|
|
|
|
|
|
this.game.update(Date.now());
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
this._timeOutID = window.setTimeout(this._onLoop, this.game.time.timeToCall);
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* Stops the requestAnimationFrame from running.
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#stop
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
stop: function () {
|
|
|
|
|
|
|
|
if (this._isSetTimeOut)
|
|
|
|
{
|
|
|
|
clearTimeout(this._timeOutID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.cancelAnimationFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the browser using setTimeout?
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#isSetTimeOut
|
2013-10-01 15:39:39 +00:00
|
|
|
* @return {boolean}
|
2013-10-03 00:21:08 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
isSetTimeOut: function () {
|
|
|
|
return this._isSetTimeOut;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the browser using requestAnimationFrame?
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#isRAF
|
2013-10-01 15:39:39 +00:00
|
|
|
* @return {boolean}
|
2013-10-03 00:21:08 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
isRAF: function () {
|
|
|
|
return (this._isSetTimeOut === false);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|