Lakshya dubey
Lakshya dubey

Reputation: 311

How do I loop the function callback with a random delay in phaser 3?

So my Phaser 3 game requires to call a function spawn with random delay each time between 3s to 6s. Here is what I tried :

Enemies = this.time.addEvent({ 
delay:  Phaser.Math.Between(3000,6000),  
loop: true, 
callback: spawn, 
callbackScope: this });

But this code does not works. The delay is chosen at random only once and then that value is used throughout.

Upvotes: 1

Views: 1663

Answers (1)

Kokodoko
Kokodoko

Reputation: 28158

You can see in your own link that there is a oneShot timer. You can use that inside the spawn function.

Now a new one-shot timer with a random delay will be created when spawn() is called. The timer then calls spawn() again...

function spawn() {
     let delay = Phaser.Math.Between(3000,6000)
     var timer = scene.time.delayedCall(delay, spawn, args, scope); 
}

spawn()

Upvotes: 1

Related Questions