I'm developing a prototype game using framework Phaser with JavaScript. So, I'm trying to run a delayed function with a conditional var isPlaying == true in update() method, but it's not working. The function works, but not with the defined delayed time. It runs quickly and probably at FPS (deltaTime).
update(){
if(isPlaying){
this.time.addEvent({delay: 2500, callback: createItem, callbackScope: this, loop: true});
}
}
The update function is fired at FPS, so you're adding a new event each frame.
Each event that you add is with loop: true so it will call the callback function every 2500.
Depending on what you want to achieve, you may want to put the addEvent at the end of the create function instead of update and then unpause it only once in update.
var timer;
create() {
timer = this.time.addEvent({delay: 2500, callback: createItem, callbackScope: this, loop: true, paused: true});
}
update(){
if(isPlaying && timer.paused){
timer.paused = false;
}
}
You can find other note about Timers here