2014-12-07 12 views
5

Sto lavorando in Swift con Sprite-Kit utilizzando XCode 6 e ho molti nodi diversi ma per ora riesco solo a rilevare un dito e spostare un nodo contemporaneamente. Voglio sapere come posso riuscire a rilevare più dita allo scopo di spostare più nodi contemporaneamente. Il mio codice attuale è:Gesto multi-touch in Sprite Kit

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 
} 

Come potete vedere ho solo il position del mio primo dito, ma come posso rilevare la posizione più dita e assegnare ogni dito al nodo toccato dal dito?

risposta

9

Lo spostamento di più nodi contemporaneamente è abbastanza semplice. La chiave è tracciare indipendentemente ogni evento di tocco. Un modo per farlo è mantenere un dizionario che usa l'evento touch come chiave e il nodo che viene spostato come valore.

In primo luogo, dichiarare il dizionario

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

Aggiungere ogni sprite toccato al dizionario con l'evento di tocco come la chiave

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

Aggiornare le posizioni dei sprite come necessario

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

Rimuovere lo sprite dal dizionario quando il tocco termina

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

Grazie. Funziona bene – Epsilon

Problemi correlati