5

I am having trouble fully grasping the coordinate system with SpriteKit using Swift. I have looked at many resources including apples dev docs but for some reason I am very confused (i'm sure this must be simple).

I have a GameScene class inheriting from SKScene. I am just trying to add a SKSpriteNode to the screen using various coordinates to get used to the screen layout. I believe that when you create any SKNode and give it a position by doing the following

var node = SKSpriteNode(color: UIColor.brownColor(), size: CGSizeMake(100, 100))
node.position = CGPointMake(0, 0)

and then place any SKNode using

self.addChild(node)

The coordinates that have been supplied to the node are coordinates in the parent node. Does this mean that the brown square that has been created should be placed at the bottom left corner?

This is obviously not the case and is what is confusing me. I am using the iPhone 5s simulator for testing in portrait.

The sprite node only appears in the bottom left when I set the position to (350, 50).

kev3kev3
  • 197
  • 1
  • 3
  • 15
  • Are you making any modifications to the anchorPoint properties on either the node or the scene? Is the a landscape game, and if so how and where are you presenting your scene? – Mick MacCallum Jun 11 '14 at 12:38
  • I am not doing anything to anchor point properties, is this were i'm missing something? – kev3kev3 Jun 11 '14 at 12:40
  • I have the device set to portrait orientation, and I am using the standard game template code. The scene is presented in the default GameViewController provided by the standard game template. – kev3kev3 Jun 11 '14 at 12:51
  • The default anchor point for any node is 0,0, it's center. I haven't touched swift, but creating a new SKSprite, adding it to the SKScene, and then setting its position to be 0,0, would be the same as not setting its position. I'm guessing that the issue is the SKScene is larger than the screen, or is not centered on the screen. – Cooper Buckingham Jun 11 '14 at 14:52
  • I am seeing this too, I am adding my SKSpriteNode directly to the SKScene (so its on the root of the node tree) with the anchor unchanged. To get the sprite to centre on the bottom left corner of the device I have to set sprite.position = CGPoint(x: 300, y: 0). I have tried altering the view sizes in the storyboard and playing around in GameScene.sks but with no luck, what am I missing or is something bugged? – fuzzygoat Jun 11 '14 at 18:37
  • Just looked a little closer and it would seem that right at the start in didMoveToView the frame is set to x=1024, y=768 which is wrong. This is obviously affecting the view origin point. – fuzzygoat Jun 11 '14 at 18:46

2 Answers2

9

Select the GameScene.sks in the Project Navigator, select the SKScene (it should have a yellow border round it) and set its size property to 320 points x 569 points (this is assuming iPhone5 / 5S) either way this is the problem. Also make sure you double check it as I am sure it changed back to 1024x768 on me, but seems to be working now.

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • This doesn't seem to have solved the problem for me. Unless I have done this wrong. I have selected GameScene.sks which seems to be just a yellow rectangle, I have changed the value from 1024*768 to 320*569 but I still have to set it at 300 instead of 0. Any ideas? – kev3kev3 Jun 11 '14 at 19:14
  • Ok, after completing this change and then, restarting Xcode and the simulator things seem to be working. :D – kev3kev3 Jun 11 '14 at 19:51
  • Glad its working, I will investigate a bit more tomorrow and edit my answer, there are still a few things I want to check about this. – fuzzygoat Jun 11 '14 at 21:06
4

In the GameViewController class at the function viewDidLoad(), add this line of code:

scene.size = skView.bounds.size
Troy
  • 91
  • 2
  • 3
    In viewDidLoad method the final size of the view may not be known yet. http://stackoverflow.com/a/9540935/3402095 and sometimes the proper implementation of viewWillLayoutSubviews (instead of viewDidLoad) for scene initialization is the good choice. – Whirlwind Jul 30 '15 at 13:49