How to change the default SKScene that displays on startup

房东的猫 提交于 2020-01-17 05:06:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!