简体   繁体   English

如何重命名iOS SpriteKit模板中的默认场景?

[英]How do you rename the default scene in the iOS SpriteKit template?

I'm very new to both Swift and iOS development. 我对Swift和iOS开发都很​​陌生。

I have been working on a game based off Apple's SpriteKit (with GameplayKit integration) template. 我一直在研究基于Apple的SpriteKit(带有GameplayKit集成)模板的游戏。

All was working fine until I renamed GameScene.swift and GameScene.sks to MapMainScene.swift and MapMainScene.sks. 一切正常,直到我将GameScene.swift和GameScene.sks重命名为MapMainScene.swift和MapMainScene.sks。 I made sure to change this code in GameViewController.swift: 我确保在GameViewController.swift中更改此代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
    // including entities and graphs.
    if let scene = GKScene(fileNamed: "GameScene") {

        // Get the SKScene from the loaded GKScene
        if let sceneNode = scene.rootNode as! GameScene? {

            // Copy gameplay related content over to the scene
            sceneNode.entities = scene.entities
            sceneNode.graphs = scene.graphs

            // Set the scale mode to scale to fit the window
            sceneNode.scaleMode = .aspectFill

            // Present the scene
            if let view = self.view as! SKView? {
                view.presentScene(sceneNode)

                view.ignoresSiblingOrder = true

                view.showsFPS = true
                view.showsNodeCount = true
            }
        }
    }
}

to: 至:

override func viewDidLoad() {
    super.viewDidLoad()

    // Load 'MainMapScene.sks' as a GKScene. This provides gameplay related content
    // including entities and graphs.
    if let scene = GKScene(fileNamed: "MainMapScene") {

        // Get the SKScene from the loaded GKScene
        if let sceneNode = scene.rootNode as! MainMapScene? {

            // Copy gameplay related content over to the scene
            sceneNode.entities = scene.entities
            sceneNode.graphs = scene.graphs

            // Set the scale mode to scale to fit the window
            sceneNode.scaleMode = .aspectFill

            // Present the scene
            if let view = self.view as! SKView? {
                view.presentScene(sceneNode)

                view.ignoresSiblingOrder = true

                view.showsFPS = true
                view.showsNodeCount = true
            }
        }
    }
}

I also changed the following in Gamescene.swift : 我还在Gamescene.swift更改了以下Gamescene.swift

class GameScene: SKScene {
    // Some code
}

to this in MainMapScene.swift : MainMapScene.swift

class MainMapScene: SKScene {
    // Some code
}

but the app crashes upon launch. 但应用程序在发布时崩溃了。 The output of the console says: 控制台的输出说:

2016-07-21 16:29:35.593592 MyGame[10656:1944648] [User Defaults] CFPrefsPlistSource<0x6080000e5e00> (Domain: kCFPreferencesAnyApplication, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null)) is waiting for writes to complete so it can determine if new data is available
2016-07-21 16:29:35.603729 MyGame[10656:1944648] [FenceWorkspace] creating a CA fence port (5d13)
2016-07-21 16:29:35.638 MyGame[10656:1944648] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (GameScene) for key (root); the class may be defined in source code or a library that is not linked'
*** First throw call stack:

So it looks like there is still a reference to GameScene somewhere in my project but I have done a project-wide search and there is no GameScene anywhere to be found. 所以看起来在我的项目中仍然存在对GameScene的引用,但我已经完成了项目范围内的搜索,并且找不到任何GameScene

I'm sure this is something really straightforwards. 我确信这是非常简单的事情。 Do I need to change an outlet or something in the storyboard editor?? 我是否需要更改故事板编辑器中的插座或内容? I can reproduce this problem by trying to change the name of GameScene in a new project too so I know it's not something specific to my project. 我可以通过尝试在新项目中更改GameScene的名称来重现此问题,因此我知道它不是我的项目特有的东西。

GameScene is actually referenced in one more place, and the error coming from NSKeyedArchiver should clue you in as to where: it's encoded in the .sks file you're loading. GameScene实际上是在另一个地方被引用,来自NSKeyedArchiver的错误应该让你知道在哪里:它在你正在加载的.sks文件中编码。 You can find it in the "Custom Class" tab of the .sks file in Xcode's editor. 您可以在Xcode编辑器中的.sks文件的“自定义类”选项卡中找到它。

自定义类选项卡

This setting tells NSKeyedUnarchiver what class the encoded scene should be when you load the .sks file. 此设置告诉NSKeyedUnarchiver加载.sks文件时编码的场景应该是什么类。 This should be the last remaining reference to GameScene , so this should complete your renaming! 这应该是GameScene的最后一个参考,所以这应该完成你的重命名!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM