Jump to content

Problem with very simple game loop


Recommended Posts

I'm just finishing off my game, and I've went back to the game loop to try and make it run at 60fps. I'm not using requestAnimationFrame in this example, but I'll look into that later. Basically it's printing out that I'm getting anything between 119 and 125 frames per second. I know I must be doing something really stupid, but I just can't see it.

 

     gameLoop: function() {     		if (!this.running) {			return;		}			 		if (!this.lastTime) {			this.lastTime = new Date().getTime();		}						this.currentTime = new Date().getTime();		this.elapsedTime = this.currentTime - this.lastTime;		this.lastTime = this.currentTime;				if (this.input) {			this.input.poll();		}						for (var i = 0; i < this.scenes.length; i++) {			this.scenes[i].updateScene(this.elapsedTime);		}					this.render(this.elapsedTime);	   	  this.frameCounter++;	  this.frameTimer += this.elapsedTime;	  	  if (this.frameTimer >= 1000) {          console.log("frame counter " + this.frameCounter);	  	  this.frameCounter = 0;	  	  this.frameTimer = 0;	  	  	   }	   	   this.timeToCall = Math.max(0, (16 - this.elapsedTime));		  	   window.setTimeout(this.f, this.timeToCall); },

 

Link to comment
Share on other sites

I've been thinking. Should I change it to this?

 

 

  this.timeToCall = 16 - Math.max(0, (16 - this.elapsedTime)); 

That makes it closer to 60 frames per second. Not sure if I'm just getting confused though.

 

Edit: No definitely not, the only reason this works is because it's minus number most of the time so it takes 0 away from 16.

Link to comment
Share on other sites

 gameLoop: function() {     		if (!this.running) {			return;		}			 		if (!this.lastTime) {			this.lastTime = new Date().getTime();		}						this.currentTime = new Date().getTime();		this.elapsedTime = this.currentTime - this.lastTime;		this.lastTime = this.currentTime;				if (this.input) {			this.input.poll();		}						for (var i = 0; i < this.scenes.length; i++) {			this.scenes[i].updateScene(this.elapsedTime);		}					    this.render(this.elapsedTime);	  	  this.frameCounter++;	  this.frameTimer += this.elapsedTime;	  	  if (this.frameTimer >= 1000) {	  	  this.frameCounter = 0;	  	  this.frameTimer = 0;	  	  	   }		  	   this.timePassedThisUpdate = new Date().getTime() - this.currentTime; 	   this.timeToCall =  Math.max(0, (16 - this.timePassedThisUpdate)); 	   window.setTimeout(this.f, this.timeToCall); },

 

 

I think I've figured the problem out. This seems to work better.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...