Changing the initial scene in spritekit

蓝咒 提交于 2019-12-13 19:39:54

问题


Aim:

To load the scene in Instructions1.swift file as the first scene of the game.

What I did :

So I created 2 new files in my project : Instructions1.sks and Instructions1.swift

Then in GameViewController.swift , I changed the viewDidLoad as follows :

 override func viewDidLoad()
    {
        super.viewDidLoad()

        if let scene = GameScene.unarchiveFromFile("Instructions1") as? GameScene
        {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill

            skView.presentScene(scene)
        }
    }

However when I load the game, it goes straight to GameScene.swift. I also wrote 'Instructions1' under the Custom Class for Instructions1.sks . Still no luck unfortunately!

Would appreciate any help!

Thanks!


回答1:


You should change the name of the scene that you're loading up. Also using the init(fileNamed:) initializer is a go-to for me when loading a SKScene from a file.

Hopefully this helps!

override func viewDidLoad()
{
    super.viewDidLoad()

    if let scene = Instructions1(fileNamed:"Instructions1")
    {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}


来源:https://stackoverflow.com/questions/36876376/changing-the-initial-scene-in-spritekit

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