leandro Posted May 14, 2013 Share Posted May 14, 2013 helloI'm working on a real time game and I am using the requestAnimationFrame API for the animations. The problem is that when I switch browser tab, stops the animation. I understand that is a feature, but it is possible disable it? Quote Link to comment Share on other sites More sharing options...
Chris Posted May 14, 2013 Share Posted May 14, 2013 This is the whole purpose for using requestAnimationFrame, so... I fear there is no way to prevent that behavior A suggestion:use RAF only for your rendering output!You can base your game logics on a javascript timer - that won't stop doing game calculations when you switch away from the tab. Quote Link to comment Share on other sites More sharing options...
rich Posted May 14, 2013 Share Posted May 14, 2013 What Chris said Use raf for rendering and a setTimeout for game logic. It's not ideal, but it's easy to check the tab visibility state, so you could actually use raf for everything until the tab loses visibility and then start a setTimeout going. Alternatively you could pause the game and keep track of the time it paused, then when it resumes (i.e. they come back to the tab) you take a count of the time and make-up for that lost time in your game logic (i.e. advance that number of steps). Quote Link to comment Share on other sites More sharing options...
Hugeen Posted May 17, 2013 Share Posted May 17, 2013 RAF is stopped when you switch tab but using the setTimeout is not a good solution either. Because as mentioned in this link: http://stackoverflow.com/questions/6563859/minimum-setinterval-settimeout-delay-on-background-tabs/6563900#6563900 Background tabs have setTimeout and setInterval clamped to 1000ms to improve performance You should pause your game to fix this issue :-( Quote Link to comment Share on other sites More sharing options...
leandro Posted May 17, 2013 Author Share Posted May 17, 2013 Thank you very much, this helped me a lot. It seems that I will not be able to use timeouts / intervals. I'll try to use WebWorkers see you soon Quote Link to comment Share on other sites More sharing options...
Chris Posted May 17, 2013 Share Posted May 17, 2013 Why are you not able to use timeouts/intervals? They are available on every browser, whilst WebWorkers arent.Also, afaik, its not possible to replicate a timeout/interval with a webworker... Quote Link to comment Share on other sites More sharing options...
leandro Posted May 17, 2013 Author Share Posted May 17, 2013 by the mechanics of the game I can not pause. My game is like Tetris, between two people and in real time. If someone pauses the game, the other player would be complicated. Also there is some playing time probably would pause or would also be very different for the two players. I have no choice to have something running in the background to make constant checks with the server and many other things Quote Link to comment Share on other sites More sharing options...
Chris Posted May 17, 2013 Share Posted May 17, 2013 Uhm, that would be a setTimeout() or setInterval() ... Quote Link to comment Share on other sites More sharing options...
Ezelia Posted May 17, 2013 Share Posted May 17, 2013 @leandro I encountered a similar issue with an online game and my solution was : 1 - keep gamestate on the server (it's an online game right ?) 2 - when the window lose focus stop the rendering but continue to update gamestate from the server 3 - when the window get focus again clear the canvas and redraw the whole scene from the current server side gamestate. the solution will maybe need some modifications in your rendering process but it works just fine Hugeen 1 Quote Link to comment Share on other sites More sharing options...
martensms Posted May 19, 2013 Share Posted May 19, 2013 As suggested, split your whole game into two different loops. A render loop that only makes the draw calls and an update loop that executes the game logic and is triggered by a setInterval() finally. You also should know that intervals are executed less precisely when the tab is inactive (to save resources), so you might want to calculate an interpolation load indicator that allows you to implement correct game logic, but has not to be something like precise physics simulations. Quote Link to comment Share on other sites More sharing options...
narukaioh Posted September 6, 2013 Share Posted September 6, 2013 I'm trying to use setInterval() to make a NPC that move randomly.. but I'm not succeeding..I'm using an engine (canvas engine) for this. (forgive me if my english is very bad.. e-e"..) anyway.. can someone help me? D: 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.