arrays - 如何在 swift 中创建一个 SCNNode 数组

标签 arrays swift scenekit scnnode

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

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)”上遇到编译器错误。请帮忙。

最佳答案

找到的解决方案:

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)




    }

关于arrays - 如何在 swift 中创建一个 SCNNode 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31495667/

相关文章:

java - 可比较<类型>数组 VS 可比较数组

ios - Swift 3 数组扩展错误

ios - 带有 GLSL 数组的 SceneKit 着色器修改器

ios - 场景Kit/ARKit : map screen shape into 3D object

c - 换行符替换 char 数组中的第一个值

javascript - Highcharts 值(value)百分比

c - 快速计算数组中较小/相等/较大元素的方法

swift - UICollectionView 的标题 View 在滚动时不断重复 - 如何创建固定(非粘性)的单个 Collection View 标题?

ios - 使用 AVAudioRecorder iOS 录制 Opus

swift - 在 Swift 中扩展 SCNNode