首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery - keyDown

jQuery - keyDown
EN

Stack Overflow用户
提问于 2013-07-01 05:23:18
回答 1查看 187关注 0票数 1

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

我正在做我的小引擎,也许是为了一些小游戏。我目前有它为2名球员设置,但我希望能够添加更多在未来。我的问题是,控件(箭头)只对玩家1有效。我有player2的WSAD,但它不起作用。我试着调试它,改变它,等等。找不到问题所在。

以下是键日志记录代码:

代码语言:javascript
复制
//========== 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];
    }
  });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-01 05:50:02

你的具体问题的答案在这里:

代码语言:javascript
复制
$.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]被设置为指向这个新时间间隔的指针,它每隔0毫秒运行一次Move

于是Move开始运行。但是,您可以在Move中执行此检查

代码语言:javascript
复制
if(checkId == 0) ...

因为'w‘只对播放器2有效(checkId为1),所以不会发生任何事情,Move会返回。

然后我们回到你的$.each。现在我们可以和玩家2一起工作了。但是,我们可以这样做:

代码语言:javascript
复制
if (!pressedKeys[e.which]) ...

但是pressedKeys[87]已经设置好了。什么都没发生,但一切都安排好了。因此,程序跳过这一步,继续前进。因此,玩家2的任何动作都不会起作用。

你可以这样做:

添加一个包含对每个玩家有效的密钥的数组。在执行if (!pressedKeys[e.which])之前,请检查已按下的键是否有效:

代码语言:javascript
复制
if (validkeys[currentPlayer.id].indexOf(e.which) == -1) return true;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17394987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档