0

我想创建一个基金来创建一个节点数组。我的声明如下:

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])




        }

运行代码时,“spheres[i] = SCNNode(geometry: sphereGeometry)”行出现编译器错误。请帮忙。

4

1 回答 1

2

找到解决方案:

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)




    }
于 2015-07-18T21:38:12.230 回答