ios - Swift:不能继承 SCNNode 吗? fatal error

标签 ios swift scenekit arkit scnnode

好的,我需要对 SCNNode 进行子类化,因为我的游戏中有具有不同“能力”的不同 SCNNode(我知道人们通常不会对 SCNNode 进行子类化,但我需要这样做)

我已经关注了所有其他问题,例如 Subclassing SCNNodeCreating a subclass of a SCNNode 但继续出现此错误:

fatal error: use of unimplemented initializer 'init()' for class 'LittleDude.Dude'

Dude 是我的 SCNNode 子类的名称。

在第二个问题之后,由于分类问题,这就是我尝试从我的 .dae 场景中获取 SCNNode 并将其分配给我的 Dude() 的方式:

var theDude = Dude(geometry: SCNSphere(radius: 0.1)) //random geometry first
var modelScene = SCNScene(named: "art.scnassets/ryderFinal3.dae")!
if let d = modelScene.rootNode.childNodes.first
{
    theDude.transform = d.transform
    theDude.geometry = d.geometry
    theDude.rotation = d.rotation
    theDude.position = d.position
    theDude.boundingBox = d.boundingBox
    theDude.geometry?.firstMaterial = d.geometry?.firstMaterial
}
print("DUDE: ", theDude)

然后在我的 Dude 课上:

class Dude: SCNNode {

    init(geometry: SCNGeometry) {
        super.init()

        center(node: self)
        self.scale = SCNVector3(x: modifier, y: modifier, z: modifier)
        //theDude.transform = SCNMatrix4Mult(theDude.transform, SCNMatrix4MakeRotation(360, 0, 1, 0))
        //theDude.worldOrientation = .
        //self.theDude.position = SCNVector3Make(0, 0, -1)

        for s in animScenes {
            if let anim = animationFromSceneNamed(path: s)
            {
                animations.append(anim)
                anim.usesSceneTimeBase = true
                anim.repeatCount = Float.infinity
                self.addAnimation(anim, forKey: anim.description)
            }
        }
    }
}

/* Xcode required this */
required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented222")
}

此自定义类的第一行出现错误,并在我尝试克隆自定义 SCNNode 并将其添加到我的场景时发生:

func makeDude(hitPosition: SCNVector3) {
    //print("DUDE")
    let clone = theDude.clone() as? SCNNode
    clone?.position = hitPosition
    self.sceneView.scene.rootNode.addChildNode(clone!)
}

即使我转换到 SCNNode 来尝试避免错误。我怎样才能在我的场景中克隆和使用我的自定义 SCNNode?这里有什么问题?

最佳答案

为了清楚起见,这个答案隐藏在之前答案的评论中,所以为了避免混淆,这里给出完整的答案:


class NodeSubClass: SCNNode {

init(geometry: SCNGeometry?){
        super.init()
        self.geometry = geometry
    }

...

}

关于ios - Swift:不能继承 SCNNode 吗? fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46165577/

相关文章:

swift - UIButton 标题未显示在 UIStackView 内

ios - 尝试在使用 SpeechKit 进行语音识别之前播放系统声音

swift - 将 json 编码时间转换为 nsdate

ios - 相机位置的SCNNode

javascript - 如何禁用 iOS HTML5 视频控件?

ios - 没有这样的模块 'Stripe'

ios - 如何同时限制 Swift 中 2 个 TextView 的字符数?

ios - Swift SceneKit 是围绕由两个 3D 点定义的轴的胶囊内的点

ios - 在 SceneKit 上画一条线在设备上不起作用

ios - 我需要澄清 swift 3 兼容性和 API 可用性之间的区别