我觉得这个实例真的很简单,我可能想多了,但是有没有办法在我的 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] 删除。
我来说两句