0

I have this code to put a score label for my game in the top left corner of the screen.

    scoreLabel = SKLabelNode(text:"bats avoided: \(score)")
    scoreLabel.fontSize = 50
    scoreLabel.horizontalAlignmentMode = .left

    scoreLabel.position = CGPoint (x:10 - (frame.size.width/2), y:   (frame.size.height/2) - 50)

but the label appears on iPhone simulator only, not iPad.

If I print the portion of the label at runtime, it shows that the position is x: -365 y:617 with a screen width/2 of 375 and screen height/2 of 667, so it should definitely be there.

what gives?

JustABeginner
  • 321
  • 1
  • 5
  • 14

1 Answers1

1

The coordinate system in iOS represents 'top left' as 0,0. That is to say that as you increase x, you move to the right and as you increase y, you move down.

You are setting a value of 10 - (frame.size.width/2) for x. Suppose your frame.size.width is 300, for example, this would be equal to -140. Remember how I said 'top left' is represented as 0,0? Well, if you have an x coordinate of -140, the element will be further left than 0,0, and subsequently offscreen.

To place something in the top left, inset by 10 points. You would simply set it's position to be CGPoint(x: 10, y: 10).

It is worth noting that when working with sprites, as in your case, that Y0 is actually the bottom, rather than the top of the screen. This a somewhat annoying discrepancy but one to keep in mind.

Jacob King
  • 6,025
  • 4
  • 27
  • 45
  • so I just tried setting x: 10 y: 10 but then the label appears almost bang on in the middle of the ipad's screen? also, in my GameScene.sks panel, the (0,0) coordinate thing seems to be in the middle of the screen.. – JustABeginner Jul 11 '18 at 13:33
  • so the anchor point of my scene seems to be (0.5, 0.5). this is confusing. – JustABeginner Jul 11 '18 at 13:36
  • The anchor point defines the coordinate space. An anchor point of `(0.5, 0.5)` effectively means that your 0,0 coordinate will lie halfway across the screen on both axis. This is standard for scenes. You could try and compensate for this by using a position of `CGPoint(x: -(frame.width / 2) - 10, y: -(frame.height / 2) - 10)`. – Jacob King Jul 11 '18 at 13:45