New SKScene Set as Default in SpriteKit Game Turns Out Blank - ios

I have 2 SKScene.
GameScene
WelcomeScene.swift
I wanted my screen to load up "WelcomeScene" first instead of the default GameScene.Thus, I've edited "GameViewController.swift" accordingly. However, all I get is a blank screen, and couldn't locate what went wrong.
Here's my code in "GameViewController".
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "WelcomeScene") {
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}

If you have a corresponding SKS file created in the Scene editor then you load your scene like so...
// Load the SKScene from WelcomeScene.sks'
if let welcomeScene = WelcomeScene(fileNamed: "WelcomeScene") {
welcomeScene.scaleMode = .aspectFill
view.presentScene(welcomeScene)
}
if you do not have a corresponding SKS file created in the scene editor but would rather load the scene that was created in code use...
welcomeScene= WelcomeScene(size: skView.bounds.size)
welcomeScene.scaleMode = .aspectFill
skView.presentScene(welcomeScene)

Related

Function Calls Between GameScene and GameViewController using SKSprit

So I writing a program in swift, and am having trouble understanding the proper communication between GameScene and GameViewController:
Basically I have 'savefiles' which are plists, when loaded using a function "self.loadFromPlist(fileName: )" are loaded by clearing the previous array of nodes, and drawings a set of SKShapeNodes based on attributes from the dictionary elements of the loaded Plist
I needed a file browser, which I've implemented from a Cocoapod (FileBrowser), that must be called in the "GameViewController"
My Problem is this:
When calling my 'loadFromPlist' function from inside 'viewDidLoad' like this (to initialize the project) - the function works and the gamescene is drawn:
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)
let gameScene = scene as! GameScene
gameScene.loadFromPlist(scoreFileName: gameScene.defaultScore)
gameScene.gameViewControllerDelegate = self
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
When I try to do same thing in my own function in the GameViewController (called view a GameviewDelete in GamScene), the code executes the line and I can follow it with the debugger into the 'loadFromPlist' function in the gamescene, it executes the for loop drawing shapes etc, but it just does nothing.
func callloadplist() {
var loadedfilename2:String!
let file = FileBrowser()
present(file, animated: true, completion: nil)
if let scene = GameScene(size: view.frame.size) as? GameScene {
var strippedfilename: String?
file.didSelectFile = { (file: FBFile) -> Void in
let loadedfilename = file.displayName
scene.filebrowserselectedfilename = loadedfilename
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let myskscene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
let gameScene = myskscene as! GameScene
let strippedfilename = String(loadedfilename.dropLast(6))
print(strippedfilename)
gameScene.loadFromPlist(scoreFileName: "output")
}
}
}
}
}
So the function call working inside of 'viewDidLoad()' and also as 'self.loadFromPlist(fileName: )' in GameScene.swift, but cannot be called from a custom method/ function I've made in GameViewControllers -- Any Insights?
Thanks
So file.didSelectFile is setting up a closure to respond to the event of the file being selected. This was then running the function called on a different instance of a 'GameScene' Object.
I was able to refine my question and answer it here:
Swift - SKSprite Kit - Change Gamescene Variables from FileBrowser Closure

How to create new scene from .sks file and programmatically change its size

Right now I load my GameScene like this
if let scene = SKScene(fileNamed: "GameScene") {
if bool {
// Change size to CGSize(a, b)
} else {
// Change size to CGSize(x, y)
}
view.allowsTransparency = true
scene.backgroundColor = .clear
view.presentScene(scene)
}
However, depending on a certain bool I want to be able to change the initial size of the GameScene. I know this can be done using let scene = GameScene(size: size) but that means I can't use the .sks file for the GameScene which I have most stuff on.
How can I variably change the initial size of the GameScene while still using the .sks file?
P.S. scene.size = CGSize(a, b) does not seem to change anything

Why can't I set SKScene(fileNamed:) in GameViewController?

The relevant code is copied below and I also put a simple test project on Github to demonstrate this situation:
https://github.com/prinomen/viewPresentSceneTest
I have a GameViewController and a GameScene class. I try to set the scene with SKScene(fileNamed: "GameScene") but that call is not working because the scene does not appear and the print statement after that line is not being called.
I know I could use a different way to set the scene, like this:
let scene = GameScene()
But I'm trying to understand SpriteKit and it bothers me that the code below does not work. In another project I was able to successfully set the scene using SKScene(fileNamed: "GameScene") like in the code below.
Does anyone know why it is not working in this project?
GameViewController.swift
import SpriteKit
class GameViewController: UIViewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
print("SKScene was set")
// Set the scale mode fit the window:
scene.scaleMode = .aspectFill
// Size our scene to fit the view exactly:
scene.size = view.bounds.size
// Show the new scene:
view.presentScene(scene)
}
}
}
}
GameScene.swift
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
let logoText = SKLabelNode(fontNamed: "AvenirNext-Heavy")
logoText.text = "Game Scene"
logoText.position = CGPoint(x: 0, y: 100)
logoText.fontSize = 60
self.addChild(logoText)
}
}
I believe you need an .sks file to load scenes like that. You probably deleted it from this project, but still kept it around in the other one.
Here's what the documentation says:
The name of the file, without a file extension. The file must be in
the app’s main bundle and have a .sks filename extension.

How to reload GameScene in SpriteKit

I have a simple game and when the time is up the game is over. I have a restart button which currently just reloads the GameScene but for some reason my Nodes appear halfway down and halfway to the left of the screen. I can still use the full space of the screen to play the game though.
The images below show the game while working, end game state and then after reloading the GameScene.
I'm not sure how to fix this or what code to provide.
This is my function for reloading the GameScene:
func goToGameScene(){
let gameScene:GameScene = GameScene(size: self.view!.bounds.size)
let transition = SKTransition.fade(withDuration: 1.0)
gameScene.scaleMode = SKSceneScaleMode.fill
self.view!.presentScene(gameScene, transition: transition)
}
Replace:
gameScene.scaleMode = SKSceneScaleMode.fill
With:
gameScene.scaleMode = .aspectFit
Or:
gameScene.scaleMode = .aspectFill
You can also just completely copy the GameViewController code so you get something like this:
if let view = self.view {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFit
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
Just put that code where you want to reload the scene.

Gaming SKScene transitions Swift

I'm new to Swift and have no Objective-c experience. I already have a working Swift game that I programed using SpriteKit and a not functional Menu also in SpriteKit. Im trying to figure out how to integrate this two but I can't find information on how to handle SKScene (I asume).
Currently if I Run the file it goes straight to the game and I want to first launch the menu and when a button is pressed to jump to the game. How or where can I learn to do this?
My two file clases are as follows:
class GameScene: SKScene, SKPhysicsContactDelegate {
// handles the game
}
class Menu: SKScene {
//handles the menu
}
thank you
You should have ViewController.swift after you make standart SpriteKit application. Your game scene is created and displayed in this file. To first show the menu, it is necessary to replace the game scene creating to menu scene creating. And your game scene must be created on menu scene object. It may look something like this:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = Menu(size: CGSize(width: 1024, height: 768))
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
....
}
class Menu: SKScene {
...
func startPlayButtonPressEvent() {
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
let scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
...
}

Resources