Jump to content

Running Timer when browser tab is out of focus


claycr4ne
 Share

Recommended Posts

Hello all!

I'm still quite new to Phaser and this is my first post in here. So, I've been developing a big educational game in a group for about three months now and I've run to this little problem. As our game is an educational game, one of our intents is to use it also in exams. This means that our game has to have a timebar which works in a way that the player can't cheat. At this moment I've got the timebar up and running but there is this one problem in which the player can switch browser's tabs or put the browser window down causing Timer to pause. So I'm wondering is there any way to prevent Timer from pausing like this?

 

Although I've got lots of answers to many programmatical issues from this forum, this one is still an issue to me. In one discussion on this forum I learned how setting stage.disableVisibilityChange to true makes the game not pause when visibility changes. I added this line of code in our game's Boot-state but this doesn't clear the case. The Timer still pauses when this value is set to true - enabling players to cheat. The only thing I've noticed after playing with this value is that our game's music doesn't pause anymore when switching tabs.

 

If somebody of you knows the answer to this problem of mine, I'd really appreciate the help. Thanks!

- Marko

Link to comment
Share on other sites

I got this solved myself by checking time values with game.onPause and game.onResume. If somebody is struggling with this same problem, here's how I did it in my Game-state:

create: function() {
   ...
   this.game.onPause.add(this.onGamePause, this);

   this.game.onResume.add(this.onGameResume, this);
   ...
}

onGamePause: function() {
   // Saving duration (time left) of TimerEvent when pausing the game.
   this.timeOnPause = timer.duration;
  // Stopping Timer and clearing its events.
   timer.stop(true);
}

onGameResume: function() {
   // Adding a new TimerEvent with new delay time.
   timer.add(this.timeOnPause - this.game.time.pauseDuration, this.timeIsUp, this);
  // Starting Timer once again.
   timer.start();
}

Now it doesn't matter if the player switches tabs or puts the window down. Everytime the game resumes it has a new delay time.  B)
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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