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

嗜书者

我觉得这个实例真的很简单,我可能想多了,但是有没有办法在我的 if 语句中添加多个语句?我正在寻找类似的东西,如果我在我的游戏中选择去某个方向,你会得到这样的文字“你决定去商店。你想探索吗?” 并且有两种选择:

- 是的,然后在输入框中输入“是”,然后它会给出“探索后,你决定买东西”的结果

-不,你决定离开。

我一直试图弄清楚一个多小时,当我接近时,即使你不在那个位置,命令仍然可以访问。例如。我的游戏中的位置是,“你在公园里。如果在输入框中随机输入,“是”命令将返回“探索后,你决定买东西”

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

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

这是我的代码:

...

选择一个方向

<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>

...

卢克

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

<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在SQL Server中添加多个选择语句一个bcp命令

如何在Excel中添加多个countif语句?

如何在命令中添加多个名称?

如何在AWK命令中添加多个搜索

如何在 javascript 谷歌地图上添加多个标记?

如何在Javascript中添加多个换行符

如何在javascript的html页面中添加多个视图?

如何在当前选择语句中添加多列作为新字段?

如何在JavaScript中的if语句中指定多个条件

如何在Jenkins的“执行Windows批处理命令”中串联添加多个命令

我如何在命令行中少添加多个“ +”命令

如何在echo语句中包含命令?

如何在if语句中使用多个“not in”?

如何在 if 语句中包含多个条件?

如何使用while语句添加多个值?

如何在SQL语句中添加引号

如何在IF语句中添加值

如何在一个命令中添加多个目录,例如名称1-100

如何在命令行中添加多个网络块?

如何在一行命令中添加多个单词

如何在bcp命令SQL Server中添加多个查询

如何在批处理脚本中添加多个命令 insde 单个 if 循环

docker-compose 如何添加多个命令?

单个 npm 语句中的多个 install 命令如何工作?

如何在Chef gaurd内添加多行语句?

如何在此if / else语句中添加if / else语句?

如何在JavaScript中使用for循环/单击事件添加多个按钮?

如何在 Javascript 中为一个类添加多个属性?

如何在不重复 javascript 代码的情况下添加多个模式