简体   繁体   English

JavaScript按键addEventListener

[英]Javascript keypress addEventListener

I apologise in advance for the huge eyesore that is my inefficient coding as I'm a newbie. 对于新手来说,我的编码效率低下,给我带来极大的困扰,我为此深表歉意。

I'm attempting to make a really basic game using Javascript on a HTML5 canvas and I can't seem to find a straightforward method to "check" for multiple user inputs. 我正在尝试使用HTML5画布上的Javascript制作一款真正的基本游戏,但似乎找不到一种直接检查多个用户输入的简单方法。

I've successfully used addEventListener for the singleplayer game. 我已经成功地将addEventListener用于单人游戏。 However, when trying to make it multiplayer, everything falls apart. 但是,当尝试使其成为多人游戏时,一切都崩溃了。 I'm not sure if using 'keydown' addEventListener twice works. 我不确定两次使用'keydown'addEventListener是否可行。

Basically, the game checks for the first player's answer using the WASD keys as well as the second player's answer using the arrow keys. 基本上,游戏使用WASD键检查第一位玩家的答案,并使用箭头键检查第二位玩家的答案。

I currently have the following snippet of code and the code is duplicated exactly for p2Game with the function ga.addEventListener('keydown', check2, false) : 我目前有以下代码段,并且使用ga.addEventListener('keydown', check2, false)函数为p2Game精确复制了代码:

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

For p2Game: 对于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);
}

Well is hard to tell without a running example to debug. 如果不运行示例进行调试,很难说出来。 But first off, if you run your code twice you will have two listeners for each player. 但是首先,如果您两次运行代码,则每个播放器都有两个侦听器。 If you run it three times you will have three, and so on. 如果您运行它三次,您将拥有三个,依此类推。 That's because you are adding an event listener for the keydown event but then you are removing the keypress , which basically means that you never remove the keydown handler. 那是因为您要为keydown事件添加一个事件侦听器,但是随后要删除keypress ,这基本上意味着您永远不会删除keydown处理程序。

My advice would be that you should not handle the two inputs in separate places, it may seem easy at first but it creates 'synchronization' problems in your code, as you have to deal with the state of player 1 in the handling code for player 2. So, have just one event handling code and do whatever you need to do for every case. 我的建议是,您不应该在单独的位置处理两个输入,乍一看似乎很简单,但是它会在代码中产生“同步”问题,因为您必须在处理播放器的代码中处理播放器1的状态2.因此,只需一个事件处理代码,并针对每种情况做您需要做的所有事情。 I think you will end up having code that is more easy to follow if you use good abstractions (I know you said you are starting with programming, so that's why I am suggesting that you should follow an approach that should be more easy to reason about now). 我认为,如果您使用良好的抽象,最终将得到更易于遵循的代码(我知道您说过您是从编程开始的,所以这就是为什么我建议您采用一种应该更易于推理的方法的原因)现在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM