SpriteKiit Swift: touch a moving object

后端 未结 1 1306
时光说笑
时光说笑 2021-01-28 16:51

For example, I have a Color Sprite object wich can move and bounce at the wall. How can i make it disappear when I catch it and touch it on the screen?

相关标签:
1条回答
  • 2021-01-28 17:38

    you have to set a name for your sprite like "ballNode", then in "touchesBegan" function you can handle it.

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in (touches) {
            let positionInScene = touch.location(in: self)
            let touchedNode = self.atPoint(positionInScene)
            if let name = touchedNode.name {
                if name == "ballNode" {
                    //make it hidden by touchedNode.isHidden = true
                    //or remove it from parent by touchedNode.removeFromParent()
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题