Jump to content

How to use Phaser.Timer and Phaser.TimerEvent to replace setInterval, setTimeout, clearInterval and clearTimeout


Recommended Posts


To simulate a laser blaster heating and cooling I used setInterval for the cooling and setTimeout to delay the cooling if the weapon overheat (firelimit reached).

While the weapon is cooling, if it fires again the cooling is aborted (clearInterval, only if the weapon has not overheated).

Although it is working correctly while in game, the cooling or the delay is not paused when the game window loose focus, that's because of setTimeout and setInterval.

Here is a demo Blaster heating and cooling,  some heating and cooling feedbacks are logged in the browser console and a heating meter in the top right camera corner.

The colldown function using setInterval.

The overheat handler using setTimeout.

The cooldown aborter using clearInterval.

The github repo.

How should I use Phaser.Timer and Phaser.TimerEvent to implement the same behaviour and then get the cooling timer and cooling delay paused when the game window looses focus.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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