Sinova/Collisions

Collisions not working when browser tab changes

Closed this issue · 2 comments

When I change the browser tab, no collisions are detected, any ideas how to fix it?

gif:
ezgif com-gif-maker (1)

It probably has to do with the fact that browser slow down setInterval and requestAnimationFrame and others for inactive tabs.
I recommend to pause the game when the tab is inactive.

document.addEventListener(`visibilitychange`, function () {
    if (document.hidden) {
        pause();
    }
});

In case someone else have the same problem:

if I doa simple if/else the result is the same, I had to add a delay of 100ms before resuming the game again to avoid this problem.

thanks @GrosSacASac

const delay = ms => new Promise(res => setTimeout(res, ms));

document.addEventListener('visibilitychange', async () => {
  if (document.hidden) {
    setPause(true);
  } else {
    await delay(100)
    setPause(false)
  }
});