[英]html and javascript game function not changing global variable
When X,Y, and Z are equal to 3 then locationName should be equal to Sol, but this is not the case. 当X,Y和Z等于3时,locationName应该等于Sol,但事实并非如此。 I believe the problem is with my variable name not being updated but it could be something else.
我相信问题出在我的变量名没有更新,但可能是其他原因。
<!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>
I am new to programming and would appreciate any help. 我是编程新手,不胜感激。
The problem is that you execute your // Encounters part
only once, at initialization. 问题是初始化时您只执行一次
// Encounters part
。 You should put it in a function : 您应该将其放在一个函数中:
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.");
}
}
and call that function every time you change the position : 并在每次更改位置时调用该函数:
function goDown(){
locationZ --;
console.log("You go down.");
encounters();
}
Try with, 试试看,
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);
} }
Update locationName
against given condition by invoking locationQuery()
function 通过调用
locationQuery()
函数在给定条件下更新locationName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.