Cocos2d-x Node-Sprite Anchor Points and Removing PhysicsBody Bugs

Thanks @ricardo. I agree with @catch_up.

If I add a sprite to a node object just like that;

auto mySprite=Sprite::create("sampleSpriten.png");
myNode->addChild(mySprite);

I don’t have any problem. Because the sprite object anchor point was in the center.(Default anchor point 0.5 and 0.5) I’m adding this sprite to a node object, zero point is always at center of sprite in the node object. But I can’t do that with when I add a child sprite object to a parent sprite object.

For example;

auto parentSprite=Sprite::create("parentImage.png");
auto childSprite=Sprite::create("childImage.png");  
parentSprite->addChild(childSprite);

If I use that, child sprite is always add at bottom left of the parentSprite. But the child sprite and the parent sprite anchor point values are already (0.5,0.5). And I can’t think that is logical.

Note: I have solved my own problem with that way. (It’s like a joke :smiley: )

childSprite->setPosition(parentSprite->getPosition());
Vec2 deltaPos=parentSprite->convertToNodeSpace(childSprite->getPosition());
parentSprite->addChild(childSprite);
childSprite->setPosition(deltaPos);
1 Like

So, we don’t say that anchorPoint doesn’t affect children: http://www.cocos2d-x.org/docs/programmers-guide/2/index.html#parent-child-relationship

I will change this text and publish an update.

I prefer not overload addChild, but you can add a free function that does that.
eg:

// might not compile... treat it as pseudo code
void addChild(Node* parent, Node* child, const Vec2& anchorPoint)
{
  auto s = parent->getSize();
  parent->addChild(child);
  child->setPosition(s * anchorPoint);  // you can't multiply Vec2 with Size, but you get the idea
}

yes, we should say that anchorPoint only affects the transform operations (scale, position, rotate, skew, etc…) and does not affect children positioning. In fact, children will be always added to the bottom-left (0,0) corner of its parent

I need to be sure.

parentChild->getPosition() //center of  parentChild
parentChild->addChild(childSprite) added on the left-bottom corner

Do you think, it’s not a problem? Who want to add a child object to left-bottom corner point of a center aligned sprite object as a default? :frowning:

@pococogames

Hi poco…
It’s not a problem. It’s just the design which looks not normal :smiley: and also I think less people would be knowing about it. @slackmoehrle Can you please mention this in programmer’s guide if it is of significance.

@pococogames
If you feel, like me, that it should position in the center, then following things can be done:

1) Make a helper function in our code that will have this functionality and will automatically position the child according to our wish. Something like customaddChild() in our own sprite class… Note, you would have to extend this class from Sprite(obviously)

2) Or else you can directly add custom function inside Sprite class of the cocos2d-x library itself, build the library(if you’re using prebuild library).

3) If you think this above 2nd point is of significance and it should be present in the Node/Sprite class by default then discuss with ricardo/slackmoehrl/ @zhangxm and pull request to git can be sent.

:smile:

1 Like

Well, I will use my custom function for that. (But I still think it’s not reasonable :frowning: )

@ricardo Another problem, I get EXC_BAD_ACESS error when I call removeComponent() method for PhysicsBody objects.

Yes, it may/may not be. But as ricardo told, it is probably not the mistake but choice for design…
So, the best thing cocos2dx offers is that it is open source… so you can edit the functionality any time… like 2nd step, I told is the best pick in case you feel intrigued by implementing helper function…

1 Like

yes, you mentioned that… could remind me how to reproduce it? thanks.

I’m clarifying in a few places and will post an updated version in the AM.

What does “AM” mean?

the morning time.

PhysicsBody *pb=PhysicsBody::createBox(Size(15,5),PhysicsMaterial(0.1f,0.0f,1.0f));
this->addComponent(pb);
this->removeComponent(pb); // I get EXC_BAD_ACCESS error

@pococogames
Are you sure that’s how you remove the physics body?

This is also a way to do it.
someSprite->getPhysicsBody()->removeFromPhysicsWorld();

what is this ? what kind of object is that? thanks.

Node based object.

Yep. I get the same error. I tried. I’m using this way in my project for now;

this->getPhysicsBody->setEnabled(false);

It’s not problem for me , because this object will be removed next time in the scene. But If I can’t remove any object in my project, I need remove PhysicsBody component from the node object. (Imagine, maybe I need more performance optimization )

@pococogames
It’s strange looking at your issue…
I’ll try our your code, probably day after tomorrow(sorry travelling somewhere, so taking time).

Meanwhile, please put your code to github or somewhere bcoz as @ricardo told your code’s link is expired.
I’ve done physics body removal in my prototype in v3.3. In case anything going wrong, I’ll cross check.

1 Like

I already created an issue: https://github.com/cocos2d/cocos2d-x/issues/15932

and I can reproduce the crash… I’m looking at it right now

UPDATE: crash fixed. will be part of v3.13

1 Like

Thanks. It’s a good news. gby

Thank you too for help&tips.