Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause when holding down key

So I draw an object on the screen at objectx, objecty and increment objectx when right arrow is pressed, moving the object to the right. The problem I'm running into is that if I hold the right arrow key down it increments once, pauses, and then increments repeatedly. My question is why does it do this, and how can I make the object move fluidly without that initial pause?

$(window).keydown(function(e) {
    if(e.keyCode == 39) {
        objectx++;
    }
}
like image 734
Cains Avatar asked Oct 22 '22 03:10

Cains


1 Answers

Make an interval that increases the objects, on key down, and stop the interval on keyup. (Save the interval ID somewhere, also make sure to not make the interval twice when it's already there)

like image 106
Michiel Dral Avatar answered Nov 03 '22 02:11

Michiel Dral