42n*_*2nd 5 scenekit swift swift-playground
我正在尝试使用 SceneKit 为 WWDC 学生挑战赛做一些事情,这要求我的项目在 Swift 游乐场中。我不断收到“运行此页面时出现问题”的消息。它不提供错误消息。它只是建议检查我的代码或重新开始。我曾尝试单独删除代码段,但找不到此问题的根源。我还尝试在 man Xcode playground 中运行它,它提供了警告,我引用“操场无法继续运行,因为操场源代码做到了”。我被困住了。我的代码有什么问题。
import UIKit
import SceneKit
import QuartzCore
import PlaygroundSupport
class GameScene: UIViewController, SCNSceneRendererDelegate {
var primaryView: SCNView!
var primaryScene: SCNScene!
var cameraNode: SCNNode!
override func viewDidLoad() {
sceneAndViewInit()
cameraInit()
createGround()
moonInit()
}
func createGround() {
var ground = SCNBox(width: 200, height: 1, length: 200, chamferRadius: 0)
var groundPhysicsShape = SCNPhysicsShape(geometry: ground, options: nil)
var groundNode = SCNNode(geometry: ground)
ground.firstMaterial?.diffuse.contents = UIColor.orange
ground.firstMaterial?.specular.contents = UIColor.white
groundNode.position = SCNVector3(0, -6, 0)
groundNode.physicsBody = SCNPhysicsBody(type: .static, shape: groundPhysicsShape)
primaryScene.rootNode.addChildNode(groundNode)
}
func sceneAndViewInit() {
primaryView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 300))
primaryView.allowsCameraControl = true
primaryView.autoenablesDefaultLighting = true
primaryScene = SCNScene()
primaryView.scene = primaryScene
primaryView.isPlaying = true
}
func cameraInit() {
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
cameraNode.camera?.fieldOfView = 65
cameraNode.camera?.wantsHDR = false
primaryScene.rootNode.addChildNode(cameraNode)
}
func moonInit() {
let moonScene = SCNScene(named: "Moon.scn")
var moonNode = moonScene?.rootNode.childNode(withName: "Sphere", recursively: true)
var moonPhysicsShape = SCNPhysicsShape(node: moonNode!, options: nil)
moonNode?.position = SCNVector3(0, 0, 0)
moonNode?.scale = SCNVector3(1, 1, 1)
moonNode?.name = "Moon"
moonNode?.physicsBody = SCNPhysicsBody(type: .static, shape: moonPhysicsShape)
primaryScene.rootNode.addChildNode(moonNode!)
}
func renderer(_ aRenderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
print(time)
}
}
let gameScene = GameScene()
PlaygroundPage.current.liveView = gameScene
Run Code Online (Sandbox Code Playgroud)
(“Moon.scn”是我在资源/文件部分中的一个文件及其纹理,如果没有任何与月球相关的代码,我仍然有问题。)
我找到了解决这个问题的方法。但是,我只能向您提供有关问题所在以及如何解决问题的大致想法。
问题似乎出在类顶部的变量声明上。
var primaryView: SCNView!
var primaryScene: SCNScene!
var cameraNode: SCNNode!
Run Code Online (Sandbox Code Playgroud)
类型后面的感叹号表示该变量是一个implicitly-unwrapped optional(我引用了这个 Stack Overflow 响应,它更深入地解释了它的含义),但或多或少该变量在使用时会自动强制展开。我对 Swift 的应用知识比技术知识更多,所以我的猜测是,当GameScene创建一个对象时,合成初始化程序尝试设置这些变量,由于某种原因,它需要从变量中读取, gets nil,并且无法继续。
解决方案似乎是将变量的值放入其初始声明中(可能有更好的解决方案,但这至少在我的测试中有效。),这样它们就不适合nil初始化程序。现在我无法在这里完全解释这个问题的原因是,这似乎只是对于SCNScene。我真的不知道为什么会这样。
代码(***标记重要注释):
import UIKit
import SceneKit
import QuartzCore
import PlaygroundSupport
class GameScene: UIViewController, SCNSceneRendererDelegate {
var primaryView: SCNView!// = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 300)) //***This seems to work for reasons beyond me with the implicitly-unwrapped optional***
var primaryScene = SCNScene() //***The implicitly unwrapped optional is removed here***
var cameraNode = SCNNode() //***The implicitly unwrapped optional is removed here (I am not sure if that is necessary but it probably good practice not to use it anywhere intros code. I use it earlier to demonstrate its weird complexities.)***
override func viewDidLoad() {
sceneAndViewInit()
cameraInit()
createGround()
//moonInit() ***I removed this for simplicity in the answer***
}
func createGround() {
var ground = SCNBox(width: 200, height: 1, length: 200, chamferRadius: 0)
var groundPhysicsShape = SCNPhysicsShape(geometry: ground, options: nil)
var groundNode = SCNNode(geometry: ground)
ground.firstMaterial?.diffuse.contents = UIColor.orange
ground.firstMaterial?.specular.contents = UIColor.white
groundNode.position = SCNVector3(0, -6, 0)
groundNode.physicsBody = SCNPhysicsBody(type: .static, shape: groundPhysicsShape)
primaryScene.rootNode.addChildNode(groundNode)
}
func sceneAndViewInit() {
primaryView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 300))
primaryView.allowsCameraControl = true
primaryView.autoenablesDefaultLighting = true
//primaryScene = SCNScene() ***This is not necessary when it is declared above***
self.primaryView.scene = primaryScene
//primaryView.isPlaying = true
self.view = primaryView //***I realized later you need this bit of code or the program will only display a blank screen***
}
func cameraInit() {
//var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
cameraNode.camera?.fieldOfView = 65
cameraNode.camera?.wantsHDR = false
primaryScene.rootNode.addChildNode(cameraNode)
}
func moonInit() {
let moonScene = SCNScene(named: "Moon.scn")
var moonNode = moonScene?.rootNode.childNode(withName: "Sphere", recursively: true)
var moonPhysicsShape = SCNPhysicsShape(node: moonNode!, options: nil)
moonNode?.position = SCNVector3(0, 0, 0)
moonNode?.scale = SCNVector3(1, 1, 1)
moonNode?.name = "Moon"
moonNode?.physicsBody = SCNPhysicsBody(type: .static, shape: moonPhysicsShape)
primaryScene.rootNode.addChildNode(moonNode!)
}
func renderer(_ aRenderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
print(time)
}
}
let gameScene = GameScene()
PlaygroundPage.current.liveView = gameScene
Run Code Online (Sandbox Code Playgroud)
最后一点:self.view = primaryView代码非常重要。我在完整代码中做了注释,但我发现这种方法很难在互联网上找到。UIViewController我认为这与(我根本不了解)有关UIKit,但是如果您现在有那段代码,则要么显示空白屏幕,要么仅显示空白屏幕SceneKit。UIKit如果你不明白这些是单独的问题,那么它们真的很难解读。
| 归档时间: |
|
| 查看次数: |
534 次 |
| 最近记录: |