Cocos Creator关闭多点触摸的问题
一、 方案一:改写Node的派发事件
方法:改写node的派发事件,当有多个响应的时候不去进行dispatch。
在游戏开启的时候把node原来的方法:点击链接加入群聊【unity/cocos交流二群】
cc.Node.prototype.dispatchEvent ƒ (event) {
_doDispatchEvent(this, event);
cachedArray.length = 0;
}、
改为:在游戏必经的文件中调用一次;改写node的dispatchEvent的方法
DealMulityEventListener:function(){
cc.Node.maxTouchNum = 1;
cc.Node.touchNum = 0;
var __dispatchEvent__ = cc.Node.prototype.dispatchEvent;
cc.Node.prototype.dispatchEvent = function (event) {
switch (event.type) {
case 'touchstart':
if (cc.Node.touchNum < cc.Node.maxTouchNum) {
cc.Node.touchNum++;
this._canTouch = true;
__dispatchEvent__.call(this, event);
}
break;
case 'touchmove':
if (!this._canTouch && cc.Node.touchNum < cc.Node.maxTouchNum) {
this._canTouch = true;
cc.Node.touchNum++;
}
if (this._canTouch) {
__dispatchEvent__.call(this, event);
}
break;
case 'touchend':
if (this._canTouch) {
this._canTouch = false;
cc.Node.touchNum--;
__dispatchEvent__.call(this, event);
}
break;
case 'touchcancel':
if (this._canTouch) {
this._canTouch = false;
cc.Node.touchNum--;
__dispatchEvent__.call(this, event);
}
break;
default:
__dispatchEvent__.call(this, event);
}
};
},
二、 方案二:修改不同设备的配置
ios上很简单的在AppController.mm里
[eaglView setMultipleTouchEnabled:YES]
设置为NO,就是单点触控了,无需更改cocos底层代码;
android上的做法是找到项目所引用的cocos引擎文件:
Cocos2dxGLSurfaceView.java,找到onTouchEvent方法,在switch语句里的
MotionEvent.ACTION_POINTER_DOWN
MotionEvent.ACTION_DOWN
这两个case 的第一行都写上:
if (pointerNumber > 1) return false;
当检测到当前触控点的数量大于1时,就不让再点击屏幕。
三、 方案三:代码逻辑控制
一个全局可访问的变量用来记录 touch id
在 touch start 回调中记录当前的 touch id,在 touch end 中删除当前的 touch id
如果当前全局的 touch id 存在的话,不响应任何事件
不过这个对于 button 好像没办法,因为 button 的事件监听器是引擎内部注册的。
例如:尝试写逻辑来屏蔽多点触控

期待Cocos Creator以后可以提供一个全局的开关,用来开启或者关闭多点触摸。
四、 方案四:修改修改CCNode.js文件
如何屏蔽多点触控: 修改CCNode.js文件,增加下面加粗代码
var _touchStartHandler = function (touch, event) {
var pos = touch.getLocation();
var node = this.owner;
if(cc.currentTouchNode && cc.currentTouchNode.isValid
&& cc._currentTouchNode.activeInHierarchy){
return;
}
if (node._hitTest(pos, this)) {
if (CC_JSB) {
event = Event.EventTouch.pool.get(event);
}
event.type = EventType.TOUCH_START;
event.touch = touch;
event.bubbles = true;
node.dispatchEvent(event);
if (CC_JSB) {
event.touch = null;
event._touches = null;
Event.EventTouch.pool.put(event);
}
cc._currentTouchNode = node;
return true;
}
return false;
};
var _touchEndHandler = function (touch, event) {
cc._currentTouchNode = null;
};
var _touchCancelHandler = function (touch, event) {
cc._currentTouchNode = null;
};
以上参考是论坛群友给的方案. 欢迎验证反馈。点击链接加入群聊【unity/cocos交流二群】