warpdesign Posted December 9, 2014 Share Posted December 9, 2014 Hi, 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. Quote Link to comment Share on other sites More sharing options...
alex_h Posted December 9, 2014 Share Posted December 9, 2014 Yep, that is a common approach. It's certainly the way I set up my stuff and also the way I have seen many other people working too. Quote Link to comment Share on other sites More sharing options...
samlancashire Posted December 15, 2014 Share Posted December 15, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.