更改spritekit中的初始场景

时间:2016-04-26 21:47:57

标签: ios iphone swift sprite-kit

目的:

将Instructions1.swift文件中的场景加载为游戏的第一个场景。

我做了什么:

所以我在项目中创建了2个新文件:Instructions1.sks和Instructions1.swift

然后在GameViewController.swift中,我更改了viewDidLoad,如下所示:

 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)
        }
    }

然而,当我加载游戏时,它直接进入GameScene.swift。我还写了' Instructions1'在Custom Class for Instructions1.sks下。不幸的是,仍然没有运气!

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

您应该更改正在加载的场景的名称。当从文件加载SKScene时,使用 init(fileNamed:)初始化程序也是我的最佳选择。

希望这有帮助!

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)
    }
}