Yehuda Katz Posted September 29, 2017 Share Posted September 29, 2017 Hello, I know Phaser has built in timer. I assume it's designed to support game pause. Could anyone share idea, how game pause should be implemented? Lets assume I used only Phaser's timer class. Is there any way to stop them all and later resume them all? Of course I can do it manually for each timer, but what if later I will add some more or I miss some? It's better to have some kind of global pause for all timers =) Thanks in advance Link to comment Share on other sites More sharing options...
samme Posted September 29, 2017 Share Posted September 29, 2017 Phaser's timers pause and resume automatically when the game pauses or resumes. You can also do it yourself by calling game.time.gamePaused() and game.time.gameResumed(). Yehuda Katz 1 Link to comment Share on other sites More sharing options...
Yehuda Katz Posted September 30, 2017 Author Share Posted September 30, 2017 That's awesome @samme, I knew that there should be something like that. BTW what about audio playback, will it stop too? BTW where did you get those methods? I search official API but cannot find it https://phaser.io/docs/2.6.2/Phaser.Time.html Did I search in wrong place? Link to comment Share on other sites More sharing options...
samme Posted September 30, 2017 Share Posted September 30, 2017 Those methods are marked private, so they don't appear in the docs. Audio will also pause during game pause. Link to comment Share on other sites More sharing options...
Yehuda Katz Posted October 1, 2017 Author Share Posted October 1, 2017 Thanks @samme but as far as I know, the correct syntax for marking property/method as private is prefixing it with "_". What is the best way to stop game.time.totalElapsedSeconds() without calling gamePaused()? I need that to stop counting time while game prepares new challenge for user. p.s. I also found your another answer regarding game.time.events.add VERY helpful as I needed to have native setTimeout equivalent method =) Link to comment Share on other sites More sharing options...
samme Posted October 1, 2017 Share Posted October 1, 2017 You can check game.time.events.seconds instead and use game.time.events.pause() and game.time.events.resume(). Yehuda Katz 1 Link to comment Share on other sites More sharing options...
Yehuda Katz Posted October 2, 2017 Author Share Posted October 2, 2017 Awesome, @samme that's exactly what I was looking for! Thanks again! Link to comment Share on other sites More sharing options...
Yehuda Katz Posted October 3, 2017 Author Share Posted October 3, 2017 @samme I had to go back to original timer because game.time.events has no option to reset counter. this.game_timer = this.game.time.create(false); this.game_timer.loop(60000, function() {}); this.game_timer.start(); this.game_timer.pause(); I had to add that dummy loop event to be able to calculate time passed (this.game_timer.seconds). I thinks this is not right way to do it BTW have you any idea why the same code will not work if I replace first line with this.game_timer = new Phaser.Timer(this.game, false); Thanks in advance for your time Link to comment Share on other sites More sharing options...
samme Posted October 3, 2017 Share Posted October 3, 2017 It's fine to create your own timer if you want to do your own counting. But I don't think you need the empty loop event. As long as you do timer.start() it should start counting seconds. Yehuda Katz 1 Link to comment Share on other sites More sharing options...
samid737 Posted October 3, 2017 Share Posted October 3, 2017 There is a timer.stop(), but im not sure if it will reset the timer... Link to comment Share on other sites More sharing options...
samme Posted October 3, 2017 Share Posted October 3, 2017 stop followed by start will reset the timer. Try phaser-debug-timer if you do a lot of this. samid737 1 Link to comment Share on other sites More sharing options...
samid737 Posted October 4, 2017 Share Posted October 4, 2017 Nice @samme didn't know about that one, why are you not bundling all the debug modules , arcade, timer, advancedtiming , tweens, etc..? Link to comment Share on other sites More sharing options...
Yehuda Katz Posted October 4, 2017 Author Share Posted October 4, 2017 @samme thanks, I tried without empty loop and it still works fine. I do not use native game.time.events because it stops all other timers (game.time.events.add). However, if I create timer like that it does not count anything: this.game_timer = new Phaser.Timer(this.game, false); As for reset @samid737 - it does not work on my end... after first call stop(), start() the timer start counting in reverse and giving negative values for seconds property (also seconds never get reset). With custom timer I simply destroy old and create new one to reset it OR I have to add my own loop and count time by my own Link to comment Share on other sites More sharing options...
samme Posted October 4, 2017 Share Posted October 4, 2017 (edited) 7 hours ago, Yehuda Katz said: this.game_timer = new Phaser.Timer(this.game, false); It won't work this way because the timer never gets updated. You need to use this.game_timer = this.time.create(false); Edited October 4, 2017 by samme Use `time.create` not `time.add` Link to comment Share on other sites More sharing options...
samme Posted October 5, 2017 Share Posted October 5, 2017 Yehuda Katz 1 Link to comment Share on other sites More sharing options...
Yehuda Katz Posted October 5, 2017 Author Share Posted October 5, 2017 thanks @samme, did you intentionally miss game property and use this.time instead of this.game.time? Link to comment Share on other sites More sharing options...
samme Posted October 5, 2017 Share Posted October 5, 2017 They should be identical, so you can use either. Yehuda Katz 1 Link to comment Share on other sites More sharing options...
Yehuda Katz Posted October 6, 2017 Author Share Posted October 6, 2017 Thanks, no more questions =) I just need time to get used with Phaser's standards Link to comment Share on other sites More sharing options...
Recommended Posts