I have this code for cocos2d-x 3.x:
void MainMenu::StartGame(cocos2d::Ref* pSender)
{
auto director = Director::getInstance();
auto newScene = Scene::create();
director->replaceScene(newScene); //run
Scene *maybeNewScene = director->getRunningScene();
CCASSERT ( maybeNewScene == newScene , "...") ; //Assert Fail
auto hudLayer= HUD::create();
hudLayer->setPosition(Vec2::ZERO);
scene->addChild(hudLayer, Z_NORMAL);
}
But When I get runnign scene by director->getRunningScene()
, It gives me the scene which was running before calling director->replaceScene()
. (the scene which is going to be destructed soon)
When I checked replaceScene()
function, This function places newScene
in some varibale called _nextScene
but not assigned it to _runningScene
.
Question: How can I access newScene
, just some lines further? ( for example in HUD::init()
)
Answer
Quoted from @mannewalis
The reason your scene is not the running scene is because it it only gets set the next frame, or even later if there is a transition running.
You could add you HUD create code in the new scene onEnter method, I believe runningScene will be set by then, but possibly not if there is a transition running, in which case you should add your HUD create code to the onEnterTransitionDidFinish method. You can even use a lambda method to do it by calling setonEnterTransitionDidFinishCallback.
No comments:
Post a Comment