Debug info out of iPhone X frame's bounds - swift

As you can see below in the simulator screen, the fps debug info gets cropped out.
I know it is merely a debug functionality and it will not be delivered to production but it just bugs me.
Is there a way to display the debug info correctly? Thank you.
Here is how the GameScene is initialised:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let scene: SKScene = GameScene(size: view.bounds.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}

Apparently, view is not in a safe area. Use self.view.safeAreaLayoutGuide.layoutFrame to get the frame in which it's safe to show content and it won't be blocked.

Related

SKView does not cover the entire screen area

I started studying SpriteKit recently with one famous textbook and immediately a problem arose. In all video lessons, SKView covers the entire display area. I have the opposite situation and I don't know how to fix it. Here are the screenshots:
iPhone with notch
classic iPhone
Here is the source code:
GameViewController.swift:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set the scale mode to scale to fit the window
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
let scene = GameScene(size: CGSize(width: 2000, height: 1500))
scene.scaleMode = .aspectFill
// Present the scene
skView.presentScene(scene)
}
override var shouldAutorotate: Bool {
return true
}
override var prefersStatusBarHidden: Bool {
return true
}
}
GameScene.swift:
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
backgroundColor = .brown
}
}
I don't know if anyone had such problems before the macOS 11 and Xcode 12, but now they are.
How to stretch SKView across all screen?
Set your project's Lauch screen file to "Main".

Swift blank app

I want to learn SpriteKit and am following this tutorial, https://www.raywenderlich.com/187645/spritekit-tutorial-for-beginners-2
I have copied and pasted the same code from the tutorial into GameScene.swift as well as moved the picture of the ninja into Assets.xcassets
However, when I get two errors,
Before I run it I get
Forced cast of SkView to same type has no effect
After I run it I also get
Thread 1: EXC_BAD_ACCESS(code=2, address=0x7ffeed594fa8
When I run it, the app is simply blank... The app should show FPS count and NodeCount?
Here's my code:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
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)
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
All of this code is in GameScene.swift
If someone could please help me that would be great!
UPDATE: I am using Swift 4
It seems you have an infinite recursion there. Note that you already have a GameScene instance but in the didMove you are creating another GameScene which you are presenting again which again is presented in the view and didMove is called again and so on. I think your code should look something like this:
override func didMove(to view: SKView) {
self.size = view.bounds.size
view.showsFPS = true
view.showsNodeCount = true
view.ignoresSiblingOrder = true
self.scaleMode = .resizeFill
}

Deleting the Gamescene.sks file raises CPU to 25% on device

I just created a brand new Xcode Game Project. I deleted the Gamescene.sks file. Ran it and the cpu instantly to 25-30%. I made another Xcode project just to see if I could replicate it. Ran it before deleting the file and cpu was basically 0%, but sure enough when I deleted the file and ran it again, the cpu jumped up to the same high numbers. My goal is to subclass skscene and use that without the Gamescene.sks file. Can someone explain why this happens?
I was able to replicate this behavior just deleting the .sks file.
The problem is GameViewController is looking for that file at first loading time, as you can see here:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
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)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
So you need to initialize it another way. Just as an example, you could init an empty scene like this:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
let scene = SKScene(size: CGSize(width: 1000, height: 1000))
scene.scaleMode = .aspectFill
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
and you'll see 1% of CPU load.

GameScene misses initializers

I get an error that "Class 'GameScene' has no initializers" when I try to add a level selection to my game.
I present the scene in GameViewController.swift like this:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.level(1) {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
And in my GameScene.swift I wanted to handle the level selection like this (got this from Ray Wenderlich iOS Game Development Books)
class func level(levelNum: Int) -> GameScene? {
let scene = GameScene(fileNamed: "Level\(levelNum)")!
scene.currentLevel = levelNum
scene.scaleMode = .AspectFill
return scene
}
But then I get the error as I mentioned and I dont know how to fix this issue :-? Are there any other ways to make a level selection? Currently I have for each level one sks file, which is something I find really handy, since I enables me to lay out the levels visually.
Any recommendations and/or help?
Thanks in advance

Swift -- Sprite Kit -- Scenes have different sizes

I am programming a game but I have one problem. I have two scenes. (1. GameScene 2. PlayScene). The GameScene displays the PlayButton. If you press the PlayButton the PlayScene will be displayed. The PlayScene displays the game. If you lose the Game, the GameScene will be presented again.
GameScene --> PlayScene --> GameScene
This is the code, which is in the GameScene and presents the PlayScene.
if self.nodeAtPoint(location) == self.playButton { // Wenn position des klicks gleich mit position des play button
println("Go to the game")
var scene = PlayScene(size: self.size)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
This is the code, which is in the PlayScene and presents the GameScene again.
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.size = skView.bounds.size
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
My Problem is that the GameScene has first a different size than the GameScene which is presented after the PlayScene. I can't show photos because I don't have 10 reputations -.-, but I can explain it. For example: The PlayButton in the GameScene, which is presented after the program starts, is smaller than the PlayButton, which is presented in the GameScene after the PlayScene is presented.
I tried to change the scaleModes but that didn't work.
Here are the links for the photos, which show my problem.
This is the "second" GameScene presented:
https://drive.google.com/open?id=0B57TI4Xur5Fldy1EcVh6NmVIdm8&authuser=0
This is the "first" GameScene presented:
https://drive.google.com/open?id=0B57TI4Xur5FlR3ZXT2NFSEZRZHc&authuser=0
The issue could be that you aren't using the same settings as you are for the first scene. Try this.
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.size = self.size
scene.scaleMode = self.scaleMode
skView.presentScene(scene)
}
Thank you but it didn´t worked. I tried some other changes and it worked but I don´t know why.
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
Now both Scenes have the same size.