我有一个特定的问题。我曾经使用默认代码从视图控制器呈现我的游戏场景。然而,我添加了另一个SKScene,现在使用此代码展示我的游戏场景:
//In an SKScene (not Game scene)
let scene = GameScene()
let skView = self.view!
skView.ignoresSiblingOrder = true
scene.scaleMode = .aspectFit
let push = SKTransition.push(with: SKTransitionDirection.right, duration: 0.4)
skView.presentScene(scene, transition: push)
我的问题是,当我运行代码行时,我的游戏场景似乎不再识别我的SKS文件:
//in gamescene
sprite = self.childNode(withName: "sprite") as! SKLabelNode
展开时找不到任何东西。 当我使用代码从GameViewController呈现我的SKScene时:
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 = .aspectFit
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
}
}
}
一切正常。我究竟做错了什么?提前谢谢。
答案 0 :(得分:1)
我认为,当您从SKScene
内而不是UIViewController
内呈现时崩溃的原因是由于两个原因。
SKScene
出示时,您创建了GameScene
类型的对象,但当您从UIViewController
出现时,您创建了GKScene
类型的对象。GameScene
类型的对象时,您使用的初始值设定项不会考虑附加的SKS文件。但是当你创建GKScene
类型的对象时,你会这样做。因此,我会将第一个代码块编辑为:
//In an SKScene (not Game scene)
if let sceneContainer = GKScene(fileNamed: "GameScene") {
let skView = self.view!
skView.ignoresSiblingOrder = true
//Something comparable to the following line. I don't have a project set up, but let the compiler run you through the exact syntax.
let scene = sceneContainer.rootNode as! GameScene?
scene?.scaleMode = .aspectFit
let push = SKTransition.push(with: SKTransitionDirection.right, duration: 0.4)
skView.presentScene(scene!, transition: push)
}
可以找到SKScene
GKScene
获取<error-page>
<!-- Mapping a 404 when user refreshes with an html5 push route-->
<error-code>404</error-code>
<location>/index.html</location>
</error-page>
的文档。