Skip to content

Instantly share code, notes, and snippets.

@loktar00
Created January 30, 2013 18:23
Show Gist options
  • Select an option

  • Save loktar00/4675433 to your computer and use it in GitHub Desktop.

Select an option

Save loktar00/4675433 to your computer and use it in GitHub Desktop.
game loop
/**
* jGame.update()
*
* Main update loop for the game, updates all objects, and calls the renderer.
**/
update : function(){
var curTime = (new Date()).getTime(),
update = this.update;
this.deltaTime = curTime - this.lastTime;
this.lastTime = curTime;
this.accTime += this.deltaTime;
// Limit the delta queing
if(this.accTime > 60){
this.accTime = 0;
}
while (this.accTime > this.timeStep)
{
this.accTime -= this.timeStep;
var entities = this.entities,
entLen = this.entities.length;
while(entLen--){
var object = entities[entLen];
if(object !== undefined){
if(object.live){
object.update(this.timeStep /100);
}else{
this.removeEntity(object);
}
}
}
}
this.renderer.redraw();
this.frameRateLabel.text = Math.round(1000/this.deltaTime) + " fps";
this.currentFrameRate = Math.round(1000/this.deltaTime);
var self = this;
requestAnimFrame( function(){self.update()} );
//this.intervalId = setTimeout(function(){self.update()}, this.frameRate);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment