touchstart处理程序之后的touchmove处理程序不起作用
touchmove handler after touchstart handler wont work
我遇到了触摸移动事件的问题。当用户触摸显示器(触摸启动)时,应执行touchmove
事件处理程序和game()
,如果用户离开屏幕,则应停止所有操作。但是,collisiondetection
间隔的if条件将不起作用,因为e.pageX
和e.pageY
始终具有触摸开始的坐标,并且当用户在屏幕上移动手指(触摸移动)时不会更新它们的值。我该怎么解决这个问题?演示
$("body").on({
'touchstart mousedown': function (e) {
$(this).on('touchmove mousemove');
collisiondetection = setInterval(function() {
var xp1 = $("#n1").position();
if (e.pageY >= xp1.top && e.pageY <= xp1.top + cy * 10 && e.pageX >= xp1.left && e.pageX <= xp1.left + cx * 25) {
console.log("hit");
}
var xp2 = $("#n2").position();
if (e.pageY >= xp2.top && e.pageY <= xp2.top + cy * 10 && e.pageX >= xp2.left && e.pageX <= xp2.left + cx * 25) {
console.log("hit");
}
},10);
game();
},
'touchend mouseup': function (e) {
$(this).off('touchmove mousemove');
clearInterval(animaterects);
clearInterval(collisiondetection);
}
});
更新:如果我将其编辑为'touchstart mousedown touchmove mousemove': function (e) {
,则碰撞检测和更新坐标效果良好,但动画效果不佳。
因为当用户移动手指时,您的代码不会更新坐标。
$("body").on({
'touchstart mousedown': function (e) {
var pageX = e.pageX
var pageY = e.pageY;
$(this).on('touchmove mousemove',function(e){
pageX = e.pageX;
pageY = e.pageY;
});
collisiondetection = setInterval(function() {
var xp1 = $("#n1").position();
if (pageY >= xp1.top && pageY <= xp1.top + cy * 10 && pageX >= xp1.left && pageX <= xp1.left + cx * 25) {
console.log("hit");
}
var xp2 = $("#n2").position();
if (pageY >= xp2.top && pageY <= xp2.top + cy * 10 && pageX >= xp2.left && pageX <= xp2.left + cx * 25) {
console.log("hit");
}
},10);
game();
},
'touchend mouseup': function (e) {
$(this).off('touchmove mousemove');
clearInterval(animaterects);
clearInterval(collisiondetection);
}
});
相关文章:
- keyup事件处理程序更改焦点不适用于快速键入
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- 提示使用服务器端事件处理程序激活JavaScript
- 将事件处理程序绑定到任何可能的事件
- 正在将事件处理程序添加到不存在的类
- 在AJAX回调和呈现PIXIJS之间处理程序流时遇到了问题
- Adobe Edge:动画完成时添加onComplete处理程序
- 是否可以从输入处理程序中确定输入的类型
- 在MVVM视图模型中处理应用程序范围的元素
- 在循环中附加事件处理程序时出现浏览器性能问题
- 在同一个javascript事件处理程序中调用不同的函数
- 有没有一种方法可以让内联事件处理程序在元素创建后立即执行
- 检查事件处理程序参数
- 实现延迟的jquery更改事件处理程序
- 如何使用Python/Selenium网络驱动程序处理Angularjs/Javascript下拉列表
- 在 AJAX 完成之前,不要让其他处理程序处理
- 如何使用硒铬驱动程序处理地理位置弹出窗口
- 如何使用硒网络驱动程序处理 onblur 事件
- 是否有一种方法为chrome打包应用程序处理Http BasicAuthentication
- Ajax加载程序处理问题