SSH This SSH This - 1 year ago 108
Swift Question

Odd Positioning of SKLabelNode

I am trying to add a SKLabelNode to the top left of my scene. Currently I am achieving this using a value of "300"

func addMoneyLabel() {
let moneyLabel = SKLabelNode(fontNamed:"Copperplate")
moneyLabel.fontSize = 25
print("CGRectGetMidX(self.frame),", CGRectGetMidX(self.frame))
print("CGRectGetMinX(self.frame),", CGRectGetMinX(self.frame))
moneyLabel.position = CGPoint(
moneyLabel.text = "$500"
moneyLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Top
moneyLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left

This produces an image that looks like this:

enter image description here

Why isn't "0" or "CGRectGetMinX(self.frame)" the correct value for positioning top left?

Answer Source

Because the scene size is not necessarily the same as view's size. By default, scene size is 1024x768. As I can see, you are using iPhone 6s+ which has different proportion and different dimension in compare to default scene's size.

To debug this, put a break point, or just print scene.size and scene.view.size.

You can make a scene to have the same size of a view, if you set this in your view controller:

scene.size = view.bounds.size

Now, when you set x position to 0 (and set y to CGRectGetMaxY), the label will be placed in top left corner.

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