Jump to content

Timers overlapping events after game.state switch


Recommended Posts

Hi there, 


Phaser is super great, but sometimes I get stuck for days in one little functionality I can't find good explanations in the examples.


So this is the thing. Im trying to follow this flowchart where timers get instantiated using: 

this.timer_AreYouThere = this.game.time.create(false);


This happens in my Tutorial.js game state, a timer will play a sound that says: "Are you still there..?" every 15 seconds until you click something and the game asks you do something else, and then again the timer gets destroyed and reinstated again, but now it will play a different sound every 15 seconds, until the user makes the action is asked for.


Also after 1 min of inactivity from the user the game goes back to MainMenu game state.


The problem: Everything works great, but once I wait for the 1 minute timer to load the MainMenu state, and the user works its way back to the Tutorial state, and user waits for 15 seconds for the "are you still there" sound, and the user taps Key.ONE to interrupt that sound and continue the tutorial... everything starts overlapping, sounds from previous timers get triggered and the becomes a mess. I don't understand why this events keep happening even though I killed them when I switched the states before.


Here the code



Im really stuck trying to understand the Timers. BTW I have the sounds in a global scope array (Boot.js)


Your help is really appreciated.






Link to comment
Share on other sites

Okay I found what was the problem. I was having my sounds in a global scop Kente.sounds <-- array and in my Tutorial state I was adding a function callback event to the onStop signal. 


Kente.sounds[1].onStop.add(function(){ //First lets learn

this.timer_AreYouThere = this.game.time.events.loop(15000, this.playAreYourThere, this);
}, this);
So even if I switched the state of the game, killed the timer (in that moment). The sound was still holding a onStop callback, and then I was reading another onStop callback.. hence.. having 2 timers rolling in the background. Or at least I think that is what was happening. 
what I did was to removal Events of the onStop signal before switching state.
Kente.sounds.forEach(function(sound) { //Clean functions added to the onStop Phaser.signal
this.game.state.start('MainMenu', true,false);
Do anyone knows a good best practice to manage timers in a game ? or Game steps in a tutorial to keep track of progress and whats next to ask to the user.     
Link to comment
Share on other sites


  • Recently Browsing   0 members

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