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 )
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
Hi poco…
It’s not a problem. It’s just the design which looks not normal 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.
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…
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
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.