user1849661
user1849661

Reputation:

Anchor points in Swift Bugging?

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 ?

Upvotes: 3

Views: 2590

Answers (1)

Nik
Nik

Reputation: 1664

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

Upvotes: 3

Related Questions