Global Z order of children

I really like the new “global Z order” feature and I am using to put characters at one depth and their shadows at another depth (so that their shadows are also under all other characters). But the problem is that now all the GUI is also “under” the characters (because their global Z by default is 0). To move all interface up, I have to set global Z to each and every interface element and that is almost impossible in my situation.

The problem is that even if I set global Z for parent, children get added to scene at global Z = 0, they are below its parent. I suggest adding an attribute to force all children to have global Z greater or equal to its parent’s Z.

Or maybe there is a different solution for my problem?

@elvman

I think you can subclass an Scene, override the setGlobalZOrder of your own Scene class.

In your own setGlobalZOrder function, you could make all its children’s global Z order greater than itself.

The same goes for Sprite, Layer and Node.

Here is the code I wrote to fix this:

void setGlobalZOrderRecursive(cocos2d::Node *parent, float z)
{
    if (parent == nullptr) return;
    parent->setGlobalZOrder(z);
    
    // adjust every child menu item
    Vector<cocos2d::Node*> &children = parent->getChildren();
    
    for (int i = 0; i < children.size(); ++i)
    {
        Node* child = children.at(i);
        setGlobalZOrderRecursive(child, z + 1.0f);
    }
}
2 Likes

Yes, having a cascade option is a good idea.

1 Like