html和javascript游戏函数不更改全局变量
当X、Y和Z等于3时,locationName应等于Sol,但实际情况并非如此。我相信问题在于我的变量名没有更新,但可能是其他原因html和javascript游戏函数不更改全局变量,javascript,html,adventure,Javascript,Html,Adventure,当X、Y和Z等于3时,locationName应等于Sol,但实际情况并非如此。我相信问题在于我的变量名没有更新,但可能是其他原因 <!DOCTYPE html> <html> <head> <title>Jordan's Space Explorer </title> </head> <body> <button onclick="goNorth()">Go North</but
<!DOCTYPE html>
<html>
<head>
<title>Jordan's Space Explorer </title>
</head>
<body>
<button onclick="goNorth()">Go North</button>
<button onclick="goSouth()">Go South</button>
<button onclick="goEast()">Go East</button>
<button onclick="goWest()">Go West</button>
<button onclick="goUp()">Go Up</button>
<button onclick="goDown()">Go Down</button>
<button onclick="locationQuery()">Where am I?</button>
<button onClick="quit"()>Quit</button>
</body>
<script>
// Confirm user wants to play.
confirm("Do you want to play Jordan's Space Explorer?");
alert("The game is working.");
// Declare start location.
var locationY = 0;
var locationX = 0;
var locationZ = 0;
var locationName = "in Space";
//Go directions
function goNorth(){
locationY ++;
console.log("You head North.");
}
function goSouth(){
locationY --;
console.log("You head South.");
}
function goEast(){
locationX ++;
console.log("You head East");
}
function goWest(){
locationX --;
console.log("You head West");
}
function goUp(){
locationZ ++;
console.log("You go up.");
}
function goDown(){
locationZ --;
console.log("You go down.");
}
function locationQuery(){
console.log("X" +locationX);
console.log("Y" +locationY);
console.log("Z" +locationZ);
console.log("You are " +locationName);
}
// Encounters
if(locationX === 3 && locationY === 3 && locationZ === 3){
locationName = "Sol";
alert("Arrived at " + locationName);
}
if(locationX===0 && locationY===0 && locationZ===0){
alert("It worked.");
}
</script>
</html>
约旦的太空探险家
北上
南下
向东走
向西走
向上
下降
我在哪里?
退出
//确认用户想要玩。
确认(“你想玩乔丹的太空探险家吗?”);
警惕(“游戏正在运行。”);
//声明开始位置。
变量位置y=0;
var locationX=0;
变量位置z=0;
var locationName=“在空间中”;
//走向
函数goNorth(){
locationY++;
日志(“你往北走”);
}
函数goSouth(){
地点--;
log(“你向南走”);
}
函数goEast(){
locationX++;
console.log(“你向东走”);
}
函数goWest(){
位置X-;
console.log(“你向西走”);
}
函数goUp(){
locationZ++;
log(“你上去了。”);
}
函数仓库(){
位置Z-;
日志(“你下去了。”);
}
函数locationQuery(){
控制台日志(“X”+位置X);
控制台日志(“Y”+位置Y);
控制台日志(“Z”+位置Z);
console.log(“您是”+locationName);
}
//遭遇
if(locationX==3&&locationY==3&&locationZ==3){
locationName=“Sol”;
警报(“到达”+位置名称);
}
if(locationX==0&&locationY==0&&locationZ==0){
警惕(“它起作用了”);
}
我是编程新手,非常感谢您的帮助。问题是您在初始化时只执行了一次
//部分。你应该把它放在一个函数中:
function encounters(){
if(locationX === 3 && locationY === 3 && locationZ === 3){
locationName = "Sol";
alert("Arrived at " + locationName);
}
if(locationX===0 && locationY===0 && locationZ===0){
alert("It worked.");
}
}
并在每次更改位置时调用该函数:
function goDown(){
locationZ --;
console.log("You go down.");
encounters();
}
试着
function locationQuery(){
console.log("X" +locationX);
console.log("Y" +locationY);
console.log("Z" +locationZ);
// Encounters
if(locationX === 3 && locationY === 3 && locationZ === 3){
locationName = "Sol";
alert("Arrived at " + locationName);
}
if(locationX===0 && locationY===0 && locationZ===0){
alert("It worked.");
}
console.log("You are " +locationName);
}
通过调用locationQuery()
函数,根据给定条件更新locationName
,您还应该将确认行更改为if(确认(“您想玩Jordan的太空探索者吗?”)==false){return false;}
,否则脚本将继续执行。@jordansin既然您是新来的,您可能会;)