javascript - jQuery-按键按下

标签 javascript jquery move keydown

我有一个问题,我不知道为什么。任何帮助将不胜感激。

我正在制作我的小引擎,也许是为了一些小游戏。我目前为 2 个玩家设置了它,但我希望将来能够添加更多。我遇到的问题是控件(箭头)仅适用于玩家 1。我有适用于玩家 2 的 WSAD,但这不起作用。我试过调试它、更改它等等......但无法找出问题所在。

这是按键记录代码:

//========== KEY LOGGING ==========
var pressedKeys = [];
//declare as globals coz of debug
var x;
var y;
var x2;
var y2;

function moveLeft(checkId, checkX, checkY, cSize, cSpeed, cKey) {
  if (x > 0) {
    playerList[checkId].x = checkX - cSpeed;
  } else {
    playerList[checkId].x = 0;
  }
}
function moveUp(checkId, checkX, checkY, cSize, cSpeed, cKey) {
  if (y > 0) {
    playerList[checkId].y = checkY - cSpeed;
  } else {
    playerList[checkId].y = 0;
  }
}
function moveRight(checkId, checkX, checkY, cSize, cSpeed, cKey) {
  if (x2 < width) {
    playerList[checkId].x = checkX + cSpeed;
  } else {
    playerList[checkId].x = width - cSize;
  }
}
function moveDown(checkId, checkX, checkY, cSize, cSpeed, cKey) {
  if (y2 < height) {
    playerList[checkId].y = checkY + cSpeed;
  } else {
    playerList[checkId].y = height - cSize;
  }
}
function Move(checkId, checkX, checkY, cSize, cSpeed, cKey) {
  x = checkX - cSpeed;
  y = checkY - cSpeed;
  x2 = checkX + cSize + cSpeed;
  y2 = checkY + cSize + cSpeed;
  //player 1
  if(checkId == 0) {
    switch (cKey) {
      case 37:
        // left
        moveLeft(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      case 38:
        // up 
        moveUp(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      case 39:
        // right
        moveRight(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      case 40:
        // down
        moveDown(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      default:
        return; // exit this handler for other keys
    }
  }
  //player 2
  if(checkId == 1) {
    switch (cKey) {
      case 65:
        // left - A
        moveLeft(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      case 87:
        // up - W
        moveUp(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      case 68:
        // right - D
        moveRight(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      case 83:
        // down - S
        moveDown(checkId, checkX, checkY, cSize, cSpeed, cKey);
        break;
      default:
        return; // exit this handler for other keys
    }
  }
}

// == KEYDOWN ==
$(document.body).keydown(function (e) {
  e.preventDefault();
  //go through all players
  $.each(playerList, function (i, currentPlayer) {
    if (!pressedKeys[e.which]){
      //set interval for the function
      pressedKeys[e.which] = setInterval(function(){
        Move(currentPlayer.id, currentPlayer.x, currentPlayer.y, currentPlayer.size, currentPlayer.speed, e.which)
      }, 0);
    }
  });
  //+
  if (pressedKeys[107]) {
    currentPlayer.speed += 1; }
    // -
    if (pressedKeys[109] && currentPlayer.speed > 1) {
      currentPlayer.speed -= 1;
    }
    //addplayer
    if (pressedKeys[80]) {
      addPlayer("red", size, width / 2 + id * size, height / 2 + id * size);
    }
  });
  // == KEYUP ==
  $(document.body).keyup(function (e) {
    if (pressedKeys[e.which]){
      clearInterval(pressedKeys[e.which]);
      delete pressedKeys[e.which];
    }
  });

最佳答案

您的特定问题的答案就在这里:

$.each(playerList, function (i, currentPlayer) {            
  if (!pressedKeys[e.which]){ 
    //set interval for the function
    pressedKeys[e.which] = setInterval(function() {
      Move(currentPlayer.id, currentPlayer.x, currentPlayer.y, currentPlayer.size, currentPlayer.speed, e.which);
    }, 0);
  }
});

假设w被推。此代码遍历玩家列表,从玩家 1(currentPlayer.id 为 0)开始。假设最初是 pressedKeys是空的。自 w被按下,pressedKeys[87]设置为指向此新间隔的指针,该间隔运行 Move每 0 毫秒一次。

所以 Move运行。但是,您在 Move 中有此检查:

if(checkId == 0) ...

因为“w”仅对玩家 2 有效(checkId 为 1),所以没有任何反应,Move返回。

然后我们回到你的$.each .现在我们开始使用 Player 2。但是,我们达到了这个目的:

if (!pressedKeys[e.which]) ...

但是pressedKeys[87]已经设置。什么都没有发生,但已经注定了。所以程序跳过这一步继续前进。因此,玩家 2 的任何操作都不会奏效。

你可以这样做:

添加一个数组,其中包含对每个玩家都有效的键。做之前if (!pressedKeys[e.which]) ,检查按下的键是否有效:

if (validkeys[currentPlayer.id].indexOf(e.which) == -1) return true;

关于javascript - jQuery-按键按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17394987/

相关文章:

javascript - 在 javascript 或 bootstrap 中创建可点击的工具提示

javascript - 简单的 JQuery 脚本动画不起作用

swift - 如何让玩家在路径中 move 到另一边?

诅咒将光标 move 到非空白位置

javascript - 这个函数在语义上是否正确?

即使单击“确定”后,javascript 警报仍会连续触发

javascript - 将 javascript 变量分配给 php 代码的简单方法

javascript - 当我使用 angularjs 部分时如何运行 jquery?

javascript - 我制作了一个 jquery 幻灯片放映但它不能正常工作,我该怎么办

php - 无法重命名文件夹(PHP + Linux)