同时移动多个节点非常简单。关键是要独立跟踪每个触摸事件。一种方法是维护一个字典,该字典使用touch事件作为键,并使用要移动的节点作为值。
首先,声明字典
var selectedNodes:[UITouch:SKSpriteNode] = [:]
以touch事件为键,将每个触摸的精灵添加到字典中
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 } } }}根据需要更新子画面的位置
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 } }}触摸结束后,从字典中删除精灵
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { if selectedNodes[touch] != nil { selectedNodes[touch] = nil } }}


