-1

I'm making a simple game in Swift, and I was trying to find a good way to get the center of the game scene. At the moment I'm doing this:

CGPoint(x: size.width / 2, y: size.height / 2)

Seems simple enough. However, when checking, I found these four things also exist as part of the GameScene class:

self.frame
self.size
self.view.bounds
self.view.frame

I'm quite confused. Which one am I supposed to use? They all look the same to me. What's the difference?

MysteryPancake
  • 1,365
  • 1
  • 18
  • 47

1 Answers1

5

The frame is the location in the views superview, so if it's a 100x100 square at 100 pixels from the left and the top its {100, 100, 100 100}.

The bounds is the rect, but ignoring the position in a superview. A bound-rects origin will always be 0,0. So the view in the example above would be {0,0,100,100}

Size is is just the same as bounds.size, {100,100}

Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92
  • Which one do you reccomend I use though? – MysteryPancake May 16 '17 at 13:24
  • 1
    To find the center of the scene, I suggest you use scene.bounds. Remember that a sprite placed at the exact centre will have that as it anchor and appear to be offset, so you need to subtract your sprites size from the point you are placing it at. – Oscar Apeland May 16 '17 at 13:25