SCNScene - 调用SCNScene(命名:)的致命错误:线程1:EXC_BAD_INSTRUCTION

时间:2015-11-23 05:05:05

标签: ios swift scenekit

我正在使用共享项目,并花了很多时间将其更新到Swift 2,所以我想让它工作,但不幸的是我遇到了运行时错误。故事板上没有任何内容,所以一切都是以编程方式完成的。看到它以编程方式完成,我不知道为什么我会收到此运行时错误。

请告诉我这个代码中的“glasses1.dae”和“glasses2”是什么。这些标识符是什么?

  

Swift的函数签名特化。(_ fatalErrorMessage(Swift.StaticString,Swift.StaticString,Swift.StaticString,Swift.UInt) - >())。(闭包#2)

     

致命错误:在展开Optional值时意外发现nil   (lldb)

     

主题1:EXC_BAD_INSTRUCTION ...

class ViewController: UIViewController {
private let notificationCenter : NSNotificationCenter = NSNotificationCenter.defaultCenter()

private let screenWidth : CGFloat = 320
private let scaleX : CGFloat = (320 / 750)
private let scaleY : CGFloat = (568 / 1334)

private let eyeRectL : UILabel = UILabel()
private let eyeRectR : UILabel = UILabel()

private var scnView : SCNView!
private var glasses : SCNNode!

override func viewDidLoad() {
    super.viewDidLoad()

    // Scene
    let scene = SCNScene(named: "glasses1.dae")! // fatal error: unexpectedly found nil while unwrapping an Optional value 

    // Camera
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)
    scene.rootNode.addChildNode(cameraNode)

    // Light
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)

    // Ambient Light
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    scene.rootNode.addChildNode(ambientLightNode)

    glasses = scene.rootNode.childNodeWithName("glasses2", recursively: true)! // what is "glasses2"?

    scnView =  SCNView()
    scnView.scene = scene
    scnView.backgroundColor = UIColor.clearColor()
    scnView.frame = self.view.bounds

    let moohaha = setupMoohaha()
    let cameraView = visage.moohahaCameraView

    self.view.addSubview(cameraView)
    self.view.addSubview(scnView)

    self.view.addSubview(eyeRectL)
    self.view.addSubview(eyeRectR)

    moohaha.beginFaceDetection()
}

1 个答案:

答案 0 :(得分:3)

.dae文件包含场景信息,用于加载外部创建的场景(不在代码中)。 “glasses1.dae”字符串是应该是项目一部分的文件的名称。据推测,childWithNodeName(_:recursively:)方法调用用于查找名为“glasses2”的子节点,该子节点是作为用于创建场景的.dea文件中定义的场景的一部分创建的。

您指示的两行末尾的!运算符用于强制解包可选值,如果值包含nil,它将使程序崩溃。因此,我假设您的场景未被创建,因为“glasses.dea”文件实际上不是您项目的一部分,并且SceneKit未能加载它并返回nil场景。

相关问题