android 关闭多点触控_Cocos Creator关闭多点触摸的问题
一、 方案一:改寫Node的派發事件
方法:改寫node的派發事件,當有多個響應的時候不去進行dispatch。
在游戲開啟的時候把node原來的方法:
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;
};
以上參考是論壇群友給的方案. 歡迎驗證反饋。
正在跳轉?jq.qq.com總結
以上是生活随笔為你收集整理的android 关闭多点触控_Cocos Creator关闭多点触摸的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 调用ajax_[WEB篇]-AJAX-0
- 下一篇: react更改路由入参_react怎么路