Reputation: 449
I'm so freaking confused about anchorpoint in SpriteKit.
If I have a Node consisting of multiple childnodes, and I change the anchorpoint of the Node, then all the childnodes gets misplaced. Is there any way around this?
I think it's really annoying to position nodes if I always use the default anchorpoint (0.5,0.5), so sometimes I change the anchorpoint to for example 1.0,0.0 so it's easily placed in the right lower corner. But if this node has childnodes, then they are all misplaced when I change the anchorpoint.
What am I missing?
Upvotes: 2
Views: 2046
Reputation: 64477
In your case (and in general) the fix is relatively easy. Instead of adding child nodes to the sprite node whose anchorPoint got changed, use a common parent node (SKNode). Add the sprite node to the parent node, change the sprite's anchorPoint but don't add any children to it. Instead add those children to the parent node. You can then use the parent node to position this branch of the node tree together.
Alternatively create a subclass of (or category on) SKSpriteNode with methods that allow you to align the node to a certain border, taking the sprite's frame size and anchorPoint into account. For example you could add two properties verticalAlign and horizontalAlign with center, left/top and right/bottom alignment. Whenever the position property is changed, the point is offset before being passed to super setPosition so that the alignment mode is adhered to.
Upvotes: 5