【问题标题】:Swap mouse move for touch control交换鼠标移动以进行触摸控制
【发布时间】:2012-03-02 11:59:45
【问题描述】:

我有这个脚本用于在 html 5 画布中移动游戏中的角色,如果在任一侧检测到鼠标,它只需调用一个函数来移动角色。我如何将它换成在移动设备上使用的触摸手势?

document.onmousemove = function(e){  
    if (player.X + c.offsetLeft > e.pageX) {  
        player.moveLeft();  
    } else if (player.X + c.offsetLeft < e.pageX) {  
        player.moveRight();  
    }  
}

【问题讨论】:

    标签: javascript android ios html5-canvas


    【解决方案1】:

    我不建议换掉它。相反,只需为触摸事件添加处理程序并将事件转换为您的onmousemove 处理程序可以理解的内容。

    例如,添加一个ontouchmove 处理程序,将事件的screenXscreenY 转换为pageXpageY,然后调用现有的onmousemove 处理程序。那将适用于运行移动 Safari 的handling events from iOS devices。您可能还需要添加一些额外的翻译来处理其他设备/浏览器。

    【讨论】:

    • 欢呼声,有点工作,让它移动了一点,但后来停止了。需要弄清楚如何循环移动直到 touchend。
    • 一个标志和一个setTimeout 循环可能会起作用。类似function movementLoop() { document.onmousemove(touchPoint); if(flag) setTimeout(movementLoop, 100); }
    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2016-12-29
    • 2018-05-20
    相关资源
    最近更新 更多