admin管理员组

文章数量:1391956

I am using the following code (which is a simplified snippet in order to make it more readable):

var player;
var box_tnt;

function create (){
    this.physics.add.collider(player, box_tnt, hitTnt, null, this);
}

//the function hitTnt stop the game because the player died
function hitTnt (player, boxes){
    this.physics.pause();
    player.setTint(0xff0000);
    player.anims.play('default');
    gameOver = true;
    textGameOver.setText('GAME OVER');
}
  • Actual portment:

When the player hit the bomb: player die; end of the game

  • Desired portment:

When the player hits the bomb: the bomb waits 3 secondes and then explodes ! If the player is too close he dies. But I struggle a lot to use a timer even after reading a lot of examples in the forum. I am a newbie concerning Phaser so I didn't succeed to it so far.

Any help would be appreciated, thank you in advance!

I am using the following code (which is a simplified snippet in order to make it more readable):

var player;
var box_tnt;

function create (){
    this.physics.add.collider(player, box_tnt, hitTnt, null, this);
}

//the function hitTnt stop the game because the player died
function hitTnt (player, boxes){
    this.physics.pause();
    player.setTint(0xff0000);
    player.anims.play('default');
    gameOver = true;
    textGameOver.setText('GAME OVER');
}
  • Actual portment:

When the player hit the bomb: player die; end of the game

  • Desired portment:

When the player hits the bomb: the bomb waits 3 secondes and then explodes ! If the player is too close he dies. But I struggle a lot to use a timer even after reading a lot of examples in the forum. I am a newbie concerning Phaser so I didn't succeed to it so far.

Any help would be appreciated, thank you in advance!

Share Improve this question edited Feb 16, 2019 at 15:37 Ced asked Feb 11, 2019 at 12:19 CedCed 1,5696 gold badges24 silver badges36 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Assuming you're using Phaser 3, these are the ways you can use timer.

  1. delayedCall method, which is shorter.

delayedCall(delay, callback, args, callbackScope)

So, you would do something like this.

this.time.delayedCall(2000, onEvent, null, this);

  1. addEvent method

addEvent(config) Docs for config

this.time.addEvent({ delay: 2000, callback: onEvent, callbackScope: this });

Find these method in the docs here.

Find examples for timer events here.


Other thing you can do is if you've any tween that plays for 3 second (If you're tweening bomb or animating it for 3 seconds). You can attach onComplete callback to it. So after Tween gets over that onComplete callback would get executed.

If you want to delay the executino of hitTnt() logic by 3 seconds, you can wrap it in a setTimeout() call like that:

function hitTnt(player, boxes) {
    setTimeout(() => {
        this.physics.pause();
        player.setTint(0xff0000);
        player.anims.play('default');
        gameOver = true;
        textGameOver.setText('GAME OVER');
    }, 3000);
}

本文标签: javascriptPhaser How to use a simple timer from 0 to 3Stack Overflow