2

I develop an universal game using Sprite Kit Level editor on Xcode 8 beta 4.

My scene in the level editor does 750x3334. In my scene I have a background image, the background image is adjusted to the scene size.

  • Scene anchor point = (0,1) Top left

  • Background anchor point = (0,1) Top left.

I need a camera inside my scene.

I set a SKCameraNode with a scale (1,1), the position of the camera is the center of my scene.

camera

To create the scene :

 if let scene = GKScene(fileNamed: "GameScene") {

        if let sceneNode = scene.rootNode as! GameScene? {

            // Copy gameplay related content over to the scene
            sceneNode.entities = scene.entities
            sceneNode.graphs = scene.graphs
            sceneNode.scaleMode = .aspectFill

            if let view = self.view as! SKView? {

                view.presentScene(sceneNode)
                view.ignoresSiblingOrder = true
                view.showsFPS = true
                view.showsNodeCount = true
            }
        }
    }

When the game is launched, my camera has to be set to have the top of the background equal to the top edge of the device.

To do that:

override func sceneDidLoad() {
    camera?.position.y = -UIScreen.main.nativeBounds.size.height/2
}

For the iPhone 6, it works perfectly. For the iPhone 5, 6+, I have a blank area (out of the scene) at the top edge.

explain

So maybe my logic is wrong...

cmii
  • 3,556
  • 8
  • 38
  • 69

2 Answers2

1

You should not be touching screen size, you need to be working with the scene size. .AspectFill retains the same scene size, regardless of device, the only scaleMode that changes scene size is .ResizeFill

To get your camera at center, you need to do

camera.position = CGPointMake(scene.frame.size.width / 2, scene.frame.size.height / 2)  

Note, scene may be self, I do not know where you will be using this code

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • 1
    the height may have to be negative, but I am not positive if you plan on using a scale of (1,-1) on the scene so that you are not working with negative numbers – Knight0fDragon Aug 11 '16 at 17:39
  • But my scene frame size does (750.0, 3334.0), doing camera?.position.y = -self.frame.size.height/2, the camera is centered at the middle of the frame (in my device I see the middle of the background image in the screen). It's not what I want to do. Or I didn't understand... – cmii Aug 11 '16 at 19:04
  • is your background bigger than the scene? I am confused as to what is going on here – Knight0fDragon Aug 11 '16 at 19:05
  • fix the question also please, you said 3334, I guess you meant 1334 – Knight0fDragon Aug 11 '16 at 19:06
  • The background and the scene does (750.0, 3334.0) 3334 and not 1334 :) – cmii Aug 11 '16 at 19:06
  • 1
    if your background and your scene is the same size.... then why do we have a camera, is the background not suppose to move? – Knight0fDragon Aug 11 '16 at 19:07
  • In few words: I have a ball which falls with the gravity. The ground is the bottom of my scene/background (-3334). I need a camera to follow the ball when it falls. – cmii Aug 11 '16 at 19:09
  • if your scene is 3334, then all 3334 pixels are on screen at once, but squished. your scene size is technically infinite, the size you specify is the amount you show on screen, So I am going to guess you want the scene size to be 1334, and your background size to be 3334, then you center your camera on the background's frame, not the scenes frame – Knight0fDragon Aug 11 '16 at 19:11
  • I have already tried to set the scene to 1334, but I need to have the ground at the bottom of the background image -3334. Or with your solution, the ground will be at -1334. My ball will be stop at 1334. FYI, I'm using SKPhysicsBody(edgeLoopFrom: self.frame). – cmii Aug 11 '16 at 19:17
  • you would put the physicsbody on the background, not the scene – Knight0fDragon Aug 11 '16 at 19:18
  • It's obvious! I didn't think to do that, stupid I am :) Thanks! – cmii Aug 11 '16 at 19:21
-1

You could get the size of users device like this

if(UIScreen.mainScreen().bounds.width == 375 && UIScreen.mainScreen().bounds.height == 667).

This is for iPhone 6 you can google the sizes and then you adjust the position of the camera manual.

Paul Heinemeyer
  • 217
  • 1
  • 2
  • 18
  • 1
    Screen size has nothing to do with this problem, they are using aspectFill, so the scene gets scaled to fill the screen, but retains the same size – Knight0fDragon Aug 11 '16 at 17:29