From d276e8eabb1757d6d7361c41780f6e77f81765e6 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 24 Oct 2016 02:42:11 +0800 Subject: [PATCH] Fix X5 touch issue by adding TOUCH_TIMEOUT --- cocos2d/core/event-manager/CCTouch.js | 1 + cocos2d/core/platform/CCInputManager.js | 23 +++++++++++++++++++++-- cocos2d/core/platform/CCMacro.js | 24 +++++++++++++++++++++++- cocos2d/core/platform/CCSys.js | 16 +++++++++++++++- jsb/jsb-etc.js | 4 ++++ 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/event-manager/CCTouch.js b/cocos2d/core/event-manager/CCTouch.js index 8261bd1cd8..4e99ca547e 100644 --- a/cocos2d/core/event-manager/CCTouch.js +++ b/cocos2d/core/event-manager/CCTouch.js @@ -33,6 +33,7 @@ * @param {Number} id */ cc.Touch = cc._Class.extend(/** @lends cc.Touch# */{ + _lastModified: 0, _point:null, _prevPoint:null, _id:0, diff --git a/cocos2d/core/platform/CCInputManager.js b/cocos2d/core/platform/CCInputManager.js index b8499550e2..7337459c45 100644 --- a/cocos2d/core/platform/CCInputManager.js +++ b/cocos2d/core/platform/CCInputManager.js @@ -23,6 +23,10 @@ THE SOFTWARE. ****************************************************************************/ +var macro = require('./CCMacro'); + +var TOUCH_TIMEOUT = macro.TOUCH_TIMEOUT; + /** *

* This class manages all events of input. include: touch, mouse, accelerometer, keyboard
@@ -55,12 +59,21 @@ var inputManager = /** @lends cc.inputManager# */{ _getUnUsedIndex: function () { var temp = this._indexBitsUsed; + var now = cc.sys.now(); for (var i = 0; i < this._maxTouches; i++) { if (!(temp & 0x00000001)) { this._indexBitsUsed |= (1 << i); return i; } + else { + var touch = this._touches[i]; + if (now - touch._lastModified > TOUCH_TIMEOUT) { + this._removeUsedIndexBit(i); + delete this._touchesIntegerDict[touch.getID()]; + return i; + } + } temp >>= 1; } @@ -84,7 +97,9 @@ var inputManager = /** @lends cc.inputManager# */{ * @param {Array} touches */ handleTouchesBegin: function (touches) { - var selTouch, index, curTouch, touchID, handleTouches = [], locTouchIntDict = this._touchesIntegerDict; + var selTouch, index, curTouch, touchID, + handleTouches = [], locTouchIntDict = this._touchesIntegerDict, + now = cc.sys.now(); for(var i = 0, len = touches.length; i< len; i ++){ selTouch = touches[i]; touchID = selTouch.getID(); @@ -98,6 +113,7 @@ var inputManager = /** @lends cc.inputManager# */{ } //curTouch = this._touches[unusedIndex] = selTouch; curTouch = this._touches[unusedIndex] = new cc.Touch(selTouch._point.x, selTouch._point.y, selTouch.getID()); + curTouch._lastModified = now; curTouch._setPrevPoint(selTouch._prevPoint); locTouchIntDict[touchID] = unusedIndex; handleTouches.push(curTouch); @@ -116,7 +132,9 @@ var inputManager = /** @lends cc.inputManager# */{ * @param {Array} touches */ handleTouchesMove: function(touches){ - var selTouch, index, touchID, handleTouches = [], locTouches = this._touches; + var selTouch, index, touchID, + handleTouches = [], locTouches = this._touches, + now = cc.sys.now(); for(var i = 0, len = touches.length; i< len; i ++){ selTouch = touches[i]; touchID = selTouch.getID(); @@ -129,6 +147,7 @@ var inputManager = /** @lends cc.inputManager# */{ if(locTouches[index]){ locTouches[index]._setPoint(selTouch._point); locTouches[index]._setPrevPoint(selTouch._prevPoint); + locTouches[index]._lastModified = now; handleTouches.push(locTouches[index]); } } diff --git a/cocos2d/core/platform/CCMacro.js b/cocos2d/core/platform/CCMacro.js index b101eafebe..6f91173a9d 100644 --- a/cocos2d/core/platform/CCMacro.js +++ b/cocos2d/core/platform/CCMacro.js @@ -1959,7 +1959,29 @@ cc.macro = { * @readonly */ // Editors do not need to cache fix bug for https://github.com/cocos-creator/fireball/issues/3079 - ENABLE_GL_STATE_CACHE: CC_EDITOR ? 0 : 1 + ENABLE_GL_STATE_CACHE: CC_EDITOR ? 0 : 1, + + /** + * !#en + * The timeout to determine whether a touch is no longer active and should be removed. + * The reason to add this timeout is due to an issue in X5 browser core, + * when X5 is presented in wechat on Android, if a touch is glissed from the bottom up, and leave the page area, + * no touch cancel event is triggered, and the touch will be considered active forever. + * After multiple times of this action, our maximum touches number will be reached and all new touches will be ignored. + * So this new mechanism can remove the touch that should be inactive if it's not updated during the last 5000 milliseconds. + * Though it might remove a real touch if it's just not moving for the last 5 seconds which is not easy with the sensibility of mobile touch screen. + * You can modify this value to have a better behavior if you find it's not enough. + * !#zh + * 用于甄别一个触点对象是否已经失效,并且可以被移除的延时时长 + * 添加这个时长的原因是 X5 内核在微信浏览器中出现的一个 bug。 + * 在这个环境下,如果用户将一个触点从底向上移出页面区域,将不会触发任何 touch cancel 或 touch end 事件,而这个触点会被永远当作停留在页面上的有效触点。 + * 重复这样操作几次之后,屏幕上的触点数量将达到我们的事件系统所支持的最高触点数量,之后所有的触摸事件都将被忽略。 + * 所以这个新的机制可以在触点在一定时间内没有任何更新的情况下视为失效触点并从事件系统中移除。 + * 当然,这也可能移除一个真实的触点,如果用户的触点真的在一定时间段内完全没有移动(这在当前手机屏幕的灵敏度下会很难)。 + * 你可以修改这个值来获得你需要的效果,默认值是 5000 毫秒。 + * @property {Number} TOUCH_TIMEOUT + */ + TOUCH_TIMEOUT: 5000 }; /** diff --git a/cocos2d/core/platform/CCSys.js b/cocos2d/core/platform/CCSys.js index 03ce765d18..362295641b 100644 --- a/cocos2d/core/platform/CCSys.js +++ b/cocos2d/core/platform/CCSys.js @@ -933,8 +933,22 @@ sys.dump = function () { * @method openURL * @param {String} url */ -sys.openURL = function(url){ +sys.openURL = function (url) { window.open(url); }; +/** + * Get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. + * @method now + * @return {Number} + */ +sys.now = function () { + if (Date.now) { + return Date.now(); + } + else { + return +(new Date); + } +}; + module.exports = sys; diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index b4bb28d9f9..fa07d57430 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -25,6 +25,10 @@ 'use strict'; +cc.sys.now = function () { + return Date.now(); +}; + var NORMALIZE_RE = /[^\.\/]+\/\.\.\//; // cc.path