Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer to your specific problem lies here:</p> <pre><code>$.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); } }); </code></pre> <p>Let's say <code>w</code> is pushed. This code iterates through the player list, beginning with Player 1 (<code>currentPlayer.id</code> is 0). Let's say that initially <code>pressedKeys</code> is empty. Since <code>w</code> was pressed, <code>pressedKeys[87]</code> is set to be a pointer to this new interval, which runs <code>Move</code> every 0 milliseconds.</p> <p>So <code>Move</code> runs. However, you have this check in <code>Move</code>:</p> <pre><code>if(checkId == 0) ... </code></pre> <p>Since 'w' is only valid for Player 2 (<code>checkId</code> is 1), nothing happens and <code>Move</code> returns.</p> <p>Then we go back to your <code>$.each</code>. Now we get to work with Player 2. However, we reach this:</p> <pre><code>if (!pressedKeys[e.which]) ... </code></pre> <p>But <code>pressedKeys[87]</code> is already set. Nothing happened, but it was set. So the program skips over this step and moves on. Hence, none of Player 2's actions will ever work.</p> <p>You could do something like this:</p> <p>Add an array with the keys that are valid for each player. Before doing <code>if (!pressedKeys[e.which])</code>, check if the key that has been pressed is valid:</p> <pre><code>if (validkeys[currentPlayer.id].indexOf(e.which) == -1) return true; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload