2014-12-07 38 views
5

Pracuję w Swift z zestawem Sprite za pomocą XCode 6 i mam wiele różnych węzłów, ale na razie tylko uda mi się wykryć tylko jeden palec i przesuwać jeden węzeł w tym samym czasie. Chcę wiedzieć, w jaki sposób udało mi się wykryć wiele palców, aby przesuwać wiele węzłów w tym samym czasie. Moje rzeczywisty kod jest:Gesty wielodotykowe w zestawie Sprite

var location = CGFloat() // finger position 
var actualNode = -1 // node touched by the finger, -1 means no node touched 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) // when a finger touch the screen 
{ 
    for touch: AnyObject in touches 
    { 
     location = touch.locationInNode(self) // we detect the finger position 
    } 

    for var index = 0; index < colorNode.count; index++ 
    { 
     if nodeAtPoint(location) == colorNode[index].node 
     { 
      actualNode = index // the number of the node touched by the finger 
     } 
    } 
} 

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) // when a finger move 
{ 
    for touch: AnyObject in touches 
    { 
     location = touch.locationInNode(self) // we detect the finger position 
    } 

    if actualNode != -1 // if a node is touched 
    { 
     colorNode[actualNode].position = location // we move this node to the finger 
    } 
} 
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) // when a finger don't touch the screen anymore 
{   
    actualNode = -1 // there is no node touched 
} 

Jak widać mam tylko position mojego pierwszego palca, ale w jaki sposób można wykryć wiele pozycji palca i przypisać każdy palec do węzła dotknął palcem?

Odpowiedz

9

Przenoszenie wielu węzłów jednocześnie jest dość proste. Kluczem jest niezależne śledzenie każdego zdarzenia dotyku. Jednym ze sposobów na to jest utrzymanie słownika, który używa zdarzenia dotyku jako klucza, a węzeł jest przenoszony jako wartość.

Po pierwsze, stwierdzenie słownika

var selectedNodes:[UITouch:SKSpriteNode] = [:] 

Dodaj każdy sprite dotykał do słownika z dotykowym razie jako kluczowego

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     let location = touch.location(in:self) 
     if let node = self.atPoint(location) as? SKSpriteNode { 
      // Assumes sprites are named "sprite" 
      if (node.name == "sprite") { 
       selectedNodes[touch] = node 
      } 
     } 
    } 
} 

zaktualizować pozycje duszków ile potrzeba

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     let location = touch.location(in:self) 
     // Update the position of the sprites 
     if let node = selectedNodes[touch] { 
      node.position = location 
     } 
    } 
} 

Usuń ikonkę ze słownika, gdy kończy się dotyk

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     if selectedNodes[touch] != nil { 
      selectedNodes[touch] = nil 
     } 
    } 
} 
+0

Dzięki. To działa dobrze. – Epsilon