>我有 2 个节点,我想在我的 iPad 上用 2 根手指异步抓取。这是用于移动的代码:
var ninjaMoving = false
var monsterMoving = false
var loc = CGPoint(x: 0.0, y: 0.0)
var prevLoc = CGPoint(x: 0.0, y: 0.0)
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if !self.nodesAtPoint(location).isEmpty && (self.nodeAtPoint(location).physicsBody?.categoryBitMask == 1 || self.nodeAtPoint(location).physicsBody?.categoryBitMask == 3) {
if self.nodeAtPoint(location).physicsBody?.categoryBitMask == 1 {
ninjaMoving = true
}
else if self.nodeAtPoint(location).physicsBody?.categoryBitMask == 3 {
monsterMoving = true
}
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
loc = touch.locationInNode(self)
prevLoc = touch.previousLocationInNode(self)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
let location = touch.locationInNode(self)
if self.nodeAtPoint(prevLoc).physicsBody?.categoryBitMask == 1 {
ninjaMoving = false
}
if self.nodeAtPoint(prevLoc).physicsBody?.categoryBitMask == 3 {
monsterMoving = false
}
}
}
override func update(currentTime: CFTimeInterval) {
if ninjaMoving {
var x = playerNinja.position.x + (loc.x - prevLoc.x)
var y = playerNinja.position.y + (loc.y - prevLoc.y)
x = max(x, playerNinja.size.width / 2)
x = min(x, size.width / 2 - playerNinja.size.width)
y = max(y, playerNinja.size.height / 2)
y = min(y, size.height - playerNinja.size.height / 2)
playerNinja.position = CGPointMake(x, y)
}
if monsterMoving {
var x = playerMonster.position.x + (loc.x - prevLoc.x)
var y = playerMonster.position.y + (loc.y - prevLoc.y)
x = max(x, self.size.width / 2 + playerMonster.size.width)
x = min(x, size.width - playerMonster.size.width / 2)
y = max(y, playerMonster.size.height / 2)
y = min(y, size.height - playerMonster.size.height / 2)
playerMonster.position = CGPointMake(x, y)
}
}
但是我不能同时将一个精灵向左移动,另一个向右移动。有没有办法在精灵套件中做到这一点?
请检查这个问题的答案: spritekit 中的多点触控
这个想法是您应该单独跟踪单个触摸。