1
I’m creating a pong using Jquery and I’m having trouble with the keydown event. I will control the player by the up and down arrow, but when I keep the button pressed, it has a delay between the first and second run.
JS:
$(document).keydown(function(e) {
switch(e.which) {
case 38:
if ((topPlayerOne - 10) >= 0) {
topPlayerOne = (topPlayerOne - 10);
playerOne.css('top', topPlayerOne);
}
break;
case 40:
if ((topPlayerOne + 10) <= 170) {
topPlayerOne = (topPlayerOne + 10);
playerOne.css('top', topPlayerOne);
}
break;
default: return;
}
e.preventDefault();
HTML:
<div id="pongTable">
<div id="playerOne" class="player"></div>
<div id="playerAI" class="player"/></div>
</div>
<div id="score">
<div id="scorePlayerOne" class="score">0</div>
<div id="scorePlayerAI" class="score">0</div>
</div>