Javascript jQuery-keyDown
我有问题,我不知道为什么。任何帮助都将不胜感激 我在做我的小引擎,也许是为了一些小游戏。目前我已经为2名玩家设置了它,但我希望将来能够添加更多。我的问题是控件(箭头)只对播放器1起作用。我已经为player2安装了WSAD,但这不起作用。我试过调试它,改变它等等。。也无法解决问题 以下是关键日志记录代码:Javascript jQuery-keyDown,javascript,jquery,move,keydown,Javascript,Jquery,Move,Keydown,我有问题,我不知道为什么。任何帮助都将不胜感激 我在做我的小引擎,也许是为了一些小游戏。目前我已经为2名玩家设置了它,但我希望将来能够添加更多。我的问题是控件(箭头)只对播放器1起作用。我已经为player2安装了WSAD,但这不起作用。我试过调试它,改变它等等。。也无法解决问题 以下是关键日志记录代码: //========== KEY LOGGING ========== var pressedKeys = []; //declare as globals coz of debug var
//========== 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];
}
});
/=============密钥记录==========
var pressedKeys=[];
//声明为调试的全局coz
var x;
变量y;
var-x2;
变量y2;
函数moveLeft(checkId、checkX、checkY、cSize、cSpeed、cKey){
如果(x>0){
playerList[checkId].x=checkX-cSpeed;
}否则{
playerList[checkId].x=0;
}
}
函数向上移动(checkId、checkX、checkY、cSize、cSpeed、cKey){
如果(y>0){
playerList[checkId].y=checkY-cSpeed;
}否则{
playerList[checkId].y=0;
}
}
函数moveRight(checkId、checkX、checkY、cSize、cSpeed、cKey){
如果(x2<宽度){
playerList[checkId].x=checkX+cSpeed;
}否则{
playerList[checkId].x=宽度-cSize;
}
}
函数向下移动(checkId、checkX、checkY、cSize、cSpeed、cKey){
如果(y2<高度){
playerList[checkId].y=checkY+cSpeed;
}否则{
playerList[checkId].y=高度-cSize;
}
}
函数移动(checkId、checkX、checkY、cSize、cSpeed、cKey){
x=检查x-cSpeed;
y=检查y-cSpeed;
x2=检查X+cSize+cSpeed;
y2=检查Y+cSize+cSpeed;
//玩家1
如果(检查ID==0){
开关(cKey){
案例37:
//左
向左移动(checkId、checkX、checkY、cSize、cSpeed、cKey);
打破
案例38:
//向上
上移(checkId、checkX、checkY、cSize、cSpeed、cKey);
打破
案例39:
//对
moveRight(检查ID、检查X、检查Y、cSize、cSpeed、检查Y);
打破
案例40:
//向下
向下移动(checkId、checkX、checkY、cSize、cSpeed、cKey);
打破
违约:
return;//退出其他键的处理程序
}
}
//玩家2
如果(检查ID==1){
开关(cKey){
案例65:
//左A
向左移动(checkId、checkX、checkY、cSize、cSpeed、cKey);
打破
案例87:
//up-W
上移(checkId、checkX、checkY、cSize、cSpeed、cKey);
打破
案例68:
//右-D
moveRight(检查ID、检查X、检查Y、cSize、cSpeed、检查Y);
打破
案例83:
//羽绒服
向下移动(checkId、checkX、checkY、cSize、cSpeed、cKey);
打破
违约:
return;//退出其他键的处理程序
}
}
}
//==按下键==
$(document.body).keydown(函数(e){
e、 预防默认值();
//检查所有球员
$.each(playerList,function(i,currentPlayer){
如果(!按按键[e.which]){
//设置函数的间隔
按键[e.which]=设置间隔(函数(){
移动(currentPlayer.id、currentPlayer.x、currentPlayer.y、currentPlayer.size、currentPlayer.speed、e.which)
}, 0);
}
});
//+
如果(按[107]键){
currentPlayer.speed+=1;}
// -
如果(按[109]&¤tPlayer.speed>1){
currentPlayer.speed-=1;
}
//addplayer
如果(按[80]键){
addPlayer(“红色”,尺寸,宽度/2+id*尺寸,高度/2+id*尺寸);
}
});
//==键控==
$(document.body).keyup(函数(e){
如果(按按键[e.which]){
clearInterval(按[e.which]键);
删除按过的键[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)。假设最初按下的键是空的。由于按下了w
,pressedKeys[87]
被设置为指向此新间隔的指针,该间隔每0毫秒运行一次Move
所以Move
运行。但是,您有此签入移动:
if(checkId == 0) ...
由于“w”仅对玩家2有效(checkId
为1),因此不会发生任何事情,并且Move
返回
然后我们回到您的$。每个。现在我们开始与玩家2合作。然而,我们做到了这一点:
if (!pressedKeys[e.which]) ...
但是按键[87]
已设置。什么也没发生,但一切都安排好了。所以程序跳过这一步,继续前进。因此,玩家2的任何动作都不会起作用
你可以这样做:
添加一个数组,其中包含对每个玩家有效的密钥。在执行操作之前,如果(!pressedKeys[e.which])
,请检查所按下的键是否有效:
if (validkeys[currentPlayer.id].indexOf(e.which) == -1) return true;
playerList看起来像什么?非常感谢,我现在看到问题了。你还不喜欢/想在那里换什么?