I want to move paddle in Breakout game by touch. I have tried to make it as mouseover event but it does not work, altough it works when its used as mouseouver event:
document.addEventListener("touchmove", funcTouchMove, false)
function funcTouchMove(e) {
var relativeX = e.clientX - canvas.offsetLeft;
if(relativeX > 0 && relativeX < canvas.width) {
paddleX = relativeX - paddleWidth/2;
}
}
Generally the aim is to touch the paddle, move right and left and play like with mouseover event. I would be grateful for ideas!
robotbrain :
I got it to work by adding the following code :\ndocument.addEventListener("touchmove", touchMoveHandler, false);\n\nfunction touchMoveHandler(event) { \n var relativeX = event.touches[0].clientX - canvas.offsetLeft;\n if(relativeX > 0 && relativeX < canvas.width) {\n paddleX = relativeX - paddleWidth/2;\n }\n}\n\nUpdate:\nThat code had the X touch reading way off to the left if the canvas isn't on the edge of the page. The following gets it to follow under your finger.\n var relativeX = event.touches[0].clientX - canvas.getBoundingClientRect().left;\n",
2021-04-12T03:06:27