I have this script for moving a character in a game in a html 5 canvas, it simply calls a function to move the character if the mouse is detected either side. How would I swap that for a touch gesture to be used on a mobile device?
document.onmousemove = function(e){
if (player.X + c.offsetLeft > e.pageX) {
player.moveLeft();
} else if (player.X + c.offsetLeft < e.pageX) {
player.moveRight();
}
}
I wouldn’t recommend swapping it out. Instead just add handlers for touch events and translate the events into something your
onmousemovehandler can understand.So for instance, add an
ontouchmovehandler that translates the event’sscreenXandscreenYtopageXandpageYand then calls your existingonmousemovehandler. That would be for handling events from iOS devices running mobile Safari. You’ll probably have to add some additional translations to handle other devices/browsers as well.