我对Swift和iOS开发都非常陌生。
我一直在开发一个基于苹果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
}
}
}
}
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
}
}
}
}
我还在
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”的内容。
我相信这是非常简单的问题。我需要在storyboard编辑器中更改outlet或其他内容吗?如果我尝试在新项目中更改“GameScene”的名称,就会出现此问题,所以我知道这不是特定于我的项目的问题。