SpriteKit SKScene not resizing correctly to fit iPhone 12 - swift

I've created a new SpriteKit project and want the scene to fill the screen entirely. I have defined the GameScene with iPhone 12 screen dimensions (390pt x 844pt) and specified the following in GameViewController.swift:
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}
However, there is still a large amount of unoccupied space above and below the SKScene. I can't figure out what I've done wrong, and any help would be appreciated.

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.

Related

How to prevent distorted images?

I have the problem that the images I add are distorted. I have created a pixel accurate background for the iPhone X at (1125 x 2436), so I don't have to use .aspectFill and .aspectFit because I want a screen without black borders.
I use the following code to create the images:
func animateDeck() {
let chip = SKSpriteNode(imageNamed: "Chip")
chip.position = CGPoint(x: 300, y: 400)
chip.zPosition = 2
chip.setScale(1)
gameScene2.addChild(chip)
print("test")
}
Is there a way to display the images in their correct size without using .aspectFit or .aspectFill?
now (left) and how it should be (right)
Thank you in advance!
Check out this project I just made to show you how to create a texture and apply it to a node. All you need should be in GameScene.swift.
Also, in your ViewController, make sure that your GameScene is initialised properly as shown in my project, or how you did it with this:
gameScene2 = GameScene(size: view, bounds: size)

SpriteKit Memory leaks changing Scenes containing SKTileMapNodes

I am trying to create a simple 2d platform game using Swift, SpriteKit, and SKTileMaps. But every time i change between scenes containing SKTileMaps I see a lot of memory leaks in the Xcode Instruments.
I have recreated the problem as simple as I can. I am using a .sks file to create the scene and this file only contains 1 tileMap filled with some tiles.
The code in the view controller for presenting the scene:
if let view = self.view as! SKView? {
let scene = LoadingScene(size: CGSize(width: 2048, height: 1536))
scene.scaleMode = .aspectFill
view.presentScene(scene)
The code for the scene:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let scene = GameScene(fileNamed: "WorldScene") else{fatalError("Could not open world scene")}
view?.presentScene(scene)
}
}
I am choosing GameScene as the custom class int the .sks scene file.
This will result in a lot of small memory leaks each time I change the scene:
picture of memory leak in Instruments
This is the leak from only one scene change. Am I doing something wrong or is this a SpriteKit bug?
Edit1
The SKCTileMapNode::_ensureChunkForTileIndex(unsigned int) leaks happen each time i load the tile map, while the rest only appear when changing a scene
Edit2
Changed the GameViewController to skip the LoadingScene and go straight to GameScene. The memory leak is still there:
if let view = self.view as! SKView? {
guard let scene = GameScene(fileNamed: "WorldScene") else{fatalError("Could not open world scene")}
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
I have come across this same issue and after looking into it, I believe it is a problem that is inherent to all SKTileMapNodes. It is a bug in SpriteKit.
When you use an SKTileMapNode with ANY tiles filled (not a blank tile map), then the memory for the tile map will persist even when loading subsequent scenes. If you keep loading levels with SKTileMapNodes, then the memory will keep increasing until the game ultimately crashes. I have tested this with different games I have coded and using the code of others as well.
Interestingly, if the tile map has all blank tiles (even if it has an SKTileSet assigned), then no memory leak will occur.
From the best that I can guess, when an SKTileMapNode has any tile in it besides blank tiles, the entire SKTileSet that it is using, is being kept in memory and never gets removed.
From my experiments, you can prevent this problem by swapping the SKTileSet with a blank tileset in didMove. You cannot do it anywhere else except within didMove (or a function called by didMove).
So my solution has been to extract the tile set into individual sprites and then "nullify" the tileset on the tilemap. You can do so with the following code:
extension SKTileMapNode {
func extractSprites(to curScene: SKScene) {
for col in 0..<numberOfColumns {
for row in 0..<numberOfRows {
if tileDefinition(atColumn: col, row: row) != nil {
let tileDef = tileDefinition(atColumn: col, row: row)
let newSprite = SKSpriteNode(texture: tileDef!.textures.first!)
curScene.addChild(newSprite)
let tempPos = centerOfTile(atColumn: col, row: row)
newSprite.position = convert(tempPos, to: curScene)
}
}
}
eraseTileSet()
}
func eraseTileSet() {
let blankGroup: [SKTileGroup] = []
let blankTileSet = SKTileSet(tileGroups: blankGroup)
tileSet = blankTileSet
}
}
Basically within didMove, you will need to call extractSprites on each SKTileMapNode. This will simply create SKSpriteNodes from each tile and put them into the scene. Then the SKTileSet will be "switched" to a blank one. Magically the memory leak will disappear.
This is a simplified solution which you will need to expand upon. This only puts the sprites there, but doesn't define how they behave. Sorry but this is the only solution I have found and I believe your problem is a major bug in SpriteKit.

Swift multiple scenes

I have made a swift SpriteKit game but now I want to add a menu scene(and eventually a lose scene). So what I did was
I created a new scene called Menu scene.
I edited GameViewController and changed it to load menu scene(it worked).
In MenuScene.swift I put some code in so when a sprite was clicked it would run this code:
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: size)
self.view?.presentScene(scene, transition:reveal)
But when I click the button it flips round to the next scene and immediately crashes.
So my question is: Am I doing this the right way and if I am why does it crash?
It turns out I wasn't deleting a particle emitter so I put in a bit of code to delete it just before the transition and it worked.
If you do, it should work:
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: self.frame.size)
let skView = view as SKView!
skView.presentScene(scene, transition:reveal)
I hope this works for you.
Since you're in sprite kit , it's wrong to use :
self.view?.presentScene(scene, transition:reveal)
You should do the following :
let scene = GameScene(size: view.bounds.size)
let skView = view as SKView
//skView.showsFPS = true
//skView.showsNodeCount = true
//skView.ignoresSiblingOrder = true
//scene.scaleMode = .ResizeFill
skView.presentScene(scene, transition:reveal)

resizing scene with swift and spritekit

Im making a game with swift and spriteKit with xcode 6.1.
My problem is that, when I run the game on the iPhone 6 or iPhone 6+ the scene/images dont resize properly
I have this on my GameViewController
// Configure the view.
let skView = view as SKView
skView.multipleTouchEnabled = false
// Create and configure the scene.
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
// Present the scene.
skView.presentScene(scene)
And this on my GameScene
override init(size: CGSize) {
super.init(size: size)
anchorPoint = CGPoint(x: 0.5, y: 0.5)
let background = SKSpriteNode(imageNamed: "Background")
addChild(background)
}
I have tried all the scale modes but they dont seem to work. I hope someone can tell me what is going on
The size of the scene is being set to the size of the view in
scene = GameScene(size: skView.bounds.size)
so it is only the images in the scene that are too small to fill the view.
Make these changes to your GameViewController:
if you set the size of the scene to the size of the background image:
scene = GameScene(size: CGSize(width: backgroundImageWidth, height: backgroundImageHeight))
(replace backgroundImageWidth and backgroundImageHeight with actual values)
then set the scene's scale mode to fill the view:
scene.scaleMode = .Fill
(this may make the scene look stretched, you could use other scale modes)

Why won't my SKSpriteNodes appear in the scene?

I am trying to add a couple of nodes to the scene. When I run the app I just have the default gray background. Neither of the nodes appear in the scene. Here is what I have:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let jumper = SKSpriteNode(imageNamed:"Jumper")
jumper.xScale = 0.25
jumper.yScale = 0.25
jumper.position = CGPointMake(50, 300)
self.addChild(jumper)
let blob = SKSpriteNode(imageNamed:"Blob")
blob.position = CGPointMake(160, 30)
blob.xScale = 0.5
blob.yScale = 0.25
self.addChild(blob)
}
I have checked the image titles and everything is correct there.
The Problem
Your sprites are not visible because your scene is not completely within the simulator's window. By default the scene's size is 1024 x 768 (specified by GameScene.sks), while the simulator is displaying only 375 x 667 points (if you selected the iPhone 6/6s simulator). Unless the scene's size is set correctly, the bottom-left corner of the simulator's window will not be the (0,0) position of your scene.
To correct this issue,
Method 1:
Change the scene's size property in GameScene.sks to match the size of the view. I suggest you set the scene size to 375 x 667 (iPhone 6/6s). If you do this and set the scene's scaleMode property to .AspectFill (the default value), SpriteKit will automatically scale the scene to fit in the view of the iPhone 6+, 6s+, 5, and 5s. This will save you time when porting your game to these devices.
Method 2:
In your view controller, replace this
scene.scaleMode = .AspectFill
with this
scene.scaleMode = .ResizeFill
Warning: Using this method will work but may require additional effort to port your game to other iOS devices with a different screen size.