2016-12-03 69 views
0

例如,我有一個Color Sprite對象,可以在牆上移動和彈跳。當我抓住它並在屏幕上觸摸它時,如何使它消失?SpriteKiit Swift:觸摸移動物體

+2

教程,這可能有所幫助:https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners – shallowThought

回答

2

你必須爲你的精靈設置一個名字,比如「ballNode」,然後在「touchesBegan」函數中你可以處理它。

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