It seems scene.stop() and scene.sleep doesn’t work when the scene to stop contains setTimeOut or setInterval functions : indeed these functions still continue to trigger in background while the new scene is loaded.
Can someone can confirm to me if this is normal and if there is a “phaser solution”?
See these examples. Phaser has absolutely no way to tell what intervals or timeouts you’ve added, so they won’t be cleared unless you do it manually. Using Phaser’s Clock component will clear up that problem, along with any other problems that could arise from handling timing outside the game’s update loop.
Thanks Telinc1 and Iskhomutov, that’s what I thought. Thanks for the info on Phaser’s Clock component that I didn’t know, I understand this to be the phaser’s way to encapsulate setTimeOut function.
Actually, the Clock doesn’t encapsulate setTimeout. It keeps track of time based on the game loop’s time delta, which is the time difference between two frames. This means that, when using the Clock, your events will run at the game’s speed, so they’ll take twice as long if your game is, for some reason, running twice as slow as usual. This is generally what you want because it doesn’t give players an unfair disadvantage if they’re getting a lower framerate.
As a very unrelated side note, the game loop can sometimes run on setTimeout in older browsers, but that doesn’t matter in this case.
Thank you very much for this explanation Telinc1, I just looked at Clock.js and TimeStep.js and I think I begin to understand, All of this is indeed based on javaScript’s Date.now() and requestAnimationFrame() functions.