2013-12-18 2 views
2

X, Y 및 Z가 3 인 경우 locationName은 Sol과 같아야하지만 그렇지 않습니다. 문제는 내 변수 이름이 업데이트되지 않았지만 다른 것일 수 있다고 생각합니다.html 및 javascript 게임 함수가 전역 변수를 변경하지 않음

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

저는 프로그래밍에 익숙하지 않으며 도움이됩니다.

+0

또한 확인 행을'if (확인 ("요르단의 우주 탐색기를 실행 하시겠습니까?") == false) {return false;}로 변경해야합니다. 그렇지 않으면 스크립트가 계속 실행됩니다. – Jonathan

답변

1

문제는 초기화시 // Encounters part을 한 번만 실행하는 것입니다. 당신은 위치 변경

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(); 
} 
+0

빠른 답장을 보내 주셔서 감사합니다. 코드를 수정합니다. – jordansinn

+0

생명의 은인. 나는 진실로 감사한다. – jordansinn

+0

@jordansinn 여기에 새로 왔기 때문에 [읽을 수 있습니다] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work);) –

0

와 시도를

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

업데이트 locationNamelocationQuery() 함수를 호출하여 주어진 조건에 대해

관련 문제