Made a GameLoop helper module to make game class cleaner

This commit is contained in:
Jeremiah Billmann
2013-07-09 21:23:48 -04:00
parent 7b7021bf09
commit a31865142c
4 changed files with 57 additions and 72 deletions
+33
View File
@@ -0,0 +1,33 @@
window.GameLoop = (function (window) {
var requestAnimFrame = (function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000/60); };
})(),
_currentTime = new Date().getTime(),
_accumulator = 0.0,
start = function (renderCallback, updateCallback) {
loop();
function loop () {
requestAnimFrame(loop);
var newTime = new Date().getTime(), frameTime = newTime - _currentTime;
if (frameTime > 250) {
frameTime = 250;
}
_currentTime = newTime;
_accumulator += frameTime;
while (_accumulator >= 15) {
updateCallback();
_accumulator -= 15;
}
renderCallback();
}
};
return {
start: start
};
}) (window);