So i have a main menu scene that has a menu item that launches an in game scene:
auto scene = MeadowScene::createScene();
Director::getInstance()->replaceScene(TransitionFade::create(1, scene));
the MainScene is started in AppDelegate.cpp like this:
auto scene = MainScene::createScene();
Director::getInstance()->runWithScene(scene);
I have several event listeners for many interactive sprites on the MeadowScene. The Event listener looks something like this:
bool Snail::addTouchEvent() {
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(false);
listener->onTouchBegan = [&] (Touch* touch, Event* event){
listener->setSwallowTouches(true);
// get the sprite from the event object
auto sprite = event->getCurrentTarget();
Vec2 pt = touch->getLocation();
auto recTemp = sprite->getBoundingBox();
if (recTemp.containsPoint(sprite->getParent()->convertToNodeSpace(pt) )) {
Director::getInstance()->getEventDispatcher()->pauseEventListenersForTarget(sprite);
this->animate(sprite);
}
return true;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this->sprite);
return true;
}
When the MeadowScene is the first scene to load then everything works fine however when the MainScene replaces itself with MeadowScene then I start having issues. When the different interactive elements get touched I get this error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x178)
That happens in this function:
void EventListenerTouchOneByOne::setSwallowTouches(bool needSwallow)
{
_needSwallow = needSwallow; // Thread 1: EXC_BAD_ACCESS (code=1, address=0x178)
}
Any idea why this might be happening? is there a step that I am missing in switching scenes?