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

Posted

技术标签:

【中文标题】如何重命名 iOS SpriteKit 模板中的默认场景?【英文标题】:How do you rename the default scene in the iOS SpriteKit template? 【发布时间】:2016-07-21 15:38:51 【问题描述】:

我对 Swift 和 ios 开发都很陌生。

我一直在开发基于 Apple 的 SpriteKit(与 GameplayKit 集成)模板的游戏。

在我将 GameScene.swift 和 GameScene.sks 重命名为 MapMainScene.swift 和 MapMainScene.sks 之前,一切都运行良好。我确保在 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
            
        
    

到:

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
            
        
    

我还在Gamescene.swift中更改了以下内容:

class GameScene: SKScene 
    // Some code

MainMapScene.swift

class MainMapScene: SKScene 
    // Some code

但应用程序在启动时崩溃。控制台的输出显示:

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:

所以看起来我的项目中的某处仍有对GameScene 的引用,但我已经进行了项目范围的搜索,但在任何地方都找不到GameScene

我确信这是非常简单的事情。我需要在情节提要编辑器中更改插座或其他内容吗?我也可以通过尝试在新项目中更改 GameScene 的名称来重现此问题,所以我知道这不是我项目特有的。

【问题讨论】:

【参考方案1】:

GameScene 实际上在另一个地方被引用,来自NSKeyedArchiver 的错误应该提示您在哪里:它在您正在加载的.sks 文件中编码。您可以在 Xcode 编辑器的 .sks 文件的“自定义类”选项卡中找到它。

这个设置告诉NSKeyedUnarchiver 当你加载.sks 文件时,编码场景应该是什么类。这应该是对GameScene 的最后一个引用,所以这应该会完成您的重命名!

【讨论】:

那是隐藏的地方。谢谢! 你拯救了这一天!谢谢!

以上是关于如何重命名 iOS SpriteKit 模板中的默认场景?的主要内容,如果未能解决你的问题,请参考以下文章

iOS/iPadOS 中的 SwiftUI + DocumentGroup:如何重命名当前打开的文档

如何重命名 CSS 规则?

如何为 .NET Core 重命名 ABP 中的列?

进阶学习9:ECMAScript——概述ES2015 / ES6新特性详解

在“applicationDidFinishLaunching”方法退出之前,SpriteKit 窗口不会重绘

iOS SpriteKit 如何使用应用程序包中的文件夹中的图像创建节点?