3

I am a complete beginner at Swift 3 and programming in general so this will be an incredibly basic question.

I have been following tutorials and am currently positioning a SpriteNode using CGPoint. The tutorial recommends the following to position the Node centrally at the bottom of the screen:

Ground.position = CGPoint(x: self.frame.width / 2, y: 0)

However, that causes it to stick to the top-right of the screen.

When I use the following code:

Ground.position = CGPoint(x: 0 - self.frame.width / 2, y: 0 - self.frame.height / 2)

It positions at the bottom-centre as intended.

I do not understand why this happens as there is very little else done in the tutorial at this point to cause the error.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
Martin Jones
  • 123
  • 7

1 Answers1

2

Set the anchorPoint of the scene to the bottom left corner at the beginning of your didMove(toView: SKView) (or in GameViewController):

scene.anchorPoint = CGPoint(x: 0, y: 0)

Keep in mind that everything you place on the scene is placed based on the anchorPoint. So with this anchorPoint, the origin of your scene is the bottom left corner.

Also note that if you're using .AspectFill as your sceneScaleMode, you don't have to use self.frame and instead set the scene size to 768x1024 (portrait)/ 1024x768 (landscape) then just use number values within the scene size.

Additionally, there are advantages of setting your scenes anchorPoint to the centre:

scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)

This makes it easier to fit the scene into iPad size as well as iPhone, and also simplifies centering nodes.

See this link for more info on making your app universal: iOS Universal Device App with SpriteKit, how to scale nodes for all views?

Community
  • 1
  • 1
Nik
  • 1,664
  • 2
  • 14
  • 27
  • So, setting the anchorPoint globally gives me a lot more control, thanks. If I choose to remove .AspectFill and use exact values will this scale to different displays? – Martin Jones Oct 07 '16 at 15:33
  • 1
    @MartinJones Using .AspectFill is what scales the scene to fit all sizes. That's when you would use exact values, as it scales up to whatever size the device is. See this link for more: http://stackoverflow.com/q/34864533/6728196 – Nik Oct 07 '16 at 15:36
  • Where and when do you set sceneScaleMode? – Confused Oct 07 '16 at 15:41
  • 1
    @Confused You can set it in both the GameViewController, and/or in each scene that you want it to affect. Try to call it before you do any placement in the scene as anything before it's called will not be affected by it. – Nik Oct 07 '16 at 15:44
  • Thank you. Having much "fun" dealing with iPad Pro 12.9 screen vs the other iPad sizes... – Confused Oct 07 '16 at 15:45
  • @Confused No problem. It can be quite the struggle when dealing with all the various sizes :) – Nik Oct 07 '16 at 15:46
  • So... if I'm understanding this right, setting to .AspectFill, I can use literal numbers in the range of 1024x768, and this should work the same on both the big iPad Pro and smaller iPads... right? – Confused Oct 07 '16 at 15:50
  • And, sorry, one more stupid question, where inside the gamescene's code do you set the .AspectFill? Inside didMoveToView? – Confused Oct 07 '16 at 15:52
  • @Confused Yes. However for iPads, the background should be at 1024x1024 and you will have to do some extra customization (as in the link from earlier). That means that the screen is just wider (or taller) than on iPhone and you would have to account for that – Nik Oct 07 '16 at 15:53
  • @Confused Try to set it as early as possible, so first thing in `didMove(toView: SKView)`. I also set the scene `anchorPoint` to `CGPoint(x: 0.5, y: 0.5)` there too – Nik Oct 07 '16 at 15:53
  • Why square? 1024x1024? That doesn't make any sense to me. Surely the 3:2 ratio of 1024x768 makes more sense? But... I'm confused. – Confused Oct 07 '16 at 15:54
  • @Confused Setting it to square accounts for the wider screen... I'm not fully sure why, but it scales fine when the background is set to that. The scene size is still the 3:2 ratio, however the images that you want to be cut off on iPhone but fill iPad screen have to be 1024x1024 – Nik Oct 07 '16 at 15:56
  • That makes absolutely no sense at all. To me. Are you talking about a background image, only, not the frame size as set in the scene editor? – Confused Oct 07 '16 at 15:57
  • 1
    @Confused Yes. When I say 1024x1024 I mean only for the background image. Sorry for the confusion. This way, the background doesn't get stretched to the bigger iPad size, and is rather sized fine for iPad and the sides (or top edges if in landscape) of the image are cut off for iPhone – Nik Oct 07 '16 at 15:58
  • Cheers. That makes some sense. Although it still feels like someone at Apple dropped the ball on thinking about responsiveness. – Confused Oct 07 '16 at 16:06
  • @Confused I agree. Glad I could help – Nik Oct 07 '16 at 16:07
  • @Nik `This makes it easier to fit the scene into iPad size as well as iPhone` how do you figure? Unless you are only referring to when using the `.ResizeFill` scale mode – Knight0fDragon Oct 08 '16 at 10:25
  • 1
    @Nik also another reason, in < iOS 10, it doesn't require the SKCamera to be offset to correctly set the position – Knight0fDragon Oct 08 '16 at 10:26
  • @Knight0fDragon I'm mostly referring to using .`ResizeFill`, but also I find that setting the `anchorPoint` to the centre of the frame allows for more simpler positioning when it comes to iPhone size vs iPad size as opposite side nodes would have coordinates that are negative of each other (e.g. 560, and -560). Also, this way you're building outwards from the centre rather than from the bottom left corner which varies in distance from other edges on iPhone and iPad – Nik Oct 08 '16 at 14:28
  • @Knight0fDragon As for your other comment, I'm not familiar with using SKCamera, so if that's an issue with my answer, feel free to edit it to your liking (or delete parts) – Nik Oct 08 '16 at 14:31
  • @Nik that is what I was figuring, (the .ResizeFill part) The other scale modes do not matter in regards to iPhone and IPad since you are dealing with the same scene size, and the cropping happens evenly on all sides – Knight0fDragon Oct 08 '16 at 14:31
  • not an issue, an inclusion for a reason to use anchor at .5, .5 This is my preferred anchor point as well for coordinates, I find it easier for everyone to understand because we are taught in school that center is origin – Knight0fDragon Oct 08 '16 at 14:33