【发布时间】:2016-01-19 11:35:51
【问题描述】:
我有以下代码用于检查精灵的触摸:
void SpriteBlock::addEvents()
{
auto listener = cocos2d::EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
{
Vec2 p = touch->getLocation();
Rect rect = this->getBoundingBox();
if(rect.containsPoint(p))
{
return true; // to indicate that we have consumed it.
}
return false; // we did not consume this event, pass thru.
};
listener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
{
SpriteBlock::touchEvent(touch);
};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);
}
void SpriteBlock::touchEvent(cocos2d::Touch* touch)
{
}
这似乎工作正常,但即使在精灵被销毁后,它仍然会被触发(如果我点击精灵存在的最后一个地方),它会崩溃:
"
线程 1:EXC_BAD_ACCESS(代码=2,地址=0x7....)
"
在以下行:
Rect rect = this->getBoundingBox();
现在我似乎很清楚 sprite 已被销毁,因为我的析构函数设置为在触发时显示日志消息(确实如此):
SpriteBlock::~SpriteBlock() {
CCLOG("Block destroyed");
}
那么这里的问题是什么?为什么听者没有被我的精灵摧毁?我通过执行以下操作来销毁我的精灵:
mysprite->removeFromParent();
当我创建精灵时,我不存储任何引用。我只是将它添加到场景的主层,所以它不应该保留。我使用以下方法创建它:
SpriteBlock *block = SpriteBlock::create();
当精灵被移除时,如何确保触摸监听器也被移除?
【问题讨论】: