Skip to main content
deleted 746 characters in body; edited title
Source Link
dda
  • 6.2k
  • 2
  • 27
  • 37

JQuery jQuery - keyDown

I gothave a problem, and I have no idea why. Any help would be appreciated.

I am making my little engine, maybe for some little game. I have it currently setupset up for 2 players, but I want to be able to add more in future. The problem I have, is that controls (arrows) work only for player 1. I have WSAD for player2, but that does not work. I have tried debugingdebugging it, changing it etc. .. and cant figure out the problem.

 //========== 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]; }  }    }); 

JQuery - keyDown

I got a problem, and I have no idea why. Any help would be appreciated.

I am making my little engine, maybe for some little game. I have it currently setup for 2 players, but I want to be able to add more in future. The problem I have, is that controls (arrows) work only for player 1. I have WSAD for player2, but that does not work. I have tried debuging it, changing it etc. .. and cant figure out the problem.

 //========== 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]; }    }); 

jQuery - keyDown

I have a problem, and I have no idea why. Any help would be appreciated.

I am making my little engine, maybe for some little game. I have it currently set up for 2 players, but I want to be able to add more in future. The problem I have is that controls (arrows) work only for player 1. I have WSAD for player2, but that does not work. I have tried debugging it, changing it etc. .. and cant figure out the problem.

//========== 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]; }  }); 
Source Link
user2394418
user2394418

JQuery - keyDown

I got a problem, and I have no idea why. Any help would be appreciated.

I am making my little engine, maybe for some little game. I have it currently setup for 2 players, but I want to be able to add more in future. The problem I have, is that controls (arrows) work only for player 1. I have WSAD for player2, but that does not work. I have tried debuging it, changing it etc. .. and cant figure out the problem.

Here is the key-logging code:

 //========== 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]; } });