samid737 Posted March 14, 2017 Share Posted March 14, 2017 Hi, I am new to Phaser(and Javascript) and I am working on my first HTML5 game. In my game I have the following: If player touches floor-->game-over-->wait for x seconds(keep updating)->show menu. I am looking for a way to implement the "wait for x seconds"/ timer. I have worked with Unity before and within Unity you can use coroutines to yield the execution of your function for a certain time: https://docs.unity3d.com/ScriptReference/WaitForSeconds.html By using coroutines, there was no need to use a timer within the update function. Is there a similar approach to this in Phaser using Javascript (implementing timers without using the update function)? Link to comment Share on other sites More sharing options...
mattstyles Posted March 14, 2017 Share Posted March 14, 2017 There is nothing in Phaser to do how you want as the mechanisms to make this pleasant in JS are relatively new and support amongst slightly older browsers is sketchy or non-existent. There are polyfills however, for both generators (which you can use to make coroutines) or async-await. You'd have to code this up yourself although a number of libraries exist out there for using coroutines or using async await to do as you want. You'd have to be wary of performance though as JS implementations of this sort of thing are slower than other methods to accomplish your goal (as, no doubt, they are in the Unity world as well, even non-web output). samid737 1 Link to comment Share on other sites More sharing options...
Tom Atom Posted March 14, 2017 Share Posted March 14, 2017 As already said, there are no coroutines in Phaser, but... you do not have to code it yourself. You can use looped / repeated / one time events to easily simulate it (http://phaser.io/examples/v2/category/time). You can chain these events to simulate more complex timers like this: this.game.time.events.add(5000, function () { console.log("Hello after 5 seconds"); this.game.time.events.add(1000, function () { console.log("Next hello after 1 second"); }, this); }, this); samid737 1 Link to comment Share on other sites More sharing options...
samid737 Posted March 15, 2017 Author Share Posted March 15, 2017 @mattstyles I see yes, seems like an interesting, but difficult task to implement by myself for now. Thanks for your input in any case! @Tom Atom I think this will work and it could be useful for more purposes, like blinking a sprite every 2 seconds for example..I will try it out thanks! Link to comment Share on other sites More sharing options...
mattstyles Posted March 15, 2017 Share Posted March 15, 2017 Without real support for async/await (which is in Chrome and Firefox nightlies, possibly stable?) transpilation or coroutine libraries actually work out to much the same. samid737 1 Link to comment Share on other sites More sharing options...
Recommended Posts