Jump to content

Timers not Pausing when Window Loses Focus


megan
 Share

Recommended Posts

I have a timer that creates stars every 800 ms.

timer = game.time.events.loop(800, spawn_stars, true);

It works great, but when the window loses focus the timers keep going. When focus is restored, all the stars that were spawned while the window was unfocused all show up at the same time (so there could be 100+ stars showing up at once, which isn't ideal for this game).

 

This happens whether 

game.stage.disableVisibilityChange

is set to true or false. The timers stop properly when I use the game.paused function.

 

Is this a bug, or is there something I'm not doing correctly?
 

Link to comment
Share on other sites

I tried switching the "true" to "this" in the loop, and it still didn't work for me.

However, I've been able to narrow down the problem. It seems to work fine in any situation, except for when 

game.stage.disableVisibilityChange = true

and then I switch to a new tab in my browser. Maybe it's because the game is still "playing", but it can't draw anything to the screen until it's visible again?

Link to comment
Share on other sites

  • 2 weeks later...

I'm having this exact same issue. I have a pause function built and it works perfectly well except that a couple of timers will resume when the window is resized or loses focus. I've also found the issue on my mobile phone if I pause the game, return to the home screen, then back to the app, the timers have now resumed.

 

I haven't tried checking it's behavior with game.stage.disableVisibilitityChange = true or dug into the Phaser code itself to see where the problem lies but I have a feeling there is an odd check happening somewhere.

 

In the mean time I will probably revert the code to not using a timer object but instead checking against an internally tracked time elapsed value which I have complete control over.

Link to comment
Share on other sites

  • 1 month later...

You should use;

var nextSpawnStar = 0;spawnStarLoop: function () { // spawn star every 800 ms        nextSpawnStar += this.game.time.elapsed;        if(nextSpawnStar >= Phaser.Timer.SECOND * 0.8) {            nextSpawnStar -= Phaser.Timer.SECOND * 0.8;            this.spawnStar();        }    },
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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