From ef8f3b7127f33a3645ffe7915859eb0f24bfb146 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 30 Nov 2017 15:07:26 +0800 Subject: [PATCH] Improve hit test --- cocos2d/core/CCNode.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 802e6fa0fc..938b28bc86 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1189,20 +1189,22 @@ var Node = cc.Class({ _hitTest (point, listener) { var w = this.width, h = this.height; - var rect = cc.rect(0, 0, w, h); + var testPt; var Camera = cc.Camera; if (Camera && Camera.main && Camera.main.containsNode(this)) { - point = Camera.main.getCameraToWorldPoint(point); + testPt = Camera.main.getCameraToWorldPoint(point); } - - this.getNodeToWorldTransform(_trans); - 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; - if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) { + else { + testPt = cc.v2(point); + } + + this._updateWorldMatrix(); + math.vec2.transformMat4(testPt, testPt, this._worldMatrix); + testPt.x -= this._anchorPoint.x * this._contentSize.width; + testPt.y -= this._anchorPoint.y * this._contentSize.height; + + if (testPt.x >= 0 && testPt.y >= 0 && testPt.x <= w && testPt.y <= h) { if (listener && listener.mask) { var mask = listener.mask; var parent = this;