2016-07-15 59 views
-1

我只在使用javascript的Hangman遊戲上工作。這是一個班級。我是javascript和DOM的noob,所以請善待。在這一點上,我只想在控制檯日誌中打印出SOMETHING,真的。除了這裏展示的少數功能和遊戲過程外,我已經花了好幾天和幾個小時去嘗試讓某些東西起作用。我的思考過程是,在循環中不會改變的變量,不需要在html中動態更新,不需要成爲hang子手遊戲的屬性。但是,當我運行下面的代碼只是爲了讓console.log工作,我得到的userGuess沒有定義。我想我是通過分配keyUp事件的值來定義它的。如果我讓用戶猜測Hangman遊戲的屬性,然後用hangmanGame.userGuess更新我的所有代碼,我會得到所有這些意外的令牌錯誤。無法理解在Javascript中使用DOM的範圍和變量

有什麼想法?

//Variables=============================================================== 
var hangmanGame = {presidents: [ 
    "WASHINGTON", 
    "ADAMS", 
    "JEFFERSON", 
    "MADISON", 
    "MONROE", 
    "JACKSON", 
    "VANBUREN", 
    "HARRISON", 
    "TYLER", 
    "POLK", 
    "TAYLOR", 
    "FILLMORE", 
    "PIERCE", 
    "BUCHANAN", 
    "LINCOLN", 
    "JOHNSON", 
    "GRANT", 
    "HAYES", 
    "GARFIELD", 
    "ARTHUR", 
    "CLEVELAND", 
    "HARRISON", 
    "MCKINLEY", 
    "ROOSEVELT", 
    "TAFT", 
    "WILSON", 
    "HARDING", 
    "COOLIDGE", 
    "HOOVER", 
    "TRUMAN", 
    "EISENHOWER", 
    "KENNEDY", 
    "NIXON", 
    "FORD", 
    "CARTER", 
    "REAGAN", 
    "BUSH", 
    "CLINTON", 
    "OBAMA" 
], 

    wrongAllowed: 7, 
    goodLetters: 0, 
    wordArray: [], 
    badArray: [], 
    wins: 0, 

//functions========================================================= 
    startGame: function() { 
    var word = hangmanGame.presidents[Math.floor(Math.random() * hangmanGame.presidents.length)]; 
    }, 
} 

    function write() { 
    console.log("President" + word); 
    } 


//main process=============================== 
    document.onkeyup = function(event) { 
    var userGuess = String.fromCharCode(event.keyCode).toUpperCase(); 
    } 

    if (userGuess == 'A'){ 
     write(); 
    } 
+0

哇。格式化抱歉。這是我的第一個帖子。我認爲我在發佈之前縮進了這一切。我不確定它的第一部分是否在代碼框外? – JenPem

+0

我不能從你的縮進中得知,但它看起來像你的'hangmanGame'對象沒有完全關閉 –

+1

你只定義了userGuess作爲局部變量,所以它在這段代碼之外是不可見的document.onkeyup = function(事件){}。通過簡單地將userGuess添加爲全局變量,您將解決問題。希望這可以幫助。 – acac97

回答

3

改成這樣

// Main process 
document.onkeyup = function(event) { 
    var userGuess = String.fromCharCode(event.keyCode).toUpperCase(); 
    if (userGuess === 'A') { 
    write(); 
    } 
} 
+0

或者執行此操作xDDD – acac97

+0

在write()調用時定義了哪個單詞? – guest271314

+0

@ guest271314'window.hangmanGame.startGame()',如果OP會在方法中返回該變量 –

0

包括if語句中keyup事件處理程序;從handman.startGame功能returnword變量,傳遞到word功能write

//Variables=============================================================== 
 
var hangmanGame = { 
 
    presidents: [ 
 
    "WASHINGTON", 
 
    "ADAMS", 
 
    "JEFFERSON", 
 
    "MADISON", 
 
    "MONROE", 
 
    "JACKSON", 
 
    "VANBUREN", 
 
    "HARRISON", 
 
    "TYLER", 
 
    "POLK", 
 
    "TAYLOR", 
 
    "FILLMORE", 
 
    "PIERCE", 
 
    "BUCHANAN", 
 
    "LINCOLN", 
 
    "JOHNSON", 
 
    "GRANT", 
 
    "HAYES", 
 
    "GARFIELD", 
 
    "ARTHUR", 
 
    "CLEVELAND", 
 
    "HARRISON", 
 
    "MCKINLEY", 
 
    "ROOSEVELT", 
 
    "TAFT", 
 
    "WILSON", 
 
    "HARDING", 
 
    "COOLIDGE", 
 
    "HOOVER", 
 
    "TRUMAN", 
 
    "EISENHOWER", 
 
    "KENNEDY", 
 
    "NIXON", 
 
    "FORD", 
 
    "CARTER", 
 
    "REAGAN", 
 
    "BUSH", 
 
    "CLINTON", 
 
    "OBAMA" 
 
    ], 
 

 
    wrongAllowed: 7, 
 
    goodLetters: 0, 
 
    wordArray: [], 
 
    badArray: [], 
 
    wins: 0, 
 

 
    //functions========================================================= 
 
    startGame: function() { 
 
    var word = hangmanGame.presidents[Math.floor(Math.random() * hangmanGame.presidents.length)]; 
 
    return word 
 
    } 
 
} 
 

 
function write(word) { 
 
    console.log("President " + word); 
 
} 
 

 

 
//main process=============================== 
 
var hangman = hangmanGame.startGame(); 
 

 

 
document.onkeyup = function(event) { 
 
    var userGuess = String.fromCharCode(event.keyCode).toUpperCase(); 
 
    if (userGuess == 'A') { 
 
    write(hangman); 
 

 
    } 
 
}