0
votes

I'm learning Swift and am getting some weird positioning issues in Sprite Kit with an SKLabelNode.

I wrote the following code:

shareText.position = CGPointMake( self.frame.size.width / 2 , shareButton.position.y );

This does exactly what I expected. It centers the text horizontally on the screen.

Well, I'm splitting my screen in half visually so I'd expect to divide by 4 and have it show up "centered" on the left half of the screen.

shareText.position = CGPointMake( self.frame.size.width / 4 , shareButton.position.y );

Unfortunately it looks as if the SKLabelNode is at the "0" position related to the frame. In fact, based on how much of the text I can see it looks like it's slightly into the negative.

When I check it's position using NSLog, when I divide by 2, it says its x position is 512. When I divide by 4, it says its x position is 256 (what I'd expect) but visually, it's not even close. I've also tried setting it to shareButton.position.x which is also at 256 (and shows visually exactly how I expect it to) but I get the same results. When I manually set it to around 400, it seems to work.

Is there something wrong with Swift (I now it's a beta) or am I doing something wrong?

1
Learn about the coordinate systems used by SpriteKit.duci9y
I sort of found more about the problem. From some random Googling, I found that, when changing scenes to this scene, if I used SKSceneScaleMode.ResizeFill instead of AspectFill, it "fixed" the problem and moved the text to the right location but completely broke the rest of the scene. So it has something to do with that but what should I do differently?user1269977
As I said, you need to be aware of the coordinate systems SpriteKit works with. Read the documentation.duci9y
In reply to duc9y: I understand the basics of the coordinate system. 0,0 should be bottom left. Any nodes have 0,0 as their local center point. I've released a game using Unity3D so I have a firm grasp of basic x,y coordinate systems in general. It's just that 0,0 doesn't seem to be the bottom left like I have read. It seems to be off screen.user1269977
You have not read enough. Read up on the position and the anchorPoint properties. To reply directly to a user, prefix their username with an @ sign in your comment. For example, "@duci9y <your comment>".duci9y

1 Answers

0
votes

So I think I figured out the problem and it had nothing to do with my Swift code but with XCode itself (or the documentation on how XCode functions by default). The Documentation @duci9y linked to in the above comments mentions the following...

"So, a scene is initialized with a height of 1024 and a width of 768, has the origin (0,0) in the lower-left corner, and the (1024,768) coordinate in the upper-right corner."

I found that in my GameScene.sks file, it defaulted to the opposite. A width of 1024 and a height of 768. My game is supposed to be in portrait so I want it to be as the documentation says, NOT how XCode defaulted my project. I'm guessing this means that (0,0) would have been 128 points off the screen to the left rather than the bottom left corner in portrait but in the correct location in landscape.

I'm unsure if this is an issue with XCode 6 beta 4 or if the documentation listed is just out of date, but as soon as I swapped these numbers, it all seemed to work the way I wanted it.