I have an SKSpriteNode that is parented to (i.e. a child of) a scrolling background. The SKSpriteNode does not move itself, it just moves along with the scrolling background. What is the best way to find the absolute position of the SKSpriteNode as the background moves.
Asked
Active
Viewed 3,216 times
1 Answers
13
CGPoint positionInScene = [self.scene convertPoint:self.position
fromNode:self.parent];
However, convertPoint:fromNode:
is pretty expensive, especially if you use it in update
method. I'd rather just add childNode
's x
position to parentNode
's x
position:
CGFloat xPosition = self.position.x + self.parent.position.x;

Andrey Gordeev
- 30,606
- 13
- 135
- 162
-
Thank you Andrey, I was previously doing the add that you mention, but was trying convertPoint:toNode: I will have a better look and get it working. Ultimately I will take your advice and use the add, as I do want to use it inside update. Much appreciated. – fuzzygoat Feb 18 '14 at 12:17
-
4Please don't say things like "pretty expensive" without any links to facts (real measurements) or at least mentioning by which factor it is slower compared to alternatives (on average or worst case). The thing is that (not just) beginners will often take such advice and try to avoid using such functions if at all possible (premature optimization, based on hearsay no less). Certainly the manual way will be a bit faster but I doubt it will make any difference in nearly every use case. – CodeSmile Feb 19 '14 at 09:20
-
2Adding the x positions like that doesn't account for zRotation but convertPoint does so. – nicopolyptic May 04 '14 at 17:22
-
1Also the second way don't recurse through the parents chain, so it only works for node child of node child of scene. – hariseldon78 Jan 23 '15 at 17:43