Width and Height of a Sprite

Hi Guys,

I’ve created an empty sprite and added some dynamic images to it. I have tried to get the width and height of the sprite using boundingBox().size.width & getContentSize().width but both are always showing zero. Anybody please point out the real error here?

I have used cocos2Dx 3.2 CPP version.

Thanks

Show me your code here please.

Thanks Owen,

Here is the code:

Sprite* container=Sprite::create();
for(uint i=0 ; i<10; i++){
armature=Armature::create(NODE_ARMATURE);
container->addChild(armature,200+i);
}
cout<<"container width: “<< container->getContentSize().width<<” height: "<< container->getContentSize().height;

But showing 0 as width and height.

container->addChild(armature,200+i);

Just to be sure, this line says: add armature to container with a z-order of 200+i.

I just don’t want you to think this has something to do with size…

A Sprite will not dynamically update it’s size when you add children to it. Since you created a default Sprite, the size will always be 0. If you want to change this behavior, you could subclass Sprite and have it update the content size in an overridden “addChild” method.

@toojuice - yeah subclassing would work. Technically they wouldn’t need to but it does seem way cleaner. I prefer subclassing when I am adding my own functionality but I notice a lot of users here don’t subclass.

Also, what I am wondering is does the OP need to setPosition() when the do the addChild() above? Otherwise the sprites will be stacked on top of each other, centered to the container. If they do, then I think they would also need to manually calculate the width and height then too as the position of each new could change the width and height of the container

It’s really helpful replies, thanks a lot, Sorry for the noob question, I was trying to create a board game, so I have created some armatures and added it on a container grid (just a sprite,armatures arranged like a grid,not mentioned in the code above) and trying to position the container grid at the screen center position. I thought it would be easier for me to place it at the screen center position if I knows the height and width of container grid.

If you know what your container width and height should be ahead of time, which I imagine you do for a board game, you could try doing:

container->setContentSize(Size(width, height));

where you replace your width and height by your known values. This is the simpler way that I think @slackmoehrle was hinting to as opposed to subclassing Sprite. But I agree with him that subclassing is cleaner.

@slackmoehrle - do you know if there’s a reason why Sprite doesn’t update it’s size when addChild is called? Is this to prevent weird artifacts if you add child nodes during a scene that might cause position to have to be recalculated due to a changing anchor point?

@toojuice - I’d imagine that we couldn’t always make the assumption that adding to width and height is appropriate for everyone. So having to do it manually ensures that it is flexible, but let me talk to one of the devs to see if they have any thoughts.

@slackmoehrle - ok, cool. Maybe it can be an option that the user has to turn on? I would be happy to contribute!

What time zone are you in? I’m in PST, Cali.

How about we work on it together? I’m busy this week (iPhone launch Friday, I’m going to wait in line starting tonight!!) but I could make time next week…

Did this ever get figured out? It seems like a very common request to be able to enable a node to keep track of its size and the size of its children. It would help me, atm. Did you two work on it and produce a solution?

I am creating a CCNode and adding children, and contentSize & boundingBox always come up with 0 w / 0 h, which seems to be expected. But its very clumsy for UI development to have to know all your sizes in advance.

I started working on a PR a while back but became to busy with other things.

Ok, thanks. I will take a look at it and post back if I make progress or end up going another way.

Would the getCascadeBoundingBox(Node *node) helper function work for you? It’s available in ccUtils.cpp