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

标签 ios swift2 skspritenode

我正在尝试制作一个简单的泡泡点击游戏。我在代码中创建了一个 SKSpriteKid 节点。问题是我希望当用户点击 Sprite 时 Sprite 会消失。我似乎无法删除节点。我该如何解决这个问题?

我创建了一个名为 bubble1 的 Sprite 。

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]))
}

我真的很努力让这项工作成功。提前致谢!

最佳答案

如果你想使用节点的 name 属性,你应该将其设置为某个值。运行时无法将变量名称识别为名称。所以在bubblesInView函数之后

let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")

你应该这样做

bubble1.name = "bubble1"

关于ios - (SpriteKit + Swift 2) - 当用户点击它时移除 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33762221/

相关文章:

ios - 在 iOS9 和 Swift2 中使用 Parse 登录 Facebook

swift - SKSpriteNode 卡在 init 中(edgeLoopF​​rom :) - SKPhysicsBody

ios - 取消存档 SKSpriteNode 会破坏 iOS 7.1 中 Sprite 的 SKTexture,否则没问题

ios - arc4random:限制生成的随机数的值

iphone - 自定义 UITableViewCell 宽度在 tableview reloadData 上调整大小

ios - 从 UInt32 转换为 Int 时溢出

xcode - - 创建自定义转场时不显示执行

ios - SKSpriteNode 减慢但不应该

ios - UINavigationBar 中的点击事件被手势识别器覆盖

ios - 自定义分段控制