为什么我的敌人在它死后会进行攻击?(Javascript)

时间:2017-07-28 04:03:41

标签: javascript

     function battle(){
    //User turn
    alert("It's your turn!");
    var user_move = prompt("Type 1 to use fireball, 2 to use lightning strike, or 3 to do earthquake");
    if(user_move == 1){
      var r = Math.round(Math.random()*(dmg.length-1));
      alert("You did "+dmg[r]+" damage to the enemy!");
      enemy_HP = enemy_HP - dmg[r];
    }
    else if(user_move == 2){
      var r = Math.round(Math.random()*(dmg.length-1));
      alert("You did "+dmg[r]+" damage to the enemy!");
      enemy_HP = enemy_HP - dmg[r];
    }
    else if(user_move == 3){
      var r = Math.round(Math.random()*(dmg.length-1));
      alert("You did "+dmg[r]+" damage to the enemy!");
      enemy_HP = enemy_HP - dmg[r];
    }
    alert("Your enemy has "+enemy_HP+"HP");
    //Enemy turn
    alert("Now its your enemys turn!");
    var a = Math.round(Math.random(enemy_attack.length-1));
    var r = Math.round(Math.random()*(dmg.length-1));
    alert("You enemy used "+enemy_attack[a]+"\nand did "+enemy_dmg[r]+" damage to you!");
    HP = HP - enemy_dmg[r];
    alert("You now have "+HP+"HP!");
    //If still living keep battling
    if(HP > 0 && enemy_HP > 0){
      battle();
    }
    else if(HP <= 0){
      alert("You lost D:");
    }
    else if(enemy_HP <= 0){
      alert("You win :D!");
    }
  }
  battle();

我正在尝试制作一个有趣的小js战斗游戏但有时当你杀死敌人时它会有一次最终攻击并且即使它已经死亡也可以杀死你。我该如何解决?

2 个答案:

答案 0 :(得分:1)

嗯,根据你的代码判断,你先做敌人的伤害,然后检查他是否有足够的生命。如果你检查敌人是否死亡的陈述,包括内部敌人的伤害。

答案 1 :(得分:0)

将敌人的攻击代码置于if语句

if (enemy_HP > 0){
   //enemy attack code
} else alert("You win :D!");