我有一个用于在 html 5 Canvas 中移动游戏中的 Angular 色的脚本,它只是在检测到鼠标的任一侧时调用一个函数来移动 Angular 色。我如何将其替换为在移动设备上使用的触摸手势?
document.onmousemove = function(e){
if (player.X + c.offsetLeft > e.pageX) {
player.moveLeft();
} else if (player.X + c.offsetLeft < e.pageX) {
player.moveRight();
}
}
最佳答案
我不建议更换它。相反,只需添加触摸事件的处理程序并将事件转换为 onmousemove
处理程序可以理解的内容。
例如,添加一个 ontouchmove
处理程序,将事件的 screenX
和 screenY
转换为 pageX
和 pageY
然后调用现有的 onmousemove
处理程序。那将是 handling events from iOS devices运行移动 Safari。您可能还需要添加一些额外的翻译来处理其他设备/浏览器。
关于javascript - 将鼠标移动改为触摸控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9220623/