setGlobalZOrder not working for UI item?

I am running with 3.0 RC2.

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));

@vision

The Global ZOrder 's behavior is :

Nodes that have a Global Z Order lower, are renderer first.

Don’t mess with Global ZOrder and Local Zorder. There are totally different things.

Please refer to the comment in CCNode.h to take a closer look at what does Global ZOrder and Local ZOrder mean.

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.

@owen wrote:

@vision

The Global ZOrder 's behavior is :

Nodes that have a Global Z Order lower, are renderer first.

Don’t mess with Global ZOrder and Local Zorder. There are totally different things.

Please refer to the comment in CCNode.h to take a closer look at what does Global ZOrder and Local ZOrder mean.

@vision

Try to use the function setPositionZ instead.

Sadly serVertexZ didn’t work for my case neither. I had to work around the menu with a sprite replaced anyway.
@owen wrote:

@vision

Try to use the function setPositionZ instead.

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:

@vision

Try to use the function setPositionZ instead.

1 Like

Do you found any solution setGlobalZOrder() to children of the node??

same problem, wondering if i can set globalzorder to label

I solve this problem in this way : just set the GlobalZOrder for all children in this Menu…
and you can solve the problem in label just in this way

cocos2d::MenuItemSprite* back_game = MenuItemSprite::create
(
Sprite::create(“back_game.png”),
Sprite::create(“back_game_select.png”),
CC_CALLBACK_0(GameScene::menuContinueCallback, this)
);
back_game->getNormalImage()->setGlobalZOrder(PrioGame::Menu2);
back_game->getSelectedImage()->setGlobalZOrder(PrioGame::Menu2);
forgive my poor english

1 Like