微信小游戏上单点触控问题(cocosd-js)

##问题描述

  • 假定现在有两个节点A和B,两者都注册了TOUCH_ONE_BY_ONE的触摸事件监听。发布到微信小游戏后, 当一个手指按住其中一个节点的时候,另一个手指点击另外一个节点也能响应事件。需求是当第一个触摸没有结束后面的触摸事件注册了TOUCH_ONE_BY_ONE的节点都不要响应。有什么办法实现?(原生平台没有这个问题,引擎版本是cocos2d-x3.16)

在整个场景的最上层 加一个layer吞噬掉多余的点击事件
getSwallowTouchesLayer: function() {
var self = this;
var curTouch = null;
var layer = new cc.Layer();
var listener = cc.EventListener.create({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan:function(touch, event) {
if(!curTouch){
listener.setSwallowTouches(false);
curTouch = touch;
}else
listener.setSwallowTouches(true);
return true;
},
onTouchEnded:function(touch, event) {
if(touch === curTouch){
listener.setSwallowTouches(false);
curTouch = null;
}
},
onTouchCancelled:function (touch, event) {
if(touch === curTouch){
listener.setSwallowTouches(false);
curTouch = null;
}
}
});
cc.eventManager.addListener(listener, layer);
return layer;
}