Jump to content

Timer vs update


spinnerbox
 Share

Recommended Posts

In my memory game there are two loop timers, one which runs on each 100 milliseconds and second on each 1000 milliseconds. We cannot forget also the update() function which comes along the Phaser.State class.

 

In the 100 mil timer I do stuff like replacing cards by position, and showing helper arrows.

On the 1000 mil timer I just increment the clock by one second.

All works well but the code got kind of too cluttered so I am afraid, I could break something at this point.

 

For best performance, is it good to use only one timer and no update() method or should I just move all in update() method and count the number of frames passed?

Link to comment
Share on other sites

Functions not in the update() function are generally better on performance. The update loops several hundred times per second. Even if you're not executing the function, you're still checking to see if you need to execute the function - which means several hundred operations per second. Granted it's probably not so drastic because the update function is optimized to not waste performance ( i assume). but still, I think it's a good strategy to avoid putting things in update that don't need to be there. If you can run the function on a timer or run it as part of a procedure, it's much better on performance. difficult to tell without seeing code though. If it's implemented in update() properly, the difference might be negligible (you would need to check with a js profiler). But a good rule of thumb is:

 

Does this operation need to run multiple times in the same second? If so, put it directly in update. Prime examples are collision detection and checking keys for camera/character movement. 

 

In your case, two timers does seem overly-complicated. I would just create a single timer to handle both operations. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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