Search code examples
swiftsprite-kitskspritenodesklabelnode

Position of a Node after addChild


I have an SKSpriteNode like a light gray square in the view, and I want to put a label inside it... I do this way:

let puntosCubo = SKSpriteNode(color: SKColor.lightGrayColor(), size: CGSize(width: gameoverTitle.frame.width, height: gameoverTitle.frame.height*4))
puntosCubo.position = CGPoint(x: CGRectGetMinX(self.frame)-100, y:y2)

I put a SKLabelNode inside puntosCubo this way:

let puntosCuboTitle1 = SKLabelNode(fontNamed: "Apple SD Gothic Neo")
    puntosCuboTitle1.fontColor = SKColor.blackColor()
    puntosCuboTitle1.fontSize = 20
    puntosCuboTitle1.text = "Score"
    puntosCubo.addChild(puntosCuboTitle1)
    puntosCuboTitle1.position = CGPoint(x: 0, y: puntosCubo.position.y)

But the result is that the position of the SKLabelNode is not inside puntosCubo. I think i am using the position of puntosCubo on a wrong way...

Any ideas/help. Thanks.


Solution

  • Because of

    puntosCubo.addChild(puntosCuboTitle1)
    

    the position of the label puntosCuboTitle1 is relative to the postion of its parent (puntosCubo)

    puntosCuboTitle1.position = CGPoint(x: 0, y: 0)
    

    makes the position of the puntosCuboTitle1 in the middle of its parent puntosCubo