I want to add touch controls when the game is played on touch screen devices , Ideally, the user paddle would move every time I touch the screen. Here is the code I have inputted for my key presses.
window.addEventListener(‘keydown’, keyDownHandler);
window.addEventListener(‘keyup’, keyUpHandler);
function keyDownHandler(event) {
switch (event.keyCode) {
case 38:
upArrowKey = true;
break;
case 40:
downArrowKey = true;
break;
}
}
function keyUpHandler(event) {
switch (event.keyCode) {
case 38:
upArrowKey = false;
break;
case 40:
downArrowKey = false;
break;
}
}
And this is the code that I inputted to move the ball if user presses key.
let userSpeed = 9;
if (upArrowKey && user.y > 0) {
user.y -= userSpeed;
}else if (downArrowKey && (user.y < canvas.height - user.height)) {
user.y += userSpeed;
}
How can I implement touch controls in the same way?