Drawing order issue. Easy to reproduce

Hi everyone.
I’m having trouble drawing a label in front of sprites.
It is easy to reproduce. Just open NewRendererTest.cpp (official cpp test source code)

And add these lines:

LabelTTF* label = LabelTTF::create("Cocos2d-x", "Arial", 42, Size(0, 0), TextHAlignment::CENTER, TextVAlignment::TOP);
label->setPosition(40, -20);
label->setLocalZOrder(30);
parent->addChild(label);

in method:

void NewSpriteTest::createSpriteTest()

Just before:

parent->addChild(child1);

The text is rendered behind the sprites. Shouldn’t it be in front?
Using label->setGlobalZOrder(30) does’t work either (although it does work for the sprites… strange!)

The only way drawing in front is adding to the parent node the last.
But I’d like to alter the z order.
Why doesn’t setLocalZOrder work?

I’m using v3.17.1.

Thank you!

I dont’t use setLocalZOrder() very much at all. Are you still having trouble with this?

Yes.
I just need a way to reorder the depth of sprites and labels. Not the order in which they are created.
Any approach would be fine. But I haven’t found a way yet.

How is it supposed to be done?

Perhaps our docs don’t cover this enough. I will see where I can add text to improve this.

but have you tried:

addChild(sprite, 0);
addChild(label, 1); 

and then reverse it to see the difference?

addChild(sprite, 1);
addChild(label, 0); 

and you can make these numbers up, technically.

addChild(sprite, 98);
addChild(label, 99); 

addChild(sprite, 40);
addChild(label, 39); 

Just tried. The label is always behind.

In fact, I tried with the already created sprites in the official cpp test NewRendererTest:

parent->addChild(child4,30);
parent->addChild(child5,29);

But the child5 is always in front of child4.

it works for me in a skeleton app running cocos new.

image

image

Strange… I will do more test.
This is what I’m getting.

Could you try this particular test? Just do a simple change and recompile the cpp tests.
Just to know if there’s something less obvious in my setup.
Thank you

SortAllChildren()??

SortAllChildren() was there originally.
Rememeber that this code is part of the official cocos2d cpp tests.

If I comment that line, nothing seems to change.

I’m not sure why you are stuck on this fact. cpp-tests is just a set of code we use to test the functionality of the engine. Why not create using cocos new and try the code I showed you. You aren’t going to start from cpp-tests. I will try the test you mention though.

ok, I looked at the same test you are using and I don’t have a call to parent->sortAllChildren()

ok, using just the default code, this is what I see when I run test 55 in cpp-tests, code unchanged:

Now, I changed the code to look like this:

parent->addChild(child1,7);
parent->addChild(child2,6);
parent->addChild(child3,5);
parent->addChild(child4,4);
parent->addChild(child5,3);
parent->addChild(child6,2);
parent->addChild(child7,1);

Notice the order did change:

Yes, sorry, sortAllChildren was added later while testing.
Anyway, it doesn’t solve the problem.

The reason why I’m using cpp-test is because I’m getting the same problem in my game.
I wanted to find a way to reproduce the issue. This way it is simpler to report and test by others.

As you can see, using depth parameter in addChild doesn’t work, but global Z does work (but it doesn’t for labels…). Could you try if this is the behaviour for you also?

You are asking me to test code that you added to the cpp-test while telling me it doesn’t work.

I showed you it does work in my screenshots.

and it works with labels too.

auto label1 = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "Testing order");
label1->setPosition(10.0f, -20.0f);

parent->addChild(child1,7);
parent->addChild(child2,6);
parent->addChild(child3,5);
parent->addChild(child4,4);
parent->addChild(child5,3);
parent->addChild(child6,2);
parent->addChild(child7,1);
parent->addChild(label1, 10);

Sorry, I didn’t see your last message before posting mine.

This is strange. I will investigate further, and write back if I find the issue.

Thank you for your help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.