Pedro Cabaco
Pedro Cabaco

Reputation: 185

Spritekit scale full game to iPad

So I'm developing a game on Xcode using Spritekit, for the iPhone. And it looks all the same and great on all iPhone devices, by using the node.setScale() method. Now I want to make it Universal and runnable on the iPad as well. How do I do this easily? Is there some formula for the scale?

What I already tried was this:

if UIDevice.current.userInterfaceIdiom == .pad {
        sscale = frame.size.height / 768
    }else{
        sscale = frame.size.height / 320
    }

And then for every node I make node.setScale(sscale).

This ends up not working because on the iPad Mini it looks different from the iPad Air... i.e node sizes and positions...

Any help?

Upvotes: 4

Views: 1951

Answers (2)

crashoverride777
crashoverride777

Reputation: 10674

You basically have 2 options.

1) Set scene size to 1024X768 (landscape) or 768x1024 (portrait) and use the default .aspectFill for scale mode. This was the default setting in Xcode 7 (iPad resolution).

You than usually just show some extra background at the top/bottom (landscape) or left/right (portrait) on iPads.

Examples of games that show more on iPads:

Altos Adventure, Leos Fortune, Limbo, The Line Zen, Modern Combat 5.

2) Apple changed the default scene size in xCode 8 to iPhone 6/7 (750*1334-Portait, 1337*750-Landscape). This setting will crop your game on iPads.

Chosing between the 2 options is up to you and depends what game you are making. I usually prefer to use option 1 and show more background on iPads.

Regardless of scene size scale mode is usually best left at the default setting of .aspectFill or .aspectFit

To adjust specific things such as labels etc for a device you can do it this way

 if UIDevice.current.userInterfaceIdiom == .pad {
   // adjust some UI for iPads
 }

I would not try to do some random hacks where you manually change scene or node sizes/scales on difference devices, you should let xCode/SpriteKit do it for you.

Hope this helps

Upvotes: 7

Mark Brownsword
Mark Brownsword

Reputation: 2307

If you are using SKCameraNode then you can scale the entire scene by scaling the camera.

// SKCameraNode+Extensions.swift
extension SKCameraNode {
    func updateScaleFor(userInterfaceIdiom: UIUserInterfaceIdiom) {
        switch userInterfaceIdiom {
            case .phone:
                self.setScale(0.75)
            case .pad:
                self.setScale(1.0)
            default:
                break
        }
    }
}

Then call it when your scene initializes.

guard let camera = self.childNode(withName: "gameCamera") as? SKCameraNode else {
    fatalError("Camera node not loaded")
}

// Update camera scale for device / orientation
let userInterfaceIdiom = UIDevice.current.userInterfaceIdiom 
camera.updateScaleFor(userInterfaceIdiom: userInterfaceIdiom)

Upvotes: 2

Related Questions