SpriteKit - How do I handle multiple screen sizes? - ios

My new game is finished and now I'm just testing on multiple real devices. I came across multiple issues after testing. The big issue is how to handle screen sizes. The game is how I want it to look on 6 Plus/6s Plus, but not on 6s, 6, 5s, 5, 4s, 4, or iPad.
I found these two answers but I don't know how to implement them: How to support multiple screen sizes in SpriteKit?
and SpriteKit how to get correct screen size
I really would like any type of help, because this is irritating me.

Its quite a commonly asked question.
What we usually do in SpriteKt is to give the SKScene a fixed size and let SpriteKit do the scaling for you on different devices.
So basically we 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 have/show some extra background at the top/bottom (landscape) or left/right (portrait) on iPads which gets cropped on iPhones.
Examples of games that show more on iPads / crop on iPhones:
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.
For scale mode it is usually best to either use .aspectFill or .aspectFit.
You would use the Universal asset slot and/or device specific images. This way you will have a consistent experience on all devices
Spritekit scale full game to iPad
How to make SKScene have fixed width?
Hope this helps

In my case I have a portrait game, and I want to keep the height fixed while showing more or less content left/right depending on the device's screen.
For that in Xcode's Scene Editor I added and set an SKCameraNode for my scene, and set the scene size to the "widest" aspect ratio device (iPhone X ), that is 2436x1125 pixels.
Then set a custom class for the scene and override sceneDidLoad:
override func sceneDidLoad()
{
super.sceneDidLoad()
self.size.width = self.size.height * (UIScreen.main.bounds.size.width / UIScreen.main.bounds.size.height)
self.scaleMode = .aspectFit
}
If I want to preview how my game will look in the "narrowest" device (any iPad has a 1.5:1 ratio) I just temporarily change my scene's width to around 1500 pixels.
Note: SKView's contentMode changes nothing.

I was reminded of checking the screen size and changing the node sizes, I found this as an answer: how to check screen size of iphone 4 and iphone 5 programmatically in swift
All I had to add was this in my GameScene and it was called in every .swift:
extension UIScreen {
enum SizeType: CGFloat {
case Unknown = 0.0
case iPhone4 = 960.0
case iPhone5 = 1136.0
case iPhone6 = 1334.0
case iPhone6Plus = 1920.0
}
var sizeType: SizeType {
let height = nativeBounds.height
guard let sizeType = SizeType(rawValue: height) else { return .Unknown }
return sizeType
}
}
And this
if UIScreen.mainScreen().sizeType == .iPhone4 {
// Make specific layout for small devices.
}

Related

Scale sprite kit game for all devices

When creating my game in spriteKit I used the game size 1080x1920. Now however If I am to run it on Ipad it looks bizarre. Is there an easy fix? I am simply worried that my app will get rejected. Thank you in advance?
Its quite a commonly asked question.
What we usually do in SpriteKt is to give the SKScene a fixed size and let SpriteKit do the scaling for you on different devices.
So basically we 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 have/show some extra background at the top/bottom (landscape) or left/right (portrait) on iPads which gets cropped on iPhones.
Examples of games that show more on iPads / crop on iPhones:
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, 1334*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.
Regardless of scene size scale mode is usually best left at .aspectFill or .aspectFit, again depending on what you prefer and need (e.g cropping with aspectFill or black bars with aspectFit)
You would use the Universal asset slot and/or device specific images. This way you will have a consistent experience on all devices
Spritekit scale full game to iPad
How to make SKScene have fixed width?
Hope this helps
When you present the scene's you also set the aspect ratio.
example :
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
There are 4 different aspect radios you can set
aspectFit
aspectFill
fill
resizeFill
You can find out about the 4 different types here in the apple documents.
It looks to me that you currently have it set to aspectFill but you would be better off using aspectFit. This will create a black bar on the top and bottom on some devices but it will keep the aspect ratio the same.
If you want it to look good on all devices(no black bar) you would need to check which device it is running on then update the size and position of your sprites accordingly.
Whenever scenes are hard coded, (not made within the editor) using .resizeFill will ensure that the width AND height of the scene ALWAYS conform to the view. So your game will scale correctly on any device. For scenes that are made within the editor, this is not the case.
Personally, I hardcoded all the levels in my game "Astro Path" (it's on the app store for reference) and used .resizeFill so it would scale correctly on any device without any cropping or black bars. (who wants that stuff yuck) For my title screens, I used:
.aspectFill on iPhone
.fill on iPad
because they were designed within the editor. I checked for the current device using: if UIDevice.current.userInterfaceIdiom == .pad { do w/e }

How to make SKScene have fixed width?

I am making a spriteKit based game and wonder if I can make the SKScene fixed width (to make it easier to layout the sprites), and let it resize to the actual screen width while keeping the aspect ratio. Is it possible to achieve this goal and is it recommended (will sprites not be rendered clearly due to resizing)?
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
Any resizing comes with a performance penalty and resizing artifacts such as blurred edges or others.
Scene size is defined by the ViewController when it instantiates the scene before presenting it. Isn't it better to create the scene with the same size as the actual screen size and don't resize it any further?

Sprite Kit Scene Editor GameScene.sks scene width and height

I'm using the spritekit scene builder to build my scene (aka the GameScene.sks file when you create the project in xcode 7 ios9). SpriteKit only uses pixel values when setting the screen height and width. My question is this:
If I set the pixels values to that of an iphone 6, will the game be too small for ipad and iphone 6+? Why is there no option to fit the device screen? What should I do to avoid this?
Pixel density
First of all the size of the Scene is defined in Points not Pixels.
Let's see how the devices that support iOS 9 deal with this:
1 point = 1x1 pixel: iPad 2, iPad mini
1 point = 2x2 pixels: iPhone 4s, iPhone 5, iPhone 5c, iPhone 5s, iPhone 6, iPhone 6s, iPad mini 2, iPad mini 3, iPad mini 4, iPad 3,
iPad 4, iPad Air, iPad Air 2, iPad Pro, iPod touch 5, iPod touch 6
1 point = 3x3 pixels: iPhone 6 Plus, iPhone 6s Plus
This allow you to specify a size that is automatically converted for the pixel density of a particular device.
Screen size
There are several screen sizes available on the iPhone/iPad supported by iOS 9.
With SpriteKit you can easily face this problem setting the scaleMode property of your Scene.
You can chose among 4 options:
Fill: Scale the SKScene to fill the entire SKView.
AspectFill: Scale the SKScene to fill the SKView while preserving the scene's aspect ratio. Some cropping may occur if the view has a
different aspect ratio.
AspectFit: Scale the SKScene to fit within the SKView while preserving the scene's aspect ratio. Some letterboxing may occur if
the view has a different aspect ratio.
ResizeFill: Modify the SKScene's actual size to exactly match the SKView.
You probably want to set AspectFill but it really depends on your game.
To set the property open GameViewController.swift (or .m if you use Objective-C), in viewDidLoad you'll find this line. Just change it to mach your preference.
/* Set the scale mode to scale to fit the window */
gameScene.scaleMode = .AspectFill

How to scale app so iPhone 6 is identical(zoomed) to iPhone 5 SpriteKit

Ive been making an app using sprite kit in Xcode 7. I made all my sprite nodes positioned around by referencing other sprite nodes and self.frame
for example
self.block.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)
When I came to test my app on an iPhone 6 Plus (I previously tested on iPhone 4s but it worked nicely on an iPhone 5) My app didn't scale the nodes, Just the space Between them. The game relies on everything being the same otherwise people playing on an iPhone 6 would find it easier then people playing on a smaller device. Is there anyway that I could make the iPhone 6 view just a stretched version of the iPhone 5 view? Thanks Guys!
It's best not to position your sprites using self.frame as a reference. Much better to treat the scene as the "true" coordinates and let SpriteKit scale your scene for you.
ie. Specify the scale mode of your scene when you create it (probably in the view controller viewDidLoad). I prefer AspectFill for the scale mode, which makes sure the screen is filled, but you will lose the edges on some devices. Just make sure the core gameplay is in the middle of the scene.
scene.scaleMode = .AspectFill
Then, position your sprites using the size of your scene for reference. On a landscape scene, I tend to use a scene size of 1024x768 (iPad size).
Hope that helps.
Another way: If you remove the iPhone 6 and iPhone 6+ launch images from the project then those devices will switch to "zoom mode", which is exactly the same view as on iPhone 5, just bigger (well, zoomed).
Just look in your project for "Images.xcassets", there is a image-set called "LaunchImage". In this image-set are multiple other images, just remove the one for iPhone 6 and 6+.

game works well on all devices but iPad 2, why?

I am working in Swift and have created a basic game using SpriteKit in which squares move down the screen repeatedly and a sprite node "jumps" when tapped. Relatively simple.
The game runs and looks as it should on every device, including the iPad air and iPad Retina, except for the iPad 2.
When I run the game on the iPad 2 simulator, the squares are way too big, have no space in between them, the node is way too big as well and only jumps maybe half a centimeter on the screen when tapped. The game is a mess.
Why is this occurring? Is there a way to specify in the code: if the device is an iPad 2, format and size everything in this manner? Or is there another way to fix this?
The iPad 2 has the same resolution of the original iPad (1st Gen), so it has a resolution scale of 1.0. All you need to do is to check the iPad resolution and deppending on the results do what you need. Just add this extension and usage is pretty simple:
extension UIDevice{
var iPad2: Bool {
userInterfaceIdiom == .pad && UIScreen.main.scale == 1
}
var iPadRetina: Bool {
userInterfaceIdiom == .pad && UIScreen.main.scale == 2
}
}
usage:
if UIDevice.current.iPadRetina {
// do whatever you need for the iPad with Retina Screens (2x)
}
if UIDevice.current.iPad2 {
// do whatever you need for the iPad 2 without Retina (1x)
}

Resources