3

I tried to understand the basics of anchor points in swift reading this : https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html

I understood that in siwft, with sprite kit, the anchor point of the scene is in the down-left corner of the screen.

So if I position a node like this :

myNode.position = CGPoint(x: 0, y: 0)
self.addChild(myNode)

It doesn't appear on my view. So i tried another thing :

 myNode.position = CGPoint(x: middleOfmyView, y: 0)
    self.addChild(myNode)

And now I see half of the top of my square. So I see 20 pixels up from the bottom of the screen.

So I added :

  myNode.position = CGPoint(x: middleOfmyView, y: 0+myNode.frame.size.height/2)
        self.addChild(myNode)

And now I can see my entire square, on the middle of the screen.

Then I put it at x: 0 and let the Y point like that :

 myNode.position = CGPoint(x: 0, y: 0+myNode.frame.size.height/2)
 self.addChild(myNode)

And now I can't see my node appearing even if there is written "1 Node" in the bottom right corner of my screen!

So i'm wondering is the 0,0 point of the iPhone screen really on the down-left corner of the screen ? Then where is the anchorpoint of my sprite node? How can I put my square right in the down left corner of my screen ?

This is all just to understand how things work… Thanks a lot in advance for your help :)

I'm using Xcode 7.3.1 and I still do not understand why the point 0 makes that I do not see my stuff… it's definitely a bug ?

CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • Are you sure the scene is in the correct position in your view? What is the `frame` of the scene, compared to the frame of the main view? – jrturton Aug 20 '16 at 14:59
  • When I print the frame scene ; I got that : ((-0.0, -0.0, 1024.0, 768.0)) So the same as the main view, nope ? Anyways I did not change the frame of the scene before trying to put my node on the scene. –  Aug 21 '16 at 12:09
  • 1
    The view size is not necessarily the same as the scene size (to achieve this, you can set scene.size = view.bounds.size in your view controller). So if your view is of an iPhone 6 size, the scene by default will be 1024x768, means, that if you place your node at 0,0, it likely not be visible depending on node size. – Whirlwind Aug 21 '16 at 15:17
  • Thanks a lot, I added scene!.size = view.bounds.size and it works now, I can see my little square :) –  Aug 22 '16 at 06:42
  • 1
    Using the frame size for the scene size isn't recommended as it screws up the aspect when using iPads. Change the gameViewController to use `.aspectFill` and set the size of the scenes to be 768 by 1024 (portrait) or 1024 by 767 (landscape). This prevents scenes from being skewed on different devices – Nik Aug 24 '16 at 21:43

1 Answers1

3

SKSpritenodes default to an anchorPoint of (0.5, 0.5) meaning that the middle of the node goes where the position is set to. To change the anchorPoint do this: node.anchorPoint = CGPoint(x: 0, y: 0) this example sets the anchorPoint of the node to its bottom left corner. Keep in mind that changing the anchorPoint also changes the origin of rotation, and that anchorPoint values are between 0 and 1. To put the square in the bottom left corner:

myNode.anchorPoint = CGPoint(x: 0, y: 0)
myNode.position = CGPoint(x: frame.minX, y: frame.minY)

If you need help with anything else (or if I didn't cover what you wanted) feel free to ask me and I'll do my best to help

Nik
  • 1,664
  • 2
  • 14
  • 27
  • Hello Nik, thanks for your answer. When I do what you told me to, I can't see my square, whereas it should be showing in my view. I can't see it ! Do you have any explanation ? Could you try it, launch the app and tell me what you got ? I have "1 node", and nothing appearing on the screen... –  Aug 21 '16 at 11:47
  • Try adding in the line `self.anchorPoint = CGPoint(x: 0, y: 0)` to your scene – Nik Aug 21 '16 at 16:10