From cc1c03248bfd0e4ea42042274cca62d32cb0c471 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 18 Jan 2018 18:34:10 +0800 Subject: [PATCH 1/2] Fix hit test on rotated nodes --- cocos2d/core/CCNode.js | 21 +++++++++++-------- cocos2d/core/base-nodes/CCSGNode.js | 2 +- cocos2d/core/value-types/CCAffineTransform.js | 13 ++++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index dde008c0cb..cc4f473103 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1357,20 +1357,23 @@ var Node = cc.Class({ _hitTest (point, listener) { var w = this.width, - h = this.height; - var rect = cc.rect(0, 0, w, h); + h = this.height, + pt = point; var Camera = cc.Camera; if (Camera && Camera.main && Camera.main.containsNode(this)) { - point = Camera.main.getCameraToWorldPoint(point); + pt = Camera.main.getCameraToWorldPoint(pt); } - var trans = this.getNodeToWorldTransform(); - cc._rectApplyAffineTransformIn(rect, trans); - var left = point.x - rect.x, - right = rect.x + rect.width - point.x, - bottom = point.y - rect.y, - top = rect.y + rect.height - point.y; + var trans = this.getWorldToNodeTransform(); + pt = cc.pointApplyAffineTransform(pt, trans); + pt.x += this._anchorPoint.x * w; + pt.y += this._anchorPoint.y * h; + + var left = pt.x, + right = w - pt.x, + bottom = pt.y, + top = h - pt.y; if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) { if (listener && listener.mask) { var mask = listener.mask; diff --git a/cocos2d/core/base-nodes/CCSGNode.js b/cocos2d/core/base-nodes/CCSGNode.js index 452643fe77..3905a7e4f1 100644 --- a/cocos2d/core/base-nodes/CCSGNode.js +++ b/cocos2d/core/base-nodes/CCSGNode.js @@ -1716,7 +1716,7 @@ _ccsg.Node = cc.Class({ * @return {cc.AffineTransform} */ getWorldToNodeTransform: function () { - return cc.affineTransformInvert(this.getNodeToWorldTransform()); + return cc.affineTransformInvertIn(this.getNodeToWorldTransform()); }, /** diff --git a/cocos2d/core/value-types/CCAffineTransform.js b/cocos2d/core/value-types/CCAffineTransform.js index 27c2c4fdf5..feb0b74370 100644 --- a/cocos2d/core/value-types/CCAffineTransform.js +++ b/cocos2d/core/value-types/CCAffineTransform.js @@ -374,6 +374,19 @@ cc.affineTransformInvert = function (t) { tx: determinant * (t.c * t.ty - t.d * t.tx), ty: determinant * (t.b * t.tx - t.a * t.ty)}; }; +cc.affineTransformInvertIn = function (t) { + var a = t.a, b = t.b, c = t.c, d = t.d; + var determinant = 1 / (a * d - b * c); + var tx = t.tx, ty = t.ty; + t.a = determinant * d; + t.b = -determinant * b; + t.c = -determinant * c; + t.d = determinant * a; + t.tx = determinant * (c * ty - d * tx); + t.ty = determinant * (b * tx - a * ty); + return t; +}; + /** * !#en Put the invert transform of an AffineTransform object into the out AffineTransform object. * !#zh 求逆矩阵并存入用户传入的矩阵对象参数。 From a9d0a08271c50aed0e9cfb13b103f0b0d321974f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 17 Jan 2018 14:52:15 +0800 Subject: [PATCH 2/2] Fix recursive schedule task issue --- cocos2d/core/CCScheduler.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js index 6000748f9a..9c3ec89076 100644 --- a/cocos2d/core/CCScheduler.js +++ b/cocos2d/core/CCScheduler.js @@ -160,6 +160,7 @@ HashTimerEntry.put = function (entry) { * @extends cc.Class */ function CallbackTimer () { + this._lock = false; this._scheduler = null; this._elapsed = -1; this._runForever = false; @@ -176,6 +177,7 @@ function CallbackTimer () { var proto = CallbackTimer.prototype; proto.initWithCallback = function (scheduler, callback, target, seconds, repeat, delay) { + this._lock = false; this._scheduler = scheduler; this._target = target; this._callback = callback; @@ -241,8 +243,10 @@ proto.getCallback = function(){ }; proto.trigger = function () { - if (this._target && this._callback){ + if (this._target && this._callback) { + this._lock = true; this._callback.call(this._target, this._elapsed); + this._lock = false; } }; @@ -256,7 +260,7 @@ CallbackTimer.get = function () { return _timers.pop() || new CallbackTimer(); }; CallbackTimer.put = function (timer) { - if (_timers.length < MAX_POOL_SIZE) { + if (_timers.length < MAX_POOL_SIZE && !timer._lock) { timer._scheduler = timer._target = timer._callback = null; _timers.push(timer); } @@ -572,6 +576,10 @@ cc.Scheduler = cc._Class.extend({ timer = CallbackTimer.get(); timer.initWithCallback(this, callback, target, interval, repeat, delay); element.timers.push(timer); + + if (this._currentTarget === element && this._currentTargetSalvaged) { + this._currentTargetSalvaged = false; + } }, /**