btm2424
btm2424

Reputation: 631

How to decrease addEvent delay in Phaser?

I'm using Phaser.io

I'm making a simple game. I want my 'enemies' to fall from the top of the screen:

    const enemies = this.physics.add.group();

    function enemyGen(){
      const xCoord = Math.random()*gameState.w;
      enemies.create(xCoord, 10, 'enemy');
    }

    const enemyGenLoop = this.time.addEvent({
      callback: enemyGen,
      delay: ......,
      callbackScope: this,
      loop: true
    })

I would like to decrease the delay when a player reach a certain score (which is stored in gameState.score).

I was thinking about having a variable stored in gameState.delay and then updating it from update(). The problem is that even if the gameState.delay is changed the addEvent still uses the previous value of gameState.delay.

Upvotes: 2

Views: 322

Answers (1)

Manuel Abascal
Manuel Abascal

Reputation: 6312

You can store the addEvent delay into a variable & set it to another value afterwards like so:

  • First, store the addEvent delay into a variable.
delayTimer = this.time.addEvent({
    delay: 2000,
    callback: ()=>{
        // calls a function here
    },
    loop: true
})
  • Second, set the delay option to another value.
delayTimer.delay = 1000;

Upvotes: 1

Related Questions