I'm making a platformer HTML game and have a problem that the player doesn't instantly move after I trigger the touch event. When I start pressing the button I have to wait 1-2 seconds till the player starts moving
I've also had other games with the same problem where I tried other methods fixing it.
Here's the code:
<div id="stage">
<div id="player"></div>
<div id="block"></div>
<button ontouchstart="config.speedX = -1" ontouchend="config.speedX = 0" id="left"></button>
<button ontouchstart="config.speedX = 1" ontouchend="config.speedX = 0" id="right"></button>
</div>
let config = {
x: 0,
y: 0,
speedX: 0,
speedY: 0
};
let gameloop = setInterval(() => {
player.style.left = config.x + "px";
player.style.bottom = config.y + "px";
config.x += config.speedX;
config.y += config.speedY;
}, 20);