Problems with cocos2d::Layer positioning

Hi guys :smile:
Consider this very simple code (functions are called once in the cocos2d-x hello world project init):

void HelloWorld::addNode() 
{
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto n = Node::create();
    n->setContentSize(visibleSize);
    n->setAnchorPoint({0.5f, 0.5f});
    n->setPosition(visibleSize * 0.5f); // center the node
    addChild(n);
    
    auto s = Sprite::create("HelloWorld.png");
    s->setAnchorPoint(Vec2::ZERO);
    n->addChild(s);
}

void HelloWorld::addLayer() 
{
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto n = Layer::create();
    n->setContentSize(visibleSize);
    n->setAnchorPoint({0.5f, 0.5f});
    n->setPosition(visibleSize * 0.5f);  // center the layer
    addChild(n);
    
    auto s = Sprite::create("HelloWorld.png");
    s->setAnchorPoint(Vec2::ZERO);
    n->addChild(s);
}

Here the result:

I expected the layer position, being the layer a node, to be equal to the node one (the center of the scene)!
Am I missing something?

Since you’re adding the second node to the first node, the second node is placed in respect to the first nodes coordinate system. I would recommend reading some documentation on that.

I know this!
The question is not so trivial! :smile:

If you look carefully the code you notice that:

  • Parents have content size equals to visible size
  • Parents’ node anchor is on the center {0.5f, 0.5f}
  • Parents’ position is on the center of visible size (this implies that parents’ origin should be on screen bottom-left corner)

Then adding a sprite ({0,0} anchored) to the position {0, 0}, I expect to be placed on the bottom-left corner.
This is true if the parent is a Node, it is not true if it is a Layer.
I don’t understand why Layer behaves differently being itself a Node!

@drakon99

Try with setting
Ignore anchor point position parameter

With anchor {0,0} Layer e Node classes behave the same, with anchor {0.5f, 0.5f} not!
I think this is atypical! :frowning: