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
Asked
Active
Viewed 645 times
4

Andrew Harris
- 396
- 7
- 24
2 Answers
3
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:

Whirlwind
- 14,286
- 11
- 68
- 157
-
thanks for response, however using your position makes the label completely go off the view, and adding the horizontalAlignmentMode.Left only leaves the end on the label on the view. I've tried using .Right but that sets the label off the view aswell, To make it clear, this is my Highscore label code - – Andrew Harris Jan 25 '16 at 21:04
-
the position used to be - .position = CGPoint(x: self.frame.width / 2 + 60, y: self.frame.height / 2 + 335) – Andrew Harris Jan 25 '16 at 21:06
-
Sure, you have to reposition your label accordingly. Just move the label a bit to the right. – Whirlwind Jan 25 '16 at 21:06
-
so your position sets the position to be top left, how come the label is off the view then? – Andrew Harris Jan 25 '16 at 21:08
-
@AndrewHarris No problem, glad to hear it works! If this answer helped you with to solve the issue, please mark it as accepted. – Whirlwind Jan 25 '16 at 21:51
-
Hi @Whirlwind, how can I get CGRectGetMaxY ? – Sky Jun 30 '20 at 14:05
0
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!

kevchoi
- 434
- 2
- 7
-
thanks for the quick response! I'm quite new to SKLabelNodes, how would I add this anchorPoint to the label? – Andrew Harris Jan 25 '16 at 20:38
-
-
1
-
2Whoops - sorry about that! You're right, and the other answer is correct :) – kevchoi Jan 25 '16 at 21:02