如何在我的 if 语句中添加多个命令? JavaScript

How can I add multiple commands in my if statement? JavaScript

我觉得这个实例真的很简单,我可能想多了,但是有没有办法在我的 if 语句中添加多个语句?我正在寻找类似的东西,如果我选择在我的游戏中走向某个方向,你会得到 "You decided to visit the store. Do you want to Explore?" 的文本并且有两个选项:

-是的,然后在输入框中输入 "Yes" 然后它会给出结果 "after exploring, you decided to buy something"

-不,你决定离开。

一个多小时以来,我一直在努力解决这个问题,当我接近时,即使您不在那个位置,仍然可以访问该命令。例如。我游戏中的位置是,“你在公园。如果在输入框中随机输入,"Yes" 命令将 return 为, "after exploring, you decided to buy something"

我想要这样,如果您在该特定位置,则只能在该位置访问该代码,而不能在其他任何地方访问。

因此,如果您在公园作为位置并随机输入 "yes",则不会发生任何事情,因为您不在商店的位置。

这是我的代码:

...

选择一个方向

<input id = "input" type = "text" placeholder = "Type 'Help1' for actions"/><button onClick = "button()">Confirm Action</button>
<p id = "message"></p>

<script>

  function button() {
    var textInput;
    var newInput = input.value;
    if (newInput == "North") {

      textInput = "you went to the Store. Do you explore?"; // <-- I'd like a way to put in "Yes" as a response which would print out a text saying "You explored the store and bought some food" //

      document.getElementById("message").innerHTML = textInput;

    }


    if (newInput == "North Again") {
      textInput = "you went to the Forest Home";
      document.getElementById("message").innerHTML = textInput;
    }

    if (newInput == "Help1") {
      textInput = "[Page 1 of 2. Type 'Help2' for page 2] Commands you can use: <ul><li>South</li><li>North</li><li>East</li><li>West</li><li>North Again</li><li>South again</li><li>West Again</li><li>East Again</li>";
      document.getElementById("message").innerHTML = textInput;
    }

    if (newInput == "Help2") {
      textInput = "[Page 2 of 2] Commands you can use: <li>North One More</li><li>East Once More</li><li>West Once More</li><li>South Once More</li>";
      document.getElementById("message").innerHTML = textInput;
    }
  }

</script>

</div>

<div id = "stat-box">
  <h2 align = "center">STATS</h2>
</div>

...

看看状态机结构。您应该在游戏中存储用户的当前状态,并在每次按下按钮时检查它。

状态机设计模式将检查您的玩家当前所在的位置,并据此采取行动。

    var state = 'asked_for_direction';
    if (state == 'asked_for_direction' && newInput == "North") {

      textInput = "you went to the Store. Do you explore? (Yes/No)";

      document.getElementById("message").innerHTML = textInput;
      state = 'north';
    }

    if (state == 'north' && newInput == "Yes") {
      textInput = "You explored the store and bought some food";

      document.getElementById("message").innerHTML = textInput;
      state = 'north_explored';
    }

    if (state == 'north' && newInput == "No") {
      textInput = "You decided to leave";

      document.getElementById("message").innerHTML = textInput;
      state = 'asked_for_direction';
    }


    if (state == 'north' && newInput == "North Again") {
      textInput = "you went to the Forest Home";
      document.getElementById("message").innerHTML = textInput;
      state = 'forest_home';
      // Add how many states as you want
    }

我不知道你的游戏的确切结构,但你肯定可以扩展这段代码。

执行此类操作的基本方法是将游戏状态存储在变量中,每次用户运行命令时,您都会修改游戏状态。查看此代码(如果将其放入您的站点,它应该可以工作):

<script>

var state = "init" // Initialize the game state

function button() {
    var textInput;
    var newInput = input.value;

    if (state == "init") {
        // Handle commands when game state is in "init"

        if (newInput == "Help1") {
            textInput = "[Page 1 of 2. Type 'Help2' for page 2] Commands you can use: <ul><li>South</li><li>North</li><li>East</li><li>West</li><li>North Again</li><li>South again</li><li>West Again</li><li>East Again</li>";
            document.getElementById("message").innerHTML = textInput;
        } else if (newInput == "Help2") {
            textInput = "[Page 2 of 2] Commands you can use: <li>North One More</li><li>East Once More</li><li>West Once More</li><li>South Once More</li>";
            document.getElementById("message").innerHTML = textInput;
        } else if (newInput == "North") {
            textInput = "you went to the Store. Do you explore?";
            state = "store"
            document.getElementById("message").innerHTML = textInput;
        }

    } else if (state == "store") {
        // Handle commands when game state is in "store"

        if (newInput == "Yes") {
            textInput = "There's a giant dragon in the cereal asile. Do you fight it?";
            state = "dragon"
            document.getElementById("message").innerHTML = textInput;
        } else if (newInput == "No") {
            // Change the state to something else
        }

    } else if (state == "dragon") {
        // Handle commands when game state is in "dragon"

        if (newInput == "Yes") {
            textInput = "You slayed the dragon!";
            document.getElementById("message").innerHTML = textInput;
        } else if (newInput == "No") {
            textInput = "You got eaten";
            document.getElementById("message").innerHTML = textInput;
        }

    }
}

</script>