diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 0f43861a57..2ae8ec2837 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -124,7 +124,7 @@ var _touchStartHandler = function (touch, event) { var pos = touch.getLocation(); var node = this.owner; - if (node._hitTest(pos)) { + if (node._hitTest(pos, this)) { event.type = EventType.TOUCH_START; event.touch = touch; event.bubbles = true; @@ -144,7 +144,7 @@ var _touchEndHandler = function (touch, event) { var pos = touch.getLocation(); var node = this.owner; - if (node._hitTest(pos)) { + if (node._hitTest(pos, this)) { event.type = EventType.TOUCH_END; } else { @@ -159,7 +159,7 @@ var _mouseDownHandler = function (event) { var pos = event.getLocation(); var node = this.owner; - if (node._hitTest(pos)) { + if (node._hitTest(pos, this)) { event.type = EventType.MOUSE_DOWN; node.dispatchEvent(event); event.stopPropagation(); @@ -168,7 +168,7 @@ var _mouseDownHandler = function (event) { var _mouseMoveHandler = function (event) { var pos = event.getLocation(); var node = this.owner; - if (node._hitTest(pos)) { + if (node._hitTest(pos, this)) { event.stopPropagation(); if (!this._previousIn) { event.type = EventType.MOUSE_ENTER; @@ -188,13 +188,31 @@ var _mouseUpHandler = function (event) { var pos = event.getLocation(); var node = this.owner; - if (node._hitTest(pos)) { + if (node._hitTest(pos, this)) { event.type = EventType.MOUSE_UP; node.dispatchEvent(event); event.stopPropagation(); } }; +var _searchMaskParent = function (node) { + if (cc.Mask) { + var index = 0; + var mask = null; + for (var curr = node; curr; curr = curr.parent, ++index) { + mask = curr.getComponent(cc.Mask); + if (mask) { + return { + index: index, + node: curr + }; + } + } + } + + return null; +}; + /** * Class of all entities in Fireball scenes. @@ -828,6 +846,7 @@ var Node = cc.Class({ event: cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches: true, owner: this, + mask: _searchMaskParent(this), onTouchBegan: _touchStartHandler, onTouchMoved: _touchMoveHandler, onTouchEnded: _touchEndHandler @@ -841,6 +860,7 @@ var Node = cc.Class({ event: cc.EventListener.MOUSE, _previousIn: false, owner: this, + mask: _searchMaskParent(this), onMouseDown: _mouseDownHandler, onMouseMove: _mouseMoveHandler, onMouseUp: _mouseUpHandler @@ -894,7 +914,7 @@ var Node = cc.Class({ } }, - _hitTest: function (point) { + _hitTest: function (point, listener) { var apx = this._anchorPoint.x, apy = this._anchorPoint.y, w = this.width, @@ -907,7 +927,23 @@ var Node = cc.Class({ bottom = point.y - rect.y, top = rect.y + rect.height - point.y; if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) { - return true; + if (listener && listener.mask) { + var mask = listener.mask; + var parent = this; + for (var i = 0; parent && i < mask.index; ++i, parent = parent.parent) {} + // find mask parent, should hit test it + if (parent === mask.node) { + return parent._hitTest(point); + } + // mask parent no longer exists + else { + listener.mask = null; + return true; + } + } + else { + return true; + } } else { return false; diff --git a/cocos2d/core/components/CCMask.js b/cocos2d/core/components/CCMask.js index 0dcbe300aa..57cbb387b6 100644 --- a/cocos2d/core/components/CCMask.js +++ b/cocos2d/core/components/CCMask.js @@ -70,10 +70,6 @@ var Mask = cc.Class({ this.node.off('anchor-changed',this._onAnchorChanged,this); }, - onDestroy: function () { - - }, - _onContentSizeChanged: function() { if(this._clippingStencil) { this._clippingStencil.setContentSize(this.node._contentSize); @@ -96,6 +92,6 @@ var Mask = cc.Class({ } }, - }); +}); - cc.Mask = module.exports = Mask; +cc.Mask = module.exports = Mask;