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 02 Answers
Reset to default 6Assuming you're using Phaser 3, these are the ways you can use timer.
- delayedCall method, which is shorter.
delayedCall(delay, callback, args, callbackScope)
So, you would do something like this.
this.time.delayedCall(2000, onEvent, null, this);
- 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
版权声明:本文标题:javascript - Phaser: How to use a simple timer from 0 to 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744706050a2620849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论