Hi,
I am trying to use Director::getInstance()->getRunningScene() multiple places but i get NULL at all places.
after onEnter() I am trying to use and gets Null.
on my custom widget call created from reference with Widget Dev Link gives NULL while trying to access the current scene . I am displaying this widget on click of button from my scene.
Following is my code which always print NULL as output.
HelloWorld* h_scene = dynamic_cast<HelloWorld*>(Director::getInstance()->getRunningScene());
if(h_scene != NULL)
{
CCLOG("***%s",h_scene->getDescription().c_str());
auto vectorP = h_scene->getChildren();
for(auto n : vectorP)
{
CCLOG("---------");
CCLOG("%s",n->getName().c_str());
CCLOG("%s",n->getDescription().c_str());
}
CCLOG("***%zd",h_scene->getChildrenCount());
}else
{
CCLOG("**Hello World null");
}
Hi,
I had this some sort of issue myself. Not sure if it applies to you or not.So if it is returning null , it means that no single frame has been outputted. So once single frame has been outputted it will work. My solution add flag (true) and call this method in update(), then set flag to false.
I have already gone through the post and as quoted by him no single frame is outputted it returns NULL. In my case the frame is already on the screen as I am trying to call this from a button call on new node so the scene is not in preparation state.
In My main Scene Call also I am calling this in onEnter() after adding root as child to scene it may happen during that time scene is not played but I am not sure that is the case with first one.
Yes sure this are the files…
The Scene is normal defualt scene file of defult cocos create new with node1 folder being my custom node’s class file.
I am accessing in onEnter() of scene as well as onClick() in node1. Cocos Test.zip (9.4 KB)
@padmnabh
the problem is that you are doing a dynamic_cast<HelloWorld*>.
And HelloWorld is just a child of Scene… in your code you have this:
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
Pay attention to that code: First you create a Scene, then a HelloWorld node, and then you add HelloWorld as a child of Scene.
So, dynamic_cast will always fail.
Ok So what is the right procedure to create scene so that I can retrieve the Scene object from other nodes.
I have implemented this as it’s found in cocos2d-x test project.