2014-11-09 2 views
0

다음과 같이 별이 빛나는 하늘을 코딩했습니다. 이제는 사용자가 터치 할 때 별표를 제거하고 싶습니다. 다음 코드는 하늘의 모든 별을 제거합니다. 단일 별 노드에 액세스하려면 어떻게해야합니까?스프라이트 키트의 SKNode를 조작하는 방법은 무엇입니까?

override func didMoveToView(view: SKView) { 
    for(var i = 0; i < stars ; i++) { 
     planetsLayer.addChild(createPlanet(view)) 
    } 
    self.addChild(planetsLayer)  
} 


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    /* Called when a touch begins */   
    for touch in touches { 
     let location = touch.locationInNode(planetsLayer) 
     let touchedLayer = nodeAtPoint(location) 
     let touchedNode = nodeAtPoint(location) 
     touchedNode.removeFromParent() 
    } 
func createPlanet() -> SKShapeNode { 
    ... 
    var shapeNode = SKShapeNode(); 
    ... 
    return shapeNode 

} 

답변

0

행성 노드를 만들 때 이름을 지정하고 노드를 제거 할 때이 이름을 확인하십시오. 이것은 행성이 아닌 다른 노드를 삭제하지 못하도록합니다.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    /* Called when a touch begins */   
    for touch in touches { 
     let location = touch.locationInNode(planetsLayer) 
     let touchedNode = nodeAtPoint(location) 
     if touchedNode.name == "planet" { 
      touchedNode.removeFromParent() 
     } 
    } 
} 

func createPlanet() -> SKShapeNode { 
    ... 
    var shapeNode = SKShapeNode() 
    shapeNode.name = "planet" 
    ... 
    return shapeNode 

} 
관련 문제