2014-12-07 18 views
5

Tôi đang làm việc với Swift với Sprite-Kit sử dụng XCode 6 và tôi có nhiều nút khác nhau nhưng hiện tại tôi chỉ phát hiện một ngón tay và di chuyển một nút cùng một lúc. Tôi muốn biết làm thế nào tôi có thể quản lý để phát hiện nhiều ngón tay để di chuyển nhiều nút cùng một lúc. mã thực tế của tôi là:Cử chỉ cảm ứng đa điểm trong 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 
} 

Như bạn có thể thấy tôi chỉ có position của ngón tay đầu tiên của tôi, nhưng làm thế nào tôi có thể phát hiện nhiều vị trí ngón tay và gán mỗi ngón tay đến nút chạm bằng ngón tay?

Trả lời

9

Di chuyển nhiều nút cùng một lúc là khá đơn giản. Điều quan trọng là để theo dõi từng sự kiện cảm ứng một cách độc lập. Một cách để làm điều đó là duy trì một từ điển sử dụng sự kiện cảm ứng làm khóa và nút được di chuyển làm giá trị.

Thứ nhất, tuyên bố từ điển

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

Thêm mỗi sprite chạm vào từ điển với sự kiện liên lạc là chìa khóa

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

Cập nhật vị trí của các sprites khi cần thiết

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

Xóa sprite khỏi từ điển khi chạm vào kết thúc

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

Cảm ơn. Điều này hoạt động tốt. – Epsilon

Các vấn đề liên quan