4
\$\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\$
5
\$\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\$
  • \$\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
  • \$\begingroup\$ No problem. Nothing better than answering a question 2 years before it was asked! \$\endgroup\$ – MentatOfDune Apr 19 '16 at 22:05
  • \$\begingroup\$ Lols! It's back to the future. \$\endgroup\$ – Septronic Apr 20 '16 at 23:02

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.