Well what you have to remember is that getRunningScene will return the current scene but the scene you are trying to get i.e(Game2) this is actually a layer running on the scene.
To access this layer you will need to add a tag when creating your layer originally
Then when you have your scene do a scene->getChildByTag(1);
This will give you your custom “Scene” layer
@ChrisS91
Ok, but how do I set the layer Tag when I load the scene with ccbReader from a .ccbi file like this:
auto ccNodeLoaderLibrary = NodeLoaderLibrary::newDefaultNodeLoaderLibrary();
ccNodeLoaderLibrary->registerNodeLoader(className, loader);
auto ccbReader = new cocosbuilder::CCBReader(ccNodeLoaderLibrary);
Scene* scene = ccbReader->createSceneWithNodeGraphFromFile(IGameManager::getInstance().getSceneName());
return scene;
I dont know about loading it from ccbReader but what you can try is instead of getting it by tag get the array of children attached to it then using the first one.
E.g just an example out of my head dont know if it will work
ccArray children = Scene->getChildren();
Game2 *MainLayer = (Game2*)children->objectAtIndex(0);
mainLayer->runFunction();
@ChrisS91
I also came up with the idea of children and I checked and it was “doable” However this wouldn’t look nice in code because the children count of the scene was 2. The first child was the Camera* and the second child was the Game2* scene that I actually want. Both children with Tag == -1 I could iterate through the children and set them tags i.e setTag(i++); However I decided to experiment a bit with the ccbReader and in the end I’ve done something like this:
Scene* scene = Scene::create();
auto node = ccbReader->readNodeGraphFromFile(IGameManager::getInstance().getSceneName());
node->setTag(1);
scene->addChild(node);
return scene;
And then dynamic_cast fulfilled everything
Thanks @ChrisS91! And hopefully someone else will need this in the future so above You have a working example