這裏是解決你的問題。
我將回車鍵邏輯添加到您的eventlistener中。所以當你按下回車鍵開始遊戲。 我刪除了startGameScreen()
中的循環,以防止瀏覽器崩潰重複此無限循環。 http://jsfiddle.net/3aoozxte/19/
function main() {
canvas = document.createElement("canvas");
canvas.width = COLS*20;
canvas.height = ROWS*20;
ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
ctx.font = "12px Helvetica";
frames = 0;
keystate = {};
started = false;
// keeps track of the keyboard input
document.addEventListener("keydown", function(evt) {
keystate[evt.keyCode] = true;
if(keystate[Key_ENTER] && !started) { // Start the game if it's not started and Enter is pressed.
started = true;
init();
loop();
}
});
document.addEventListener("keyup", function(evt) {
delete keystate[evt.keyCode];
});
//Attempting to create a welcome screen
startGameScreen();
}
function startGameScreen() {
ctx.font = "25px Helvetica";
drawStartScreen();
}
似乎你不隨時隨地的捕捉按鍵緊圈可以工作o他們。 – 2014-09-05 08:02:46
遊戲響應按鍵,因爲我可以移動蛇,所以林不知道你的意思? – Light1c3 2014-09-05 08:03:52
'do {if(code == 13){code = 13; }/* ... * /}而(代碼=== 13);'這是...奇怪。你能詳細說明你在那裏想達到什麼嗎? – 2014-09-05 08:04:12