将计时器集成到 fall damage 的 Phaser.js 中。

huangapple go评论59阅读模式
英文:

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 &amp;&amp;
      this.player.body.velocity.y &gt;= 300
    ) {
      //console.log(&quot;falling&quot;);
      this.falling = true;
      this.clock.start();
    }

    if (this.player.body.onFloor() == true &amp;&amp; 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 &amp;&amp;
      this.player.body.velocity.y &gt;= 300
    ) {
      //console.log(&quot;falling&quot;);
      this.falling = true;
      this.startTick = this.tick;
      console.log(this.tick)
      return;
    }

    if (this.player.body.onFloor() == true &amp;&amp; this.falling == true) {
      this.fallDmgAmt = (this.tick-this.startTick)/100
      this.health = this.health - this.fallDmgAmt;
      if (this.health &lt;= 0) {
        this.health = 0;
        this.scene.launch(&quot;death&quot;);
        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 &amp;&amp;
  this.player.body.velocity.y &gt;= 300  &amp;&amp; 
  this.falling == false
) {
  this.falling = true;
  this.fallStartTime = (new Date()).getTime(); // milliseconds
}

if (this.player.body.onFloor() == true &amp;&amp; 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;
}

huangapple
  • 本文由 发表于 2023年2月24日 04:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定