I have a Sprite node with many 6 child nodes. Each are placed one after another in X-axis. I want to know the complete with of the Sksprite node.

SKSpriteNode *array = [SKSpriteNode node];

[array addChild:box1];
[array addChild:box2];
[array addChild:box3];
[array addChild:box4];
[array addChild:box5];
[array addChild:box6];

array.size.width dosen't get me any size, so how should i get the complete width of array with child nodes..

If i add multiple sprites to NSMutalbleArray then how can i get the width of the complete array.


To get the complete bounding rectangle of a node and its child nodes you use the calculateAccumulatedFrame method. It's a member of SKNode.

I ran into this when adding my SKSpriteNodes to separate SKNodes as layers for parallax and HUD elements. I kept getting (0,0) width/height for my bounding rect on the SKNode, and I found this.

It returns a CGRect, and you can use it like so:

CGRect layerSize = [_layer calculateAccumulatedFrame];
float widthVar = layerSize.size.width;
  • \$\begingroup\$ Fantastic! This had been bugging me for over 2 months! I was going to adjust a child node in another project, and kept getting 0. Ended up tackling it using the parent node's size, and gave up on this aspect. Thank you \$\endgroup\$ – Septronic Mar 18 '16 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.