如何创建SCNNode的迅速数组 [英] How to create an array of SCNNode in swift

查看:173
本文介绍了如何创建SCNNode的迅速数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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] = SCNNode(几何:sphereGeometry)当我运行code。请帮助。

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

推荐答案

找到解决办法:

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)




    }

这篇关于如何创建SCNNode的迅速数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆