Jump to content

Put game logic into same or separate loop than renderer?


Recommended Posts



I'm working on a simple 2D platform game. Right now, I have two loops:


 - the renderer loop that's using requestAnimationFrame

 - the game logic that handles objects move, collisions, etc.. into a separate loop (that's now using setTimeout)


I'm wondering if this is the proper way to do it. I did it this way so that if the computer can't animate my app with 60fps, game is still responsive.

Link to comment
Share on other sites

Yes, separate is the usual approach. I usually do something like:

function update(mod) {   //do logic!}		function render() {    requestAnimationFrame(render);    if (eng.loadedImages == eng.imageCount) { //if all images have are loaded...        //draw stuff!    }}		function tick() {    update((Date.now() - lastTick) / 1000);    lastTick = Date.now();}		var lastTick = Date.now();setInterval(tick, 16.67); //60fpsrender();

Also some quick reading: http://stackoverflow.com/questions/729921/settimeout-or-setinterval

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.

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.


  • Recently Browsing   0 members

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