popScene crashes app

In AppDelegate, I use Director::getInstance()-runWithScene(SceneA::createScene())
In the init of SceneA, if some conditions are met, I use Director::getInstance()->pushScene(SceneB::createScene())
In the init of SceneB, if some conditions fail, I use Director::getInstance()->popScene()
It is here that the app crashes.

In logcat, I get:

D/cocos2d-x debug info: Assert failed: running scene should not null
E/cocos2d-x assert: ../../../../../../cocos2d/cocos/base/CCDirector.cpp function:popScene line:919
D/cocos2d-x debug info: Assert failed: no objects added
E/cocos2d-x assert: ../../../../../../cocos2d/cocos\base/CCVector.h function:popBack line:366

Any help would be appreciated.

SceneB is being popped before it’s added to the scene stack, so in effect you’re popping SceneA instead. The reason for this is that you’re doing the scene popping in the SceneB::init() method, which is still in the createScene chain of calls, so it hasn’t yet been added to the scene stack at this point. A debug session would have shown you this very quickly.

You need to re-design your code to move that conditional somewhere else, like in update() etc…

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.