2015-01-07 101 views
2

我只是想知道如何從場景中移除SKSprite節點。這是我到目前爲止有:Swift + Sprite套件觸摸檢測

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 


    for touch: AnyObject in touches { 
     let location = (touch as UITouch).locationInNode(self) 
     if let theName = self.nodeAtPoint(location).name { 
      if theName == "monster" { 

       monster! .removeFromParent() 



      } 
     } 
    } 
} 

我創造很多在屏幕上這些怪物的,但是當我在其中一人挖掘它不會做任何事情。如果我嘗試添加println("touched"),它告訴我它已被觸動。

+1

您是否給每個怪物命名爲「怪物」?你有沒有在所有的怪物上設置userInteractionEnabled爲true? – Okapi

回答

3

當你這樣做monster.removeFromParent()這不會刪除被觸摸的節點,因爲monster不是對被觸摸的節點的引用。要刪除被觸摸的節點,您可以使用以下代碼:

for touch in touches { 
    let location = (touch as UITouch).locationInNode(self) 
    if let theMonster = self.nodeAtPoint(location) 
     if theMonster.name == "monster" { 
      theMonster.removeFromParent() 
     } 
    } 
} 
+0

感謝此工作 – user3808792

0

你跟蹤你的怪物嗎?如果不是,請通過將這些添加到Mutable Array來跟蹤這些內容。還要爲每個精靈添加唯一的名稱。

然後只是比較對象與你的數組並刪除該對象。希望這有助於.. :)

+0

我該怎麼做?我的物體永遠不斷下降。我將它編程爲每隔0.5秒在隨機位置設置屏幕上方的精靈,然後落下。謝謝 – user3808792