CCTouchesBegan of Old Scene gets called eventhough scene has already been replaced

Hi Everyone,
I’m having a problem with my cocos2d-x project.
In my project I have a GameManager class that replaces one scene with another.
e.g.
if (CCDirector::sharedDirector()->getRunningScene() == NULL) {
CCDirector::sharedDirector()->runWithScene(sceneToRun);
}else{
CCDirector::sharedDirector()->replaceScene(sceneToRun);
}

So I have 2 scenes, each having its own layer.
When I run the first scene and touch the screen, the ccTouchesBegan of the first scene gets called, which is the right response to be expected.
I then click a CCMenuItem that replaces the current scene with the old scene. Therefore, in theory, the first scene and layer should be replaced with the new second scene and its corresponding layer.
Outwardly, everything seems to be A-OK. The CCTextFieldTTFs that I put on the 2nd scene are all there… Then I click somewhere on the screen that is outside of any of the textfield’s rect….CCTouchesBegan DOES get called….but guess what?? The CCTouchesBegan of the first scene is the one getting called and NOT the new second scene’s CCTouchesBegan.
Not only this, I then click on a CCMenuItem that replaces this current second scene with the first scene again so I can go back to where I started. This works fine….
and then I click on the screen again, the CCTouchesBegan of the first scene gets called …but what is not to be expected but still happens is the second scene’s parent class’s CCTouchesBegan also gets called after it……This would cause the app to crash by this point……
What’s going oN??
How do I solve this???
Thank you very much…

ccTouchesBegan belonging to node of scene not currently on the stage being called means touch handler of that node wasn’t correctly unregistered. Unregistration usually happens in onExit() which is called recursively for all the nodes starting from the root node (the scene itself). Please make sure you didn’t override onExit() in your scenes/layers or, if you did, you call parent class’ onExit() in it.

Hi Igor,

You are spot on in your advice.
Thank you very much :slight_smile: