Home:ALL Converter>Touch event in Breakout game in js

Touch event in Breakout game in js

Ask Time:2017-10-04T21:57:52         Author:KrisW

Json Formatter

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!

Author:KrisW,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46566796/touch-event-in-breakout-game-in-js
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
yy