Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Do once a function inside update


Arlefreak
 Share

Recommended Posts

Is there a implemented way to do a function inside the update function only once ? 

 

 

So I have this at the time: 

update: function(){    if((Math.floor(this.time.totalElapsedSeconds()) % 5) == 0)        this.reviveEnemy();},

the problem is that the update function runs a lot of time inside one second so I revive a lot of enemy's, I have resolve it with a bool variable, but it doesn't seem as a good fix, so is there a implemented function or a better way handling the timer ? there aren't any timer examples, so I don't really know if there is a better way of doing this. Any help would be appreciated 

Link to comment
Share on other sites

You can try something like the once method from Underscore.js: http://underscorejs.org/#once

 

But it sounds like an easier solution would be:

lastRevive = 0;update: function(){    seconds = Math.floor(this.time.totalElapsedSeconds());    if(lastRevive != seconds && seconds % 5 == 0){        this.reviveEnemy();        lastRevive = seconds;    }}

This will make it so your reviveEnemy method will only execute once every 5th second. I'm assuming that's the functionality you wanted.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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