Cindy Smith
Cindy Smith

Reputation: 83

swift: returning a child node with the same position

I have Two Sprite nodes in a function from a swift file. Both nodes are similar in size and position. Node2 is a child of node.

When the nodes are added to the scene, the nodes position are different from each other. They are not on top of each other like they are supposed to be.

func stoppers()->SKSpriteNode{   
 let node=SKSpriteNode(imageNamed:"image1")
    node.size=CGSize(width:20,height:300)
    node.position=CGPoint(x:100,y:100)

    let node2=SKSpriteNode(imageNamed:"image2")
    node2.size=CGSize(width:20,height:300)
    node2.position=CGPoint(x:100,y:100)

node.addChild(node2)
return node
}

the function is called to the scene like: addChild(game.stoppers())

Upvotes: 2

Views: 365

Answers (1)

Steve Ives
Steve Ives

Reputation: 8134

If node2 is a child of node1 and you want them to be in the same place on screen, then node2 must have a position of (x: 0, y: 0). This is because the position of a child node is relative to the parent node. They also need to have the same anchorPoint.

Upvotes: 2

Related Questions