Hi,
I made a sample scene for testing the touch events, and this include a swallow layer, so try it out to see if it can helps you resolve your problem.
It’s true that the current event dispatcher is sometimes tricky and not very easy to use. Don’t worry, the main improvement in the next release will be the new event dispatcher. It will be totally recoded, and you may set listeners for every node, not just the layers.
Huabin
var Rect = cc.DrawNode.extend({
corners: null,
ctor: function(rect, color) {
this._super();
this.init();
this.corners = [cc.p(0, 0),
cc.p(0, rect.height),
cc.p(rect.width, rect.height),
cc.p(rect.width, 0)];
this.drawPoly(this.corners, color, 0, color);
this.setAnchorPoint(0, 0);
this.setPosition(rect._origin);
this.setContentSize(rect._size);
},
changeColor: function(color) {
this.clear();
this.drawPoly(this.corners, color, 0, color);
}
});
var TestLayer = cc.Layer.extend({
name: null,
nameLabel: null,
target: null,
activeColor: cc.c4f(1, 0, 0, 1),
color: null,
touching: false,
ctor: function(name, priority) {
this._super();
this.name = name || "";
this.nameLabel = cc.LabelTTF.create(this.name, "Helvetica", 13, cc.size(260, 30), cc.TEXT_ALIGNMENT_RIGHT);
this.nameLabel.setAnchorPoint(1, 1);
this.setTouchEnabled(true);
this.setTouchPriority(priority);
},
setTouchTarget: function(rect, color) {
this.color = color;
this.target = new Rect(rect, color);
this.addChild(this.target);
this.nameLabel.setPosition(cc.p(rect.x+rect.width, rect.y+rect.height));
this.addChild(this.nameLabel);
},
testTouch: function(touch) {
var tpos = touch.getLocation(), tx = tpos.x, ty = tpos.y,
origin = this.target.getPosition(), ox = origin.x, oy = origin.y,
size = this.target.getContentSize(), w = size.width, h = size.height;
if(tx > ox && tx < ox+w && ty > oy && ty < oy+h)
return true;
else return false;
},
onTouchesBegan: function(touches) {
if(this.testTouch(touches[0])) {
this.touching = true;
this.target && this.target.changeColor(this.activeColor);
(logTest || cc.log)(this.name + ": Touch began");
}
},
onTouchesMoved: function(touches) {
},
onTouchesEnded: function() {
if(this.touching) {
this.target && this.target.changeColor(this.color);
(logTest || cc.log)(this.name + ": Touch ended");
this.touching = false;
}
},
onTouchBegan: function(touch) {
if(this.testTouch(touch)) {
this.target && this.target.changeColor(this.activeColor);
(logTest || cc.log)(this.name + ": Touch began");
return true;
}
else return false;
},
onTouchMoved: function(touch) {
},
onTouchEnded: function(touch) {
if(this.testTouch(touch)) {
this.target && this.target.changeColor(this.color);
(logTest || cc.log)(this.name + ": Touch ended");
return true;
}
else return false;
}
});
var SwallowLayer = TestLayer.extend({
ctor: function(name, priority) {
this._super(name, priority);
this.setTouchMode(cc.TOUCH_ONE_BY_ONE);
}
});
var PropaLayer = TestLayer.extend({
ctor: function(name, priority) {
this._super(name, priority);
this.setTouchMode(cc.TOUCH_ONE_BY_ONE);
this._isTouchEnabled = true;
cc.registerTargetedDelegate(this._touchPriority, false, this);
}
});
var TouchPropagationTest = cc.Scene.extend({
ctor: function() {
this._super();
var winSize = cc.Director.getInstance().getWinSize(), w = winSize.width, h = winSize.height;
var propaLayer1 = new PropaLayer("Propagation Layer Priority 5", 5);
propaLayer1.setTouchTarget(cc.rect(0, 0, w, h), cc.c4f(0.97, 0.76, 0.8, 1));
var standardLayer1 = new TestLayer("Standard Layer Priority 4", 4);
standardLayer1.setTouchTarget(cc.rect(0, 0, w*0.8, h*0.8), cc.c4f(0.96, 0.36, 0.18, 1));
var swallowLayer = new SwallowLayer("Swallow Layer Priority 3", 3);
swallowLayer.setTouchTarget(cc.rect(w*0.1, h*0.1, w*0.6, h*0.6), cc.c4f(0.45, 0.29, 0.22, 1));
var propaLayer2 = new PropaLayer("Propagation Layer Priority 2", 2);
propaLayer2.setTouchTarget(cc.rect(0, 0, w*0.4, h*0.4), cc.c4f(0.74, 0.76, 0.25, 1));
var standardLayer2 = new TestLayer("Standard Layer Priority 1", 1);
standardLayer2.setTouchTarget(cc.rect(0, 0, w*0.2, h*0.2), cc.c4f(0.4, 0.73, 0.7, 1));
this.addChild(propaLayer1);
this.addChild(standardLayer1);
this.addChild(swallowLayer);
this.addChild(propaLayer2);
this.addChild(standardLayer2);
}
});
TouchPropagationTest.create = function() {
return new TouchPropagationTest();
}