javascript - Phaser: How to use a simple timer from 0 to 3 - Stack Overflow

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

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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744706051a4589062.html

相关推荐

  • javascript - Phaser: How to use a simple timer from 0 to 3 - Stack Overflow

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

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信