0

我将星空编码如下。现在我想在用户触摸它时删除它。然而,下面的代码删除了天空中的所有星星。如何访问单个星节点来操作它?

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

}
4

1 回答 1

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

}
于 2014-11-09T12:59:45.207 回答