Jump to content

Modifying the timing of events


Recommended Posts



I created a looped event in my create function, as shown below, where eventTime is a global variable set to 5 seconds.


game.time.events.loop(eventTime, generateEnemies, this);


I want my game to trigger the event faster after reaching a certain score. I tried reducing eventTime in the update function but it's not affecting the loop.


I can't place my looped event in update function, because it triggers every update call (i believe every 15ms) regardless of the value of eventTime.


Please provide inputs.



Link to comment
Share on other sites

This is javascript, the eventTime parameter you passed into game.time.events.loop method doesn't keep a pointer reference to the eventTime in parent scope. Therefore, doesn't matter how you change eventTime in update, the eventTime in your created loop would not change. Ever.


There are two approaches for your need:

- Classic way, a pretty famous pattern I suppose:Use a lastTime variable to record when was the last time your looped task is called, if Date.now() - lastTime is more than eventTime, you call the task. This means, instead of using the timer, you time your event yourself.

- Save a reference to your timer (var timer = game.time.events.loop(...) ). when you need to change the eventTime, cancel the loop (using game.time.events.remove(timer) ) and create a new one.


I think in the future, ES6 generator would yield a more elegant approach for this, as seen on Unity3D. :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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