问题
I've been developing a game that has been done in the single default GameScene created by XCode. Now I want to add more screens including a title screen that displays before the GameScene does and a 'Game Over' scene. The game over scene is transitioned from the GameScene and back and works fine but I cant get the title screen to display despite altering the code in GameViewController.swift:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Original default code:
//if let scene = GameScene(fileNamed:"GameScene") {
//modified code
if let scene = Titles(fileNamed:"Titles") {
//<...more code...>
}
The app still runs but just displays a blank screen. I note that the GameScene file also has a GameScene.sks which I think is a graphical representation of the scene content. Do I need to create another .sks or link this to the new default scene even though the scene content is being added programmatically.
I could simply move all the code from GameScene to a new SKScene and use GameScene as the titles but I was curious about the problem and how to resolve it. Any ideas?
Many Thanks, Kw
回答1:
Yes - initialising a scene with 'fileNamed
' refers to a .sks file, so you should create one.
In this line of code:
let scene = Titles(fileNamed:"Titles")
you are create a new scene object called scene
of type Titles
. This should match the class name in your swift file i.e. in titles.swift
you should have:
class Titles: SKScene {
The fileNamed
: is just one of many initialisers for SKSCene
and this specifies a .sks file to load. If you want to place all content in your scene programmatically, perhaps use:
let scene = Titles(size: CGSize(width: 1536, height: 2048))
This question and it's accepted answer might help with any potential problems you might encounter - How to add an .sks files to existing Swift/Sprite-Kit project?
来源:https://stackoverflow.com/questions/42111740/how-to-change-the-default-skscene-that-displays-on-startup