Reputation: 416
So I have an SKLabelNode which displays the users high score, when the users highs score is for example 5 the labels position isn't in the same position to where it would be if the score was for example 35, or 100 makes the label go off screen, please can someone help with this issue, is there a way to make the label stay in the same position if the high score changes? do I need to put constraints on the label? BTW this is in sprite kitand using swift
Upvotes: 4
Views: 657
Reputation: 13665
You want to use SKLabelNodeHorizontalAlignmentMode.Left here, like this:
class GameScene: SKScene {
var label = SKLabelNode(fontNamed: "ArialMT")
var score: Int = 0 {
didSet {
label.text = "Score: \(score)"
}
}
override func didMoveToView(view: SKView) {
label.fontSize = 24
label.text = "Score: \(score)"
//Place the label in upper left corner of the screen
label.position = CGPoint(x: 50, y: CGRectGetMaxY(frame)-50)
//Add this
label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
addChild(label)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
score+=5
}
}
The result:
Upvotes: 3
Reputation: 434
I'd try changing the node's anchorPoint from (0.5, 0.5), which is the default, to (0, 0). This means that the node will now be "anchored" to the top left corner instead of the middle.
EDIT: (0, 0) in SpriteKit is BOTTOM LEFT. Use (0, 1) instead for TOP LEFT!
Upvotes: 0