2

I'm subclassing SKNode to render a map that consists of many SKSpriteNodes by adding those nodes to my subclass.

All is working well but I noticed that the Frame of my node subclass is always 0.

I couldn't find anything about this in the docs, so: am I supposed to override the Frameproperty and return a correct value?

Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • Can you clarify 'Frame ... Is always 0'? Frame should be a CGRect struct so can't be zero. Usually for an SKNode the frame will have a valid position but zero width and height. If it itself is positioned at 0,0 in the scene then all its members will be zero. That would be normal and no need to override frame. If you want the frame of its children then use the method calculateAccumulatedFrame. – Ali Beadle Jan 01 '17 at 22:14
  • I mean 0 for both width and height. I noticed that an SKSpriteNode has a valid width and height. – Krumelur Jan 01 '17 at 22:18

1 Answers1

3

The frame property is the frame of the node itself, ignoring any children. For an SKNode this is a position but zero width and height.

The frame is non-empty if the node’s class draws content. (https://developer.apple.com/reference/spritekit/sknode)

Which I read as: SKNode draws no content so it is correct for it to be empty.

The frame of the node and its children is provided by the method calculateAccumulatedFrame() and functions that need the frame of the sub-tree should call that and not reference the frame property.

So there should be no need to override frame for your SKNode. I never have, have similar constructs in a few apps and have not yet had problems...

Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
  • Makes sense. What would be an example of a node that "draws content"? – Krumelur Jan 02 '17 at 14:20
  • @Krumelur: An SKSpriteNode or an SKLabelNode - both inherit from SKNode but have drawable content so will have width and height according to that content. – Ali Beadle Jan 02 '17 at 14:24