Spritekit Skscene Not Resizing Correctly to Fit iPhone 12

SpriteKit SKScene not resizing correctly to fit iPhone 12

Fix (credit @aheze)

As mentioned here, this can be fixed by setting the launch screen file in your project settings under App Icons and Launch Images.

Swift & SpriteKit: Full screen image

You need to add

scene.size = skView.bounds.size

or for Swift 5

scene.size = self.view.bounds.size

just before skView.presentScene(scene) in your gameViewController file

How to make SKScene have fixed width?

Giving the scene a fixed size is actually what we want to do in SpriteKit games. SpriteKit will than scale the game for each device using the scaleMode settings (defaults to .aspectFill). Its not a good idea to make the scene the size of the device or use .resizeFill for scale mode as that will lead to massive inconsistencies on different devices. I have been there before with 1 game and it was an absolute nightmare.

So we basically have 2 ways to do it correctly

1) Set scene size to iPad (e.g 1024x768 -landscape, 768x1024 - portrait. This was the default setting in Xcode 7.

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.

Examples of games that show less on iPads:

Lumino City, Robot Unicorn Attack

Choosing between those 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. This way you will have a consistent experience on all devices.

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.

In code you would initialise a SKScene with your preferred size like so

let gameScene = GameScene(size: CGSize(width: 1024, height: 768))

If you use the visual scene editor you set the scene size directly in the inspector panel on the right.

Hope this helps

Unable to properly size SKScene when working with SwiftUI

In SwiftUI you need to use GeometryReader as a proxy to get the view's current size (this happens every time the view is rebuilt). You then want to resize your UIViewRepresentable based on the size of its parent only if the size has actually changed. I have a GeometryReader example here: https://github.com/joshuajhomann/SwiftUI-Spirograph



Related Topics



Leave a reply



Submit