自定义一个Sprite,然后添加如下代码。
EventListenerTouchOneByOne * listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(GameBlock::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(GameBlock::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(GameBlock::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
```
然后在gamaLayer用for循环创建并添加Sprite
for (int i = 0; i < 4; i ++) {
GameBlock *item = NULL;
if (index == i) {
item = GameBlock::createBlockWithTexture2d(_greenBatchNode->getTexture());
} else {
item = GameBlock::createBlockWithTexture2d(_redBatchNode->getTexture());
}
}
float offsetx = i * item->getContentSize().width + item->getContentSize().width / 2;
item->setPosition(Point(offsetx, item->getContentSize().height / 2));
this->addChild(item);
```
然后再gamelayer点击每一个Sprite,结果所有自定义sprite事件都响应了。
这个怎么破,我要点击一个,每个对象处理响应的事件就行,为啥所有添加在gamelayer上的自定sprite都响应了触摸事件,是我代码写的有问题吗?
如果是cocos2d-x 本来就这样处理,那我要吐槽。。。太不方便了,,我创建了多个自定义的Sprite,代码块应该是单独的才对,点击那个Sprite,那个响应事件,现在所有都想要了,各位高人,看看是哪里的问题,多谢了。