Dogan Dogan - 3 months ago 17
iOS Question

SKSpriteNode doesn't maintain current position after moveToParent function

The

SKNode
method
moveToParent
does not maintain its absolute position after moving to a new parent in the same scene. The node moves to another position.

The documentation for
moveToParent
says


Moves the node to a new parent node in the scene. The node maintains its current position in scene coordinates.


Did I miss something?

Answer

I think there might be a misunderstanding in the interpretation of the information provided by Apple. In fact "moves the node to a new parent node" it should understand that your node is already a child of a node in the same scene.

If you make for example:

let pos1 = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
let pos2 = CGPointMake(CGRectGetMidX(self.frame)/2,CGRectGetMidY(self.frame)/2)

// make a big yellow node
let bigNode1 = SKSpriteNode.init(color: UIColor.yellowColor(), size: CGSizeMake(400,400))
addChild(bigNode1)
bigNode1.position = pos1

// make a big blue node
let bigNode2 = SKSpriteNode.init(color: UIColor.blueColor(), size: CGSizeMake(400,400))
addChild(bigNode2)
bigNode2.position = pos2

// make a little red node as a child of bigNode1
let littleNode = SKSpriteNode.init(color: UIColor.redColor(), size: CGSizeMake(100,100))
bigNode1.addChild(littleNode)
littleNode.position = CGPointZero

after you have this output:

enter image description here

Now if you do :

littleNode.moveToParent(bigNode2)

the output not change, exactly as explained by Apple.

Update (after 0x141E comment):

@0x141E As you can see, the position remain always 10,10 so it's correct:

enter image description here