3

I'm currently working on a Spritekit Game with a SKCameraNode allowing the player to look at a strategic map. I am trying to implement a HUD by adding child nodes to the SKCameraNode, but I have run into a problem concerning how the coordinates of the child nodes should be set.

I initialize my game scene as so:

    mainScene = GameScene(size: self.view.bounds.size)
    main = view as! SKView

    mainScene.panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(shiftView(recognizer:)))
    main.addGestureRecognizer(mainScene.panRecognizer)

    mainScene.tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(testTap(recognizer:)))
    main.addGestureRecognizer(mainScene.tapRecognizer)

    mainScene.pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(zoomView(recognizer:)))
    main.addGestureRecognizer(mainScene.pinchRecognizer)

    let viewSize = mainScene.size
    mainScene.tempGameBoard.boardCamera.createHUD(size: viewSize, for: mainScene.tempGameBoard.boardCamera.xScale)
    print("cameraframe: \(mainScene.tempGameBoard.boardCamera.frame)")

    main.showsFPS = true
    main.showsNodeCount = true
    main.showsDrawCount = true
    mainScene.scaleMode = .aspectFill
    main.presentScene(mainScene)

Testing on an iPhone 7, I print the gameScene size and get (375, 667) and my camera frame is then (667, 375, 0, 0)

But, testing on an iPhone SE, I print the gameScene size and get a result of (320, 568) while the camera frame remains (667, 375, 0, 0)

It seems no matter what device I test on, the gameScene changes, but the camera frame is the same size. This confuses me endlessly. Where is the camera obtaining its frame parameters from??

Further, what coordinates could I possibly, reliably use to actually setup a HUD?

Appreciate any help! Thank you

Aleksandr
  • 533
  • 1
  • 4
  • 12
  • i think the issue is not in the code you shared. Maybe in the createHUD method? – sabi Jan 25 '17 at 09:06
  • Do not do `GameScene(size: self.view.bounds.size)` with `mainScene.scaleMode = .aspectFill` Even though the code works, it does not make sense. Use a `.resizeFill` scale to make your code less confusing. – Knight0fDragon Jan 25 '17 at 14:50
  • 1
    Could you explain why the `.aspectFill` does not make sense and `.resizeFill` does? – Aleksandr Jan 25 '17 at 15:22
  • we are telling scene how to scale, that is the point of scale mode. Since you are doing `GameScene(size: self.view.bounds.size)` You are creating scenes of a dynamic size, so the only time scaling would happen is if you were to resize the view, which in mobile gaming does not work 95% of the time. If you were to use a static size `GameScene(size: CGSize(width:320,height:568)` then aspectFill makes sense because you are scaling this to fill the view. So the view gets filled, but the coordinate system you work in always stays the same. – Knight0fDragon Jan 25 '17 at 15:38
  • 1
    That makes sense, however, without the ScaleMode setting, whenever I rotate the view to a landscape orientation it squishes the scene, while having a ScaleMode set makes sure that it stays the same visually speaking. Is that a part of the "resizing the view" comment you made? – Aleksandr Jan 25 '17 at 15:41
  • 1
    yes if you have it on rotate, your problems become worse. In your scenario, If you design in portrait mode, and go to landscape, all you are doing is cropping the top and bottom of the scene (which is almost 44% of your scene; 7/16) – Knight0fDragon Jan 25 '17 at 16:35
  • 1
    Now your camera is a different matter. Do not think of your camera as a box, think of it as a point that expands infinitely. If you want to get the view your camera sees, then you take the camera point and add on the scenes frame. – Knight0fDragon Jan 25 '17 at 16:37
  • 1
    Wow, okay that makes a lot of sense. I didn't think to look at it that way. But wouldn't the scene's frame always be the same regardless of scroll height? Because as I understood, the camera node simply changes how much of the scene is seen. Also, any advice then for changing orientation and keeping this consistent? – Aleksandr Jan 25 '17 at 18:16

0 Answers0