绑定 eventListener 问题
Bind eventListener issue
我对我的事件与你们各自的 sprite 的绑定有疑问。
这是我的代码:
var GameLayer = cc.Layer.extend({
sprite:null,
ctor:function () {
this._super();
var size = cc.winSize;
var gradient = cc.LayerGradient(cc.color(0,0,0,255),cc.color(0x46,0x82,0xB4,255));
this.addChild(gradient);
for(i=0;i<16;i++){
var tile = new MemoryTile();
this.addChild(tile,0);
tile.setPosition(49+i%4*74,400-Math.floor(i/4)*74);
}
return true;
}
});
var MemoryTile = cc.Sprite.extend({
ctor:function() {
this._super();
this.initWithFile(res.cover);
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var location = target.convertToNodeSpace(touch.getLocation());
var targetSize = target.getContentSize();
var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
if (cc.rectContainsPoint(targetRectangle, location)) {
console.log("I picked a tile!!");
}
//console.log('touche po oe: ' + targetSize);
return true;
}
});
cc.eventManager.addListener(listener.clone(), this);
}
});
所以,问题是只有第一个 sprite 仍然使用 eventListener 创建,我认为 listener.clone() 不起作用,我的任何想法做错了吗?
我认为 create 方法已被弃用,这应该可以在内存块而不是 var 侦听器和 add 侦听器中正常工作:
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: false,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var location = target.convertToNodeSpace(touch.getLocation());
var targetSize = target.getContentSize();
var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
if (cc.rectContainsPoint(targetRectangle, location)) {
console.log("I picked a tile!!");
}
//console.log('touche po oe: ' + targetSize);
return true;
}
}, this);
我对我的事件与你们各自的 sprite 的绑定有疑问。
这是我的代码:
var GameLayer = cc.Layer.extend({
sprite:null,
ctor:function () {
this._super();
var size = cc.winSize;
var gradient = cc.LayerGradient(cc.color(0,0,0,255),cc.color(0x46,0x82,0xB4,255));
this.addChild(gradient);
for(i=0;i<16;i++){
var tile = new MemoryTile();
this.addChild(tile,0);
tile.setPosition(49+i%4*74,400-Math.floor(i/4)*74);
}
return true;
}
});
var MemoryTile = cc.Sprite.extend({
ctor:function() {
this._super();
this.initWithFile(res.cover);
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var location = target.convertToNodeSpace(touch.getLocation());
var targetSize = target.getContentSize();
var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
if (cc.rectContainsPoint(targetRectangle, location)) {
console.log("I picked a tile!!");
}
//console.log('touche po oe: ' + targetSize);
return true;
}
});
cc.eventManager.addListener(listener.clone(), this);
}
});
所以,问题是只有第一个 sprite 仍然使用 eventListener 创建,我认为 listener.clone() 不起作用,我的任何想法做错了吗?
我认为 create 方法已被弃用,这应该可以在内存块而不是 var 侦听器和 add 侦听器中正常工作:
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: false,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var location = target.convertToNodeSpace(touch.getLocation());
var targetSize = target.getContentSize();
var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
if (cc.rectContainsPoint(targetRectangle, location)) {
console.log("I picked a tile!!");
}
//console.log('touche po oe: ' + targetSize);
return true;
}
}, this);