0
votes

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
"I am having compiler error ... when I run the code." – That makes no sense. You can have a compiler error (in which case the program is not compiled and you cannot run it) or a runtime error (when the program is compiled but some error occurs when you run it).Martin R

1 Answers

2
votes

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)




    }