addChild by not changing the position of the child

I am new to Cocos2d-x so if this is beginner stuff, I’m sorry :frowning:

Is there any function that can do exactly that?
And is there a way to do this in 3D(setPosition3D)?

Thanks for reading!
And sorry for the terrible writing :frowning:

Hello
Please see below Parent Child Relationship in cocos2d-x
https://docs.cocos2d-x.org/cocos2d-x/v3/en/basic_concepts/parent_child.html
children will be always added to the bottom-left (0,0) corner of its parent.

Your red-box is child of black-box
So if you change position of red-box(child), it will not affect to parent(black-box)

So, do I have to manually type the position to move the red box to its original position? Or is there a function that can calculate where to move the red box if it is added as a child?

“children will be always added to the bottom-left (0,0) corner of its parent.”
Or can I avoid the addChild changing the position of the child?

Also, sorry for my bad English :frowning:

As I know, you have to manually type the position for move the red box to it’s original position.
You can use cocos studio if difficult to calculate position
cocos studio provides GUI, so you can move child with mouse easily.
And see position values and apply it to your code
It should be much helpful in your case.

Or can I avoid the addChild changing the position of the child?
I think you can not avoid.

1 Like

@Cshift A child node is always relative to the parent node.

So, for example:
1 - red box is at (100, 100) (x,y coordinate) and is a child of the scene node
2 - black box is at (500,500) and is a child of the scene node

If you want to make the red box a child of the black box, but you want it to remain in the same position, then assuming that the position is already set to (100,100) on the red box, you can do this:

Vec2 position  = redBox->getPosition();
position = position - blackBox->getPosition(); // (100, 100) - (500, 100) = (-400, 0)
redBox->setPosition(position);
redbox->removeFromParent(); // assuming it already has a parent
blackBox->addChild(redBox);

This does not account for the anchor point, so while it may be in the correct location, it will not be exactly where you expect it to be. Sprites have a default anchor point of (0.5,0.5), so the coordinates you set are technically the middle of the sprite.

You can change the anchor point to something else, but if you keep it as it is, then it’s simple to solve the equation:

Imagine the boxes are of size (40,40):

Vec2 position  = redBox->getPosition();
position = position - blackBox->getPosition() + (blackBox->getContentSize() / 2); // (100, 100) - (500, 100)  + ((40, 40) / 2)= (-380, 20)
redBox->setPosition(position);
redbox->removeFromParent(); // assuming it already has a parent
blackBox->addChild(redBox);

So now the red box will be exactly where you expect it to be relative to the black box, assuming you have not changed the anchor point (Vec2::ANCHOR_MIDDLE). If you do change the anchor point, then the calculation would need to be changed to account for it.

3 Likes

It works! Thank you so much :smiley:

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