How to create an array of SCNNode in swift

ぐ巨炮叔叔 提交于 2019-12-25 06:59:19

问题


I want to create a fund to create an array of nodes. My declaration is as follows:

func createSphereNode () {




        var spheres = [SCNNode()]



        let sphereGeometry = SCNSphere(radius:   2.5)
        let sphereMaterial = SCNMaterial()
        sphereMaterial.diffuse.contents = UIColor.greenColor()
        sphereGeometry.materials = [sphereMaterial]

        for var i=0;i<15;i++ {

         let   gravityFields  = SCNPhysicsField.radialGravityField()
            gravityFields .strength = 0
            spheres[i]  = SCNNode(geometry: sphereGeometry)
            spheres[i].position =  SCNVector3(x: Float(-15 + (6 * i)), y: 1.5, z: 0)
            spheres[i].physicsField = gravityFields
            let shape = SCNPhysicsShape(geometry: sphereGeometry, options: nil)
           let   sphereBodys  = SCNPhysicsBody(type: .Kinematic, shape: shape)
            spheres[i].physicsBody = sphereBodys

            sceneView.scene?.rootNode.addChildNode(spheres[i])




        }

I am having compiler error on line "spheres[i] = SCNNode(geometry: sphereGeometry)" when I run the code. Please help.


回答1:


found solution:

func createSphereNode () {
  //  var gravityFields = []
  //  var shapes = [SCNPhysicsShape()]
  //  var sphereBodys = [SCNPhysicsBody]()
 //   var spheres = [SCNNode()]

    var spheresArray : [SCNNode] = []



    let sphereGeometry = SCNSphere(radius:   1.5)
    let sphereMaterial = SCNMaterial()
    sphereMaterial.diffuse.contents = UIColor.greenColor()
    sphereGeometry.materials = [sphereMaterial]

    for var i=0;i<15;i++ {

     let   gravityFields  = SCNPhysicsField.radialGravityField()
        gravityFields .strength = 0
       let spheres  = SCNNode(geometry: sphereGeometry)
        spheres.position =  SCNVector3(x: Float(-15 + (6 * i)), y: 1.5, z: 0)
        spheres.physicsField = gravityFields
        let shape = SCNPhysicsShape(geometry: sphereGeometry, options: nil)
       let   sphereBodys  = SCNPhysicsBody(type: .Kinematic, shape: shape)
        spheres.physicsBody = sphereBodys

        sceneView.scene?.rootNode.addChildNode(spheres)

        spheresArray.append(spheres)




    }


来源:https://stackoverflow.com/questions/31495667/how-to-create-an-array-of-scnnode-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!