Director::getInstance()->getRunningScene() returns NULL

Node::getScene() is different. It will return the root node of the hierarchy dynamically cast as a Scene. So if a Node has been added to a hierarchy that has a Scene as it’s root then getScene() will return that scene object regardless of whether it’s running or not. As Node::addChild() is immediate, this function will always return what you expect.

Scene* Node::getScene() const
{
    if (!_parent)
        return nullptr;
    
    auto sceneNode = _parent;
    while (sceneNode->_parent)
    {
        sceneNode = sceneNode->_parent;
    }

    return dynamic_cast<Scene*>(sceneNode);
}
1 Like