Changing the initial scene in spritekit game - ios

Similar to this question, I'm trying to change the initial scene of my game to FirstScene.swift.
I changed the following in GameViewController.swift:
from:
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
}
}
to:
if let scene = SKScene(fileNamed: "FirstScreen")
FirstScreen.swift starts with
import SpriteKit
class FirstScreen: SKScene, SKPhysicsContactDelegate {
Also tried :
if let scene = FirstScreen(fileNamed: "FirstScreen")
I've also tried a new project and still no luck! Any ideas please?

Do it this way:
let scene = FirstScene()
scene.scaleMode = .aspectFill
view.presentScene(scene)
Note that the SKScene(fileNamed: String) constructor is used to unarchive a scene from a .sks file.
I am assuming you do not have a FirstScene.sks file in your project.

Related

How to use preload in spritekit

Since I am new to swift and spriteKit, I have read the document from apple:
preload document from Apple
I tried to load some images like this code below:
let textureArray = [SKTexture(imageNamed: "enemy1"),SKTexture(imageNamed: "enemy2"),SKTexture(imageNamed: "enemy3")]
override func viewDidLoad() {
super.viewDidLoad()
SKTexture.preload(textureArray) {
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
}
}
}
Now, my question is how to use the textures I have loaded? For example, in another swift file, I have a class called player, how could I use the textures I have loaded to crate a SkspriteNode? How to pass the textureArray to that class?

Swift: Load the SKScene from GameMenu.sks doesn't work

i have the problem, that the GameViewController doesn't load my GameMenu. If i simulate my app it only shows a grey screen with node:0 and the fps count.
Here the Code from GameViewController.swift:
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = GameMenu(fileNamed: "GameMenu") {
// 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
}
}
I tried also the line:
if let scene = SKScene(fileNamed: "GameMenu")
but it changed nothing.
I also added the Custom Class "GameMenu" to the GameMenu.sks
I think the problem is this if let scene = SKScene(fileNamed: "GameMenu") line.
I have read a lot about this problem but i my case nothing was successful.
I also tried this:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let gkScene = GKScene(fileNamed: "GameMenu.sks") {
if let skScene = gkScene.rootNode as? SKScene {
// Set the scale mode to scale to fit the window
skScene.scaleMode = .aspectFill
// Present the scene
view.presentScene(skScene)
}
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
but nothing happend.
You have to load it as GKScene from .sks file and get SKScene from its rootNode attribute.
if let gkScene = GKScene(fileNamed: "GameMenu") {
if let skScene = gkScene.rootNode as? SKScene {
// TODO
}
}

Swift: How to get the size of a Spritekit Scene to present a SKNode fullscreen?

I am creating a simple Game and I would like to get the actually size (width/height) of a SpriteKit scene to be able to present a SKNode ( / SKSpriteNode) to fill the whole display, but apparently
backgroundNode.size = CGSize(width: view.frame.width, height: view.frame.height)
or anything similar doesn't work. The node is presented, but is just a quarter of the actually screen size.
I present the SKScene from GameViewController.swift like this:
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
let scene = GameScene(size: view.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = true
}
So how could one get the actually SpriteKit Scene size or is there another way to present an SKNode / SKSpriteNode fullscreen in a GameScene?
Update: I tried this as well:
let displaySize: CGRect = UIScreen.main.bounds
let displayWidth = displaySize.width
let displayHeight = displaySize.height
which gives me for an iPhone 8 375x667 as a size, but the Node is still displayed as a quarter of the screen.
I created a sample project with the code below. This creates a red-colored node that fills the screen. You can use UIScreen.main.bounds and I'm using it here as a global variable. I find it helpful this way since I can access it from any code file, as I often need to use the screen size in a calculation.
GameViewController.swift
import SpriteKit
let displaySize: CGRect = UIScreen.main.bounds
class GameViewController: UIViewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let skView = self.view as! SKView
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
scene.size = view.bounds.size
skView.ignoresSiblingOrder = true
skView.presentScene(scene)
}
}
}
GameScene.swift
import SpriteKit
class GameScene: SKScene {
let backgroundNode = SKSpriteNode()
override func didMove(to view: SKView) {
backgroundNode.size = CGSize(width: displaySize.width, height: displaySize.height)
backgroundNode.color = .red
self.addChild(backgroundNode)
}
}

gameViewController background color cannot be changed

The App running under spriteKit. Days before, the gameScene was loaded once the gameViewController was loaded. Today, I change its loading action that the gameScene is loaded only when the Start button is clicked, it means the gameScene background color is invisible, I need to set a background color for gameViewController. It's a very very simple task being done many times
before, but it sticks on light gray(image below).
Can't gameViewController background color be changed under spriteKit?
Must I change gameScene background color instead?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
// .......
}
#IBAciton func loadGameView() {
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
// if let scene = SKScene(fileNamed: "GameScene") {
if let scene = GameScene(fileNamed: "GameScene") {
scene.viewController = self
scene.size = self.view.frame.size
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = false
view.showsFPS = false
view.showsNodeCount = false
view.showsDrawCount = false
}
}
if let scene = GameScene(fileNamed: "GameScene") {
scene.viewController = self
scene.size = self.view.frame.size
scene.scaleMode = .aspectFill
//ADD THIS
scene.color = .green
scene.colorBlendFactor = 1
// Present the scene
view.presentScene(scene)
}

GameViewController to GameScene using presentScene with SKTransition

Tried to perform an SKTransition for the very first loading of GameScene from GameViewController but nothing seemed to happen. Can't we perform some transition while presenting a scene directly from the GameViewController? It seemed to me quite hilarious that the normal present scene is working but not the one with transition. Here is the code :
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let scene = GameScene(size: view.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
//transition
let transition = SKTransition.doorsOpenVertical(withDuration: 2)
// Present the scene
view.presentScene(scene, transition: transition)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
self.backgroundColor = SKColor.red
}
}
Please solve the query.

Resources