我觉得这个实例真的很简单,我可能已经想过了,但是有没有办法在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>
看看状态机的结构。您应该将用户的当前状态存储在游戏中,并在每次按下按钮时进行检查。
状态机设计模式将检查播放器当前所在的位置,并采取相应的措施。
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
}
我不知道您游戏的确切结构,但是请确保您可以扩展此代码。