我很抱歉,因爲我是一個新手,所以我的編程效率低下,這是一個巨大的難題。Javascript keypress addEventListener
我試圖在HTML5畫布上使用Javascript製作一個非常基本的遊戲,我似乎無法找到一種簡單的方法來「檢查」多個用戶輸入。
我已經成功地使用addEventListener進行單人遊戲。但是,當試圖使其成爲多人遊戲時,一切都會崩潰。我不確定是否使用'keydown'addEventListener兩次。
基本上,遊戲使用WASD鍵和第二個玩家的答案使用箭頭鍵檢查第一個玩家的答案。
目前,我有下面的代碼片段和代碼是完全複製了p2Game與功能 ga.addEventListener('keydown', check2, false)
:
function p1Game() {
ga.addEventListener('keydown', check1, false);
blankp1screen();
p1Time = 0;
switch(random[p1Level]) {
case 1: // if the answer is UP, we will display DOWN
p1drawTriangleDown();
p1correctkeyID = 87; // answer for UP (W) key
break;
case 2: // if the answer is DOWN, we will display UP
p1drawTriangleUp();
p1correctkeyID = 83; // answer for DOWN (S) key
break;
case 3: // if the answer is LEFT, we will display RIGHT
p1drawTriangleRight();
p1correctkeyID = 65; // answer for the LEFT (A) key
break;
case 4: // if the answer is RIGHT, we will display LEFT
p1drawTriangleLeft();
p1correctkeyID = 68; // answer for the RIGHT (D) key
break;
}
function check1(e) {
p1tt += p1Time;
if (e.keyCode == 87 || e.keyCode == 83 || e.keyCode == 65 || e.keyCode == 68) { // Checks if user enters the keys we want
p1Answer = e.keyCode; // Stores the key to check
if (p1correctkeyID == p1Answer) { // If the answer is the correct answer...
if (p1Level < maxlevel) { // ...if we're not on level 10, we'll continue!
blankp1screen();
p1correctkeyID = null;
p1Answer == null;
p1Levelup();
if ((p1Level - p2Level) == checkforafk) {
p2Slow();
} else {
p1Game();
}
} else if (p1Level == maxlevel) { // if we're on the max level, we'll let the player win!
p1Win();
}
} else if (p1correctkeyID !== p1Answer) {
p1Lose(); }
}
}
ga.removeEventListener('keypress', check1, false);
}
對於p2Game:
function p2Game() {
ga.addEventListener('keydown', check2, false);
p2Time = 0;
blankp2screen();
switch(random[p2Level]) {
case 1: // if the answer is UP, we will display DOWN
p2drawTriangleDown();
p2correctkeyID = 38; // answer for UP (W) key
break;
case 2: // if the answer is DOWN, we will display UP
p2drawTriangleUp();
p2correctkeyID = 40; // answer for DOWN (S) key
break;
case 3: // if the answer is LEFT, we will display RIGHT
p2drawTriangleRight();
p2correctkeyID = 37; // answer for the LEFT (A) key
break;
case 4: // if the answer is RIGHT, we will display LEFT
p2drawTriangleLeft();
p2correctkeyID = 39; // answer for the RIGHT (D) key
break;
}
function check2(e) {
p1tt += p2Time;
if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 37 || e.keyCode == 39) { // Checks if user enters the keys we want
p2Answer = e.keyCode; // Stores the key to check
if (p2correctkeyID == p1Answer) { // If the answer is the correct answer...
if (p2Level < maxlevel) { // ...if we're not on level 10, we'll continue!
blankp2screen();
p2correctkeyID = null;
p2Answer == null;
p2Levelup();
if ((p2Level - p1Level) == checkforafk) {
p2Slow();
} else {
p2Game();
}
} else if (p2Level == maxlevel) { // if we're on level 10, we'll let the player win!
p2Win(); // Max Level! Congratulations!
}
} else if (p2correctkeyID !== p2Answer) {
p2Lose();
}
}
}
ga.removeEventListener('keypress', check2, false);
}
請解釋一下問題所在。 「一切都分崩離析」似乎不是一個適當的問題或問題。代碼看起來或多或少是正確的,但由於無法運行,所以不可能知道您目前的問題是什麼。 – damianmr
道歉!在回答/經歷2次迭代之後,遊戲跳轉到p1Lose()並且p2Game()似乎沒有接受任何輸入。 重新啓動遊戲(再次調用p1Game()和p2Game()函數)後,遊戲認爲player1的每個輸入都是錯誤的。 我已經用p2Game()的代碼更新了我的帖子。 – Transit
你必須從頭開始重寫這段代碼。由於您反覆調用'p2Game',因此您還可以多次調用addEventListener,將重複事件處理程序鏈接到click事件。這意味着當您點擊時,相同的功能會被執行很多次,而且只會變得更糟。你需要坐下來重新考慮這一點。 – trincot