5
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

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;
//etc...
\$\endgroup\$
1
  • \$\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
    Commented Mar 18, 2016 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .