In this game I'm making, whenever I move my character, it only takes one step, pauses, and then keeps moving. Example: if I hold a button to move it will go the 2.5 units in whatever direction, pause for about 1/2 a second, and then continue to move for as long as I am holding down the button.
Here is the relevent code:
addEventListener('keydown',function(action){
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
if (action.code == "KeyD" || action.code == "ArrowRight" ){
ui.image_x = 192;
ui.hero.position.x = ui.hero.position.x + 2.5;
ui.update();
ui.draw();
ui.draw_sprite();
}
if (action.code == "KeyA" || action.code == "ArrowLeft" ){
ui.image_x = 288;
ui.hero.position.x = ui.hero.position.x - 2.5;
ui.update();
ui.draw();
ui.draw_sprite();
}
if (action.code == "KeyW" || action.code == "ArrowUp" ){
ui.image_x = 128;
ui.hero.position.y = ui.hero.position.y - 2.5;
ui.update();
ui.draw();
ui.draw_sprite();
}
if (action.code == "KeyS" || action.code == "ArrowDown" ){
ui.image_x = 32;
ui.hero.position.y = ui.hero.position.y + 2.5;
ui.update();
ui.draw();
ui.draw_sprite();
}