新手引导主要就是修改touchMask拦截事件的node的_hitTest方法.mask反向遮罩
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// default: null, // The default value will be used only when the component attaching
// to a node for the first time
// url: cc.Texture2D, // optional, default is typeof default
// serializable: true, // optional, default is true
// visible: true, // optional, default is true
// displayName: 'Foo', // optional
// readonly: false, // optional, default is false
// },
// ...
},
onLoad: function () {
},
onEnable: function () {
this.touchMask.on(cc.Node.EventType.TOUCH_START,this.xxxx,this);
this.touchMask._hitTest = this.customHitTest;
},
onDisable: function () {
},
onDestroy: function () {
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
customHitTest : function(point, listener) {
// console.log("---->customHitTest");
var w = this.width,
h = this.height;
var rect = cc.rect(0, 0, w, h);
var Camera = cc.Camera;
if (Camera && Camera.main && Camera.main.containsNode(this)) {
point = Camera.main.getCameraToWorldPoint(point);
}
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;
if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) {
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) {
var comp = parent.getComponent(cc.Mask);
return (comp && comp.enabledInHierarchy) ? !comp._hitTest(point) : true;//反向mask判断hitTest修改
}
// mask parent no longer exists
else {
listener.mask = null;
return true;
}
}
else {
return true;
}
}else{
return false;
}
}
});