I have found similar threads but they only talk about removing the initial delay, not setting a delay.
I have a game that moves the user accordingly if wasd is pressed and moves them to the next grid coordinate. However, if the user holds down any of the wasd keys, they shoot across nearly instantly, which I would rather not have. I have tried to create a delay in the movement, which works if you tap the key fast but not if held down. I have the following code:
window.addEventListener("keydown", onKeyDown, false);
function onKeyDown(event) {
var keyCode = event.keyCode;
if (canMove == true) {
switch (keyCode) {
case 68: //d
keyD = true;
canMove = false;
break;
case 83: //s
keyS = true;
canMove = false;
break;
case 65: //a
keyA = true;
canMove = false;
break;
case 87: //w
keyW = true;
canMove = false;
break;
}
setTimeout(function(){canMove = true;}, 400);
}
checkMovement();
}
My problem: I can press 'a' for example and zoom across the map nearly instantly. I want their to be a delay, of say 200-500ms so that they move at a steady slow rate. How to fix this?