Dogan Dogan - 1 year ago 92
iOS Question

SKSpriteNode doesn't maintain current position after moveToParent function


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

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 Source

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))
bigNode1.position = pos1

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

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

after you have this output:

enter image description here

Now if you do :


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download