2014-11-11 2 views
0

SCNNode를 설정하는 데 사용 된 구형 지오메트리 (SCNSphere)의 반지름을 어떻게 반환합니까? 부모 노드와 관련하여 일부 자식 노드를 이동하는 방법에서 반지름을 사용하고 싶습니다. 반경이 결과 노드에서 알 수없는 것처럼 보이기 때문에 아래 코드는 실패합니다. 노드를 메서드에 전달하지 않으면 어떻게됩니까?SCNNode를 생성하는 데 사용되는 SCNSphere의 반경을 얻습니다.

또한 내 배열 인덱스는 Int가 범위가 아님을 나타내지 못합니다.

내가 radiusthis

import UIKit 
import SceneKit 

class PrimitivesScene: SCNScene { 

    override init() { 
     super.init() 
     self.addSpheres(); 
    } 

    func addSpheres() { 
     let sphereGeometry = SCNSphere(radius: 1.0) 
     sphereGeometry.firstMaterial?.diffuse.contents = UIColor.redColor() 
     let sphereNode = SCNNode(geometry: sphereGeometry) 
     self.rootNode.addChildNode(sphereNode) 

     let secondSphereGeometry = SCNSphere(radius: 0.5) 
     secondSphereGeometry.firstMaterial?.diffuse.contents = UIColor.greenColor() 
     let secondSphereNode = SCNNode(geometry: secondSphereGeometry) 
     secondSphereNode.position = SCNVector3(x: 0, y: 1.25, z: 0.0) 

     self.rootNode.addChildNode(secondSphereNode) 
     self.attachChildrenWithAngle(sphereNode, children:[secondSphereNode, sphereNode], angle:20) 
    } 

    func attachChildrenWithAngle(parent: SCNNode, children:[SCNNode], angle:Int) { 
     let parentRadius = parent.geometry.radius //This fails cause geometry does not know radius. 

     for var index = 0; index < 3; ++index{ 
      children[index].position=SCNVector3(x:Float(index),y:parentRadius+children[index].radius/2, z:0);// fails saying int is not convertible to range. 
     } 


    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 

답변

4

문제에서 뭔가를 구축을 위해 노력하고 parent.geometrySCNGeometry 아닌 SCNSphere를 반환한다는 것입니다. radius을 얻으려면 parent.geometrySCNSphere 번으로 먼저 입력해야합니다. 안전을 위해, 그것을 사용하는 것이 아마도 가장 좋은 몇 가지 옵션 바인딩 것을 할 체인 :

if let parentRadius = (parent.geometry as? SCNSphere)?.radius { 
    // use parentRadius here 
} 

당신은 또한 수행해야합니다 당신의 children 노드에 radius에 액세스 할 때. 당신이 조금이 모두 함께 넣어 청소 일까지, 당신은 다음과 같이 뭔가를 얻을 :

func attachChildrenWithAngle(parent: SCNNode, children:[SCNNode], angle:Int) { 
    if let parentRadius = (parent.geometry as? SCNSphere)?.radius { 
     for var index = 0; index < 3; ++index{ 
      let child = children[index] 
      if let childRadius = (child.geometry as? SCNSphere)?.radius { 
       let radius = parentRadius + childRadius/2.0 
       child.position = SCNVector3(x:CGFloat(index), y:radius, z:0.0); 
      } 
     } 
    } 
} 

주 당신이이 아이들의 배열 attachChildrenWithAngle를 호출하고 그 불구하고 :

self.attachChildrenWithAngle(sphereNode, children:[secondSphereNode, sphereNode], angle:20) 

당신이 만약 그렇게하면 세 번째 요소에 액세스 할 때 루프가 for 번 반복되는 동안 런타임 크래시가 발생합니다. 이 함수를 호출 할 때마다 3 명의 자식이있는 배열을 전달하거나 루프의 for 루프를 변경해야합니다.

+0

예! 기존 노드의 반경을 변경하는데도 사용할 수 있습니다. 감사. – bpedit

관련 문제