SpriteKit screen size problem with swift5 - swift

I try to learn SpriteKit with Xcode 12.4 and swift5.
When I made the first project and tried to run the simulator, it showed me a screen like in the below image.
You can see that screen is not full-size. So, I've changed the size of the attribute inspector in GameScene.sks but nothing changed.
GameViewController code is like below, I didn't change anything... How can I make my GameScene fullscreen on a simulator of iPhone 11?
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}
}
}

Related

I am using SpriteKit to make a game, but my view only takes up what appears to be the safe area of the screen. How do I expand this view?

I am making a game using SpriteKit, and I want it to use the whole screen. However, right now, it is only using what looks to be the safe area. How can I make the view take up the entire screen? Below is the GameViewController code I am using:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
let scene = MenuScene(size: UIScreen.main.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .fill
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
}
}
}
If you use SwiftUI, you can present your scene as a view using SpriteView. Then, you can apply the view modifier .ignoresSafeArea() to make it take up the whole screen.
Anyway, I'm a newbie, but that's what I did and it worked for me. There may be a better way, though.
Here's an example of what I'm talking about:
import SwiftUI
import SpriteKit
struct ContentView: View {
var body: some View {
SpriteView(scene: MenuScene(size: UIScreen.main.bounds.size), transition: SKTransition.reveal(with: .down, duration: 1.0))
.ignoresSafeArea()
}
}

How do I stop Xcode from launching simulator upside down and launch landscape by default?

I'm coding my first spritekit game, and every time I launch the simulator it launches the simulator upside down.
Code:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene.newGameScene()
// Present the scene
let skView = self.view as! SKView
skView.presentScene(scene)
skView.ignoresSiblingOrder = true
skView.showsFPS = true
skView.showsNodeCount = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}
----
Pictures:
Query: How do I stop XCode from launching simulator upside down, and launch landscape by default?
You can check your simulator settings once. As the image attached below.

Modification of spite node sizes when I change scene

In my app I've 2 scenes.
I use the first scene as menu scene so I create a button for presents the second scene.
But when I present the second scene the sizes of all elements of this scene they become bigger than the sizes that I've set.
The first picture is the second scene which it was presented by first scene.
The second picture is the second scene which it wasn't presented by first scene.
I want to presente the second scene from the first scene, but I want that the objects sizes are like the objects in the second scene which is showed by second picture.
In the second scene the object sizes have been set from me.
I understood and I've done in this way:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "MainScene") {
let gameScene = SKScene(fileNamed: "GameScene")
gameScene?.scaleMode = .aspectFill
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
}
}
But it doesn't work.

How to Change view's from a UIVeiwController to a Scene

I Have a UIViewController That I need and I would like it to stay a UIView Controller. but I have a back button that takes you to an Scene that's not working. i just simply need to get to the SKcene any way possible. Thanks
Code:
#IBAction func BackButton(sender: AnyObject) {
var scene = GameScene()
let sKView = self.view! as! SKView
sKView.ignoresSiblingOrder = true
scene.size = sKView.bounds.size
let reveal = SKTransition.fadeWithDuration(0.45)
sKView.presentScene(scene, transition: reveal)
}
}
this function is current giving me this Error below
Could not cast value of type 'UIView' (0x1991fa530) to 'SKView' (0x198f5f560).
My Game Scene is my SkScene and my UIViewController that is attached to the storyboard view is called SoundboardController

In swift, how to get memory back to normal after an SKScene is removed?

I created a simple game with SpriteKit, however every time I run the game, the memory usage in simulator increases about 30mb, but never decreases when the game is finished.
When I run the game over ten times the simulator gets slower and slower and eventually crashes.
In this simple game I have two controllers and a gamescene:
MainController calls GameViewController via a button triggered
In GameViewController, gamescene is initialised in this way:
class GameViewController: UIViewController
{
var skView:SKView!
var scene:GameScene!
override func viewDidLoad() {
super.viewDidLoad()
scene = GameScene(size: view.bounds.size)
skView = view as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.viewController = self
skView.presentScene(scene)
}
//with a prepareForSegue deinitialises the scene and skview:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "GameFinished"{
scene.removeAllActions()
scene.removeAllChildren()
scene.removeFromParent()
scene = nil
skView.presentScene(nil)
skView = nil
let target = segue.destinationViewController as MainController
}
}
}
In the GameScene, viewController is a property
var viewController:GameViewController? = GameViewController()
the segue is triggered with this:
self.viewController!.performSegueWithIdentifier("GameFinished", sender: nil)
I've also tried putting remove methods into deinit in GameScene:
deinit{
self.removeAllActions()
self.removeAllChildren()
}
Still wouldn't work
Your GameViewController has a strong reference to your GameScene. And your GameScene had a strong reference to your GameViewController. This leads to a strong reference cycle, which means that neither objects will be deallocated.
You need to declare your viewController property in your GameScene as weak.
weak var viewController:GameViewController? = GameViewController()
Using Swift 3, Xcode 8 and iOS 10.
After avoiding strong references, taken care of SKTextures, etc. memory level didn't recover after dismissing the scene and returning to the "menu" viewController.
I was using:
override func sceneDidLoad() {...}
This is available in iOS 10 but I wanted iOS 8&9 compatibility that's why I've
changed to the older:
override func didMove(to view: SKView) {...}
Beside getting compatible with older iOS versions, it turns out that the memory level drops after dismissing the scene. This was a surprise. I'm missing probably some leaks but it's working for me.
I hope it helps someone.