(SpriteKit + Swift 2) - 当用户点击它时移除精灵

(SpriteKit + Swift 2) - Remove sprite when user tapped on it

我正在尝试制作一款简单的点击泡泡游戏。我在我的代码中创建了一个 SKSpriteKid 节点。问题是我想让精灵在用户点击它时消失。我似乎无法删除 parent() 节点。我该如何解决这个问题?

我创建了一个名为 bubble1 的精灵。

func bubblePressed(bubble1: SKSpriteNode) {
      print("Tapped")
      bubble1.removeFromParent()
}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches {

        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)

           if (touchedNode.name == "bubble1") {
              touchedNode.removeFromParent()
              print("hit")                
           }        
    }           
}



//the node's creation.
func bubblesinView() {

    //create the sprite
    let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")

    //spawn in the X axis min is size, max is the total height minus size bubble
    let width_bubble_1 = random(min: bubble1.size.width/2, max: size.width - bubble1.size.height/2)

    //y: size.height * X, X is 0 at bottom page to max
    //spawn position, let x be random
    bubble1.position = CGPoint(x: width_bubble_1, y: size.height)

    // Add the monster to the scene
    addChild(bubble1)

    // Determine speed of the monster from start to end
    let bubblespeed = random(min: CGFloat(1.1), max: CGFloat(4.0))

    //this tell the bubble to move to the given position, changeble x must be a random value
    let moveAction = SKAction.moveTo(CGPoint(x: width_bubble_1, y: size.height * 0.1), duration: NSTimeInterval(bubblespeed))
    let actionMoveDone = SKAction.removeFromParent()
    bubble1.runAction(SKAction.sequence([moveAction, actionMoveDone]))
}

我真的很想把它做好。提前致谢!

您刚刚忘记命名节点:

 //create the sprite
 let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")
 bubble1.name = "bubble1"

如果要使用节点的名称 属性,您应该将其设置为某个名称。运行时无法找出变量名作为名称。所以在

之后的 bubblesInView 函数中
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")

你应该做的

bubble1.name = "bubble1"