Global Z order of children

Global Z order of children
0.0 0


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?



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;
    // adjust every child menu item
    Vector<cocos2d::Node*> &children = parent->getChildren();
    for (int i = 0; i < children.size(); ++i)
        Node* child =;
        setGlobalZOrderRecursive(child, z + 1.0f);


Yes, having a cascade option is a good idea.