Switch语句多次运行

问题描述 投票:0回答:1

我又回来了,一如既往的沮丧……我在游戏中走得更远,但是回头看游戏的一部分,我要检查一下你是否有药水,如果有的话,然后您尝试使用它,它会计算当前的生命值和该药水的增加量是否大于玩家的最大生命值。如果是,它会告诉您您不能喝,如果不是,则可以让您喝。然后,如果您没有任何药水而尝试使用它,那么它会告诉您您没有任何药水。

我遇到问题的部分是它运行了两次,我不确定为什么。这是Switch语句的摘要:

while (currentarea == "Hero's Turn") {
    if (hTurn == true) {
        switch (classType) {
            case "Warrior":
            case "Archer":
                switch (input) {
                    case "punch":
                    case "p":
                        Punch();
                        break;
                    case "kick":
                    case "k":
                        Kick();
                        break;
                    case "attack":
                    case "a":
                        Attack();
                        break;
                    case "health potion":
                    case "h":
                        HealthPotion();
                        break;
                    default:
                }
            case "Mage":
                switch (input) {
                    case "fire":
                    case "f":
                        FireMagic();
                        break;
                    case "wind":
                    case "w":
                        WindMagic();
                        break;
                    case "frost":
                    case "c":
                        IceMagic();
                        break;
                    case "health potion":
                    case "h":
                        HealthPotion();
                        break;
                    case "mana potion":
                    case "m":
                        ManaPotion();
                        break;
                    default:
                }
            default:
        }
        return;
    }
}

我试图将Archer移到自己的生产线上,这使其运行了3次。问题出在战士和弓箭手身上,法师只按预期推送了一次信息。

这是该功能的代码:

function HealthPotion() {
    if (healthPotion.AmountOf < 1) {
        $("<p>Sorry, you don't have any more of those.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
        checkH();
        return;
    }
    if (healthPotion.AmountOf != 0) {
        if(battleHealth + healthPotion.Health <= maxHealth) {
            $("<p>You regained some Health! You regained " + healthPotion.Health + " HP!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
            battleHealth += healthPotion.Health;
            document.getElementById("battleHealth").innerHTML = battleHealth;
            healthPotion.AmountOf--;
            document.getElementById("healthPotion.AmountOf").innerHTML = healthPotion.AmountOf;
            checkE();
            return;
        }
        else {
            $("<p>Your health isn't damaged enough for that! It would be a waste to use that right now.<br />Press enter to carry on.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
            checkH();
            return;
        }
    }
}

这是我构造药剂的方式:

var healthPotion = { //Recovers battle HP.
   Name: "Health Potion",
   Health: 20,
   AmountOf: 5,
   Value: 30,
}

这是循环到达的地方(敌人和英雄检查):

function checkE() {
    if (eHealth >= 1) {
        currentarea = "Enemy's Turn";
        document.getElementById("currentarea").innerHTML = currentarea;
        hTurn = false;
        eTurn = true;
        return;
    }
    else if (eHealth <= 0) {
        eHealth = 0;
        document.getElementById("eHealth").innerHTML = eHealth;
        currentarea = "Death";
        win++;
        document.getElementById("win").innerHTML = win;
        hTurn = false;
        eTurn = false;
        return;
    }
}
function checkH() {
    if (battleHealth >= 1) {
        currentarea = "Battle";
        document.getElementById("currentarea").innerHTML = currentarea;
        eTurn = false;
        hTurn = true;
        return;
    }
    else if (battleHealth <= 0) {
        battleHealth = 0;
        document.getElementById("battleHealth").innerHTML = battleHealth;
        currentarea = "Death";
        document.getElementById("currentarea").innerHTML = currentarea;
        lose++;
        document.getElementById("lose").innerHTML = lose;
        return;
    }
}

在这一点上,我感到很沮丧,因为在我的所有编码中,我都具有switch语句和嵌套函数,但这是我唯一看到此问题的领域。我的主要浏览器是Firefox,但我在Chrome和Edge上进行了测试,两者的结果相同。 IE不会玩我的游戏,主要是因为我尚未编写代码来进行游戏。

在发布到这里之前,我环顾了其他人的问题,没有发现与该特定问题相符的任何内容。

在每个浏览器的管理控制台中查看,没有引发任何错误,只是多行重复的语言。

screen shot of the double verbage, playing as a warrior

如果您继续玩耍并承受足以使用该药水的伤害,则会看到此:

screen shot of the double verbage, both using and saying cannot use

javascript switch-statement
1个回答
0
投票

您突破了内部开关,该中断对外部开关没有任何作用,因此当您拥有“战士”时,它会执行这种情况,并且由于在其级别上没有中断,因此转到“法师”中。

switch (classType) {
  case "Warrior":
  case "Archer":
    switch (input) {
      case "punch":
      case "p":
        Punch();
        break;
      case "kick":
      case "k":
        Kick();
        break;
      case "attack":
      case "a":
        Attack();
        break;
      case "health potion":
      case "h":
        HealthPotion();
        break;
      default:
    }
    break; // <-- YOU NEED THIS HERE
  case "Mage":
    switch (input) {
      case "fire":
      case "f":
        FireMagic();
        break;
      case "wind":
      case "w":
        WindMagic();
        break;
      case "frost":
      case "c":
        IceMagic();
        break;
      case "health potion":
      case "h":
        HealthPotion();
        break;
      case "mana potion":
      case "m":
        ManaPotion();
        break;
      default:
    }
    break; // <-- YOU NEED THIS HERE IF YOU DO SOMETHING IN DEFAULT
  default:
}
© www.soinside.com 2019 - 2024. All rights reserved.