英文:
Implementing A timer into fall damage phaser.js
问题
以下是您的代码的中文翻译部分:
// 既然我试图在我的游戏中实现掉落伤害,这是我的代码目前的状态,
this.fallDmgAmt = 1;
if (
this.player.body.onFloor() == false &&
this.player.body.velocity.y >= 300
) {
//console.log("正在下落");
this.falling = true;
this.clock.start();
}
if (this.player.body.onFloor() == true && this.falling == true) {
this.health = this.health - this.fallDmgAmt;
this.changeHealth(this.health);
this.falling = false;
return;
}
我想要为此添加一个计时器,以便可以跟踪下落的时间并应用伤害。我尝试过使用rexrainbow时钟插件
,但也不起作用。
更新的代码:
// 掉落伤害
this.fallDmgAmt = 1;
if (
this.player.body.onFloor() == false &&
this.player.body.velocity.y >= 300
) {
//console.log("正在下落");
this.falling = true;
this.startTick = this.tick;
console.log(this.tick)
return;
}
if (this.player.body.onFloor() == true && this falling == true) {
this.fallDmgAmt = (this.tick - this.startTick) / 100;
this.health = this.health - this.fallDmgAmt;
if (this.health <= 0) {
this.health = 0;
this.scene.launch("死亡");
this.health = 10;
this.changeHealth(this.health);
this.scene.pause();
}
this.changeHealth(this.health);
this.falling = false;
return;
}
希望这对您有帮助。如果您需要进一步的解释或帮助,请随时提出。
英文:
So, I'm trying To implement Fall damage into my game, and This is my code so far,
this.fallDmgAmt = 1;
if (
this.player.body.onFloor() == false &&
this.player.body.velocity.y >= 300
) {
//console.log("falling");
this.falling = true;
this.clock.start();
}
if (this.player.body.onFloor() == true && this.falling == true) {
this.health = this.health - this.fallDmgAmt;
this.changeHealth(this.health);
this.falling = false;
return;
}
I want to add A timer to this, so i can track the amount of time fell and then apply damage. How can i do this? I tried the rexrainbow clock plugin
but that also doesn't work.
code update
//fall damage
this.fallDmgAmt = 1;
if (
this.player.body.onFloor() == false &&
this.player.body.velocity.y >= 300
) {
//console.log("falling");
this.falling = true;
this.startTick = this.tick;
console.log(this.tick)
return;
}
if (this.player.body.onFloor() == true && this.falling == true) {
this.fallDmgAmt = (this.tick-this.startTick)/100
this.health = this.health - this.fallDmgAmt;
if (this.health <= 0) {
this.health = 0;
this.scene.launch("death");
this.health = 10;
this.changeHealth(this.health);
this.scene.pause();
}
this.changeHealth(this.health);
this.falling = false;
return;
}
答案1
得分: 1
以下是翻译好的部分:
"一个更简单和更好的解决方案是在玩家开始掉落时获取当前时间,然后计算掉落的时间差。无需插件。
以下是已根据这个想法调整的代码:
this.fallDmgAmt = 1;
if (
this.player.body.onFloor() == false &&
this.player.body.velocity.y >= 300 &&
this.falling == false
) {
this.falling = true;
this.fallStartTime = (new Date()).getTime(); // 毫秒
}
if (this.player.body.onFloor() == true && this.falling == true) {
let fallDurationInSeconds = ((new Date()).getTime() - this.fallStartTime) / 1000;
this.health = this.health - (this.fallDmgAmt * fallDurationInSeconds);
this.changeHealth(this.health);
this.falling = false;
return;
}
希望对你有所帮助。"
英文:
Well a easy an better solution would be just to get the current time, on fall start and calculate the difference on fall. No plugin needed.
Here your code adapted for that idea:
<!-- languages-all: lang-js -->
this.fallDmgAmt = 1;
if (
this.player.body.onFloor() == false &&
this.player.body.velocity.y >= 300 &&
this.falling == false
) {
this.falling = true;
this.fallStartTime = (new Date()).getTime(); // milliseconds
}
if (this.player.body.onFloor() == true && this.falling == true) {
let fallDurationInSeconds = ((new Date()).getTime() - this.fallStartTime) / 1000;
this.health = this.health - (this.fallDmgAmt * fallDurationInSeconds );
this.changeHealth(this.health);
this.falling = false;
return;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论