I was trying to make a dialog. So I placed a sprite whose global Z order is set to 10 at the screen center. Then I tried to put a menu above the sprite. But it turned out that no matter how I set the global Z order or local Z order, the menu is always rendered behind the sprite. I also found that if I replaced the menu with a new sprite whose global Z order is set to 11, it is rendered above the old one.
This is confusing. Or did I use the function incorrectly?
I am attaching the codes below. Just put them into HelloWorldScene.cpp and have a try.
// Adding the following codes to verify the issue.
// set th global Z order of “HelloWorld” splash sprite
sprite->setGlobalZOrder(10);
sprite->setOpacity(200);
// set the global Z order “HelloWorld” label, not working
label->setGlobalZOrder(12);
label->setLocalZOrder(12); // not working too
label->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y + 100));
// create a new sprite and set its global Z order, works fine
auto sp = Sprite::create(“test.png”);
addChild(sp);
sp->setGlobalZOrder(11);
sp->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y - 100));
// create a new menu item, not working too
auto mi = MenuItemImage::create(“test.png”, “test.png”);
mi->setGlobalZOrder(13);
auto mm = Menu::create(mi, NULL);
addChild(mm);
mm->setGlobalZOrder(14);
mm->setPosition(Point(visibleSize.width/2 + origin.x - 100, visibleSize.height/2 + origin.y - 100));
That is as what I understand.
But if so, why the label (global Z order is 12) is not rendered after the sprite (global Z order is 10)?
I have commented out all the local Z order stuff.
Found that is just because setGlobalZOrder() is not propagated to the children of the node, thus for Label/MenuItem nodes, the _textSprite’s Z position is not set in reality. Not sure this is a bug or by design, but it’s really confusing. @owen wrote: