Arlefreak Posted December 23, 2013 Share Posted December 23, 2013 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 More sharing options...
neato Posted December 23, 2013 Share Posted December 23, 2013 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. Arlefreak 1 Link to comment Share on other sites More sharing options...
Arlefreak Posted December 23, 2013 Author Share Posted December 23, 2013 This will make it so your reviveEnemy method will only execute once every 5th second. I'm assuming that's the functionality you wanted. This is exactly what I wanted and it works perfect thanks! Link to comment Share on other sites More sharing options...
Recommended Posts