Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit positioning and coordinate system

I have a blank SKScene in my project, and I tried to add a box into the scene as follows:

let blueBox = SKSpriteNode(color: UIColor.blueColor(),
                               size: CGSize(width: 100, height: 100))
blueBox.anchorPoint = CGPoint(x: 0, y: 0)
blueBox.position = CGPoint(x: 0, y: 0)
addChild(blueBox)

For some reason the box does not appear on the screen. If my understanding of the positioning coordinates are correct anchorPoint of 0,0 and position of 0,0 means "treat point 0,0 as the bottom left of the blue box". So I expect to see a blue box at the bottom left of the screen. However, if I change the position like so:

blueBox.position = CGPoint(x: 300, y: 0)

Then I can see the blue box (at the bottom, very close to the left edge). It is as if the bottom left coordinate on the screen does not start from x=0, and some value roughly equivalent to 300.

Would anyone be able to share some light? I'm fairly new to SpriteKit, as you can clearly see..

Second question I have is regarding to the bound and frame inside SKScene. I can see that 'self.size.width' is really the same as 'self.frame.size.width'. Similarly, 'self.size.height' is the same as 'self.size.height' (In my simulator, width is 1024, and height is 768. However, if I do self.view.bounds.size.width, then I got 375. Similarly, self.view.bounds.size.height gives me 667. So my question is, how is this 'view' (SKView) being set in my code? I don't have anything that sets it.

It is literally:

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {
      // prints Optional(375.0) Optional(667.0)
      print("\(self.view?.bounds.size.width) \(self.view?.bounds.size.height)")

      // prints 1024.0 768.0
      print("\(self.size.width) \(self.size.height)")
    }
}

Again, thank you very much in advance for your help!

like image 238
caffeine_inquisitor Avatar asked Sep 03 '25 05:09

caffeine_inquisitor


1 Answers

Ahh... ok... I found the answer to my problem (partly). In my project I still have the GameScene.sks (that was generated by Xcode for new game project).

In my GameViewController, I constructed the GameScene as follows:

GameScene(fileNamed: "GameScene")

If I change it to :

GameScene(size: view.bounds.size)

then everything works ok. The size that was printed (in my didMoveToView) are now:

Optional(375.0) Optional(667.0)
375.0 667.0

which is expected, as I constructed my GameScene with that dimension. My question about the dimension of this 'bound' still remains though. Is it fixed? I'm running my simulator on IPhone 6s...

like image 117
caffeine_inquisitor Avatar answered Sep 05 '25 01:09

caffeine_inquisitor