2013-08-29 02:52:59 +00:00
|
|
|
/**
|
|
|
|
* Phaser - RequestAnimationFrame
|
|
|
|
*
|
|
|
|
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
|
|
|
|
*/
|
|
|
|
Phaser.RequestAnimationFrame = function(game) {
|
|
|
|
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
this._isSetTimeOut = false;
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
var vendors = [
|
|
|
|
'ms',
|
|
|
|
'moz',
|
|
|
|
'webkit',
|
|
|
|
'o'
|
|
|
|
];
|
|
|
|
|
|
|
|
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; x++) {
|
|
|
|
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
|
|
|
|
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'];
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.RequestAnimationFrame.prototype = {
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
/**
|
|
|
|
* The function called by the update
|
|
|
|
* @private
|
|
|
|
**/
|
|
|
|
_onLoop: null,
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
/**
|
|
|
|
* Starts the requestAnimatioFrame running or setTimeout if unavailable in browser
|
|
|
|
* @method start
|
|
|
|
**/
|
|
|
|
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
|
|
|
|
* @method RAFUpdate
|
|
|
|
**/
|
|
|
|
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
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The update method for the setTimeout
|
|
|
|
* @method SetTimeoutUpdate
|
|
|
|
**/
|
|
|
|
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
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the requestAnimationFrame from running
|
|
|
|
* @method stop
|
|
|
|
**/
|
|
|
|
stop: function () {
|
|
|
|
|
|
|
|
if (this._isSetTimeOut)
|
|
|
|
{
|
|
|
|
clearTimeout(this._timeOutID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.cancelAnimationFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the browser using setTimeout?
|
|
|
|
* @method isSetTimeOut
|
|
|
|
* @return bool
|
|
|
|
**/
|
|
|
|
isSetTimeOut: function () {
|
|
|
|
return this._isSetTimeOut;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the browser using requestAnimationFrame?
|
|
|
|
* @method isRAF
|
|
|
|
* @return bool
|
|
|
|
**/
|
|
|
|
isRAF: function () {
|
|
|
|
return (this._isSetTimeOut === false);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|