代碼都放在本人的github:fangsini demo演示地址:http://fangsini.github.io/Typing-game/game.htmlhtml
老樣子,先來看下游戲界面: git
主要修改了鍵盤監聽事件:github
<!-- lang: js --> //鍵盤監聽事件,點擊正確時將單詞清除 this.i = -1; document.onkeydown = function(e) { letterAry.i++; var e = e?e:event; for(var j = 0;j < letterAry.letters.length;j++) { if(letterAry.letters[j].isLive) { var index = j; break; } } if(letterAry.letters[j].content.charAt(letterAry.i).charCodeAt(0)==(e.keyCode+32)) { if(letterAry.i == letterAry.letters[j].content.length-1) { letterAry.letters[j].clear(); document.getElementById("num").textContent = parseInt(document.getElementById("num").textContent)+1; letterAry.i = -1; } }else { letterAry.i = -1; } }