Jump to content

Phaser wait for certain time using coroutines


samid737
 Share

Recommended Posts

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

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).

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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