Je fais un simple jeu Pong en utilisant SpriteKit + Swift. Je ne peux déplacer chaque pagaie qu'une seule fois, avec un seul doigt sur l'écran. J'ai déjà vu les autres questions qui sont liées, mais elles ne m'ont pas aidé. Mon code:Reconnaissance gestuelle multi-touch dans SpriteKit + Swift
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch?
let touchLocation = touch?.locationInNode(self)
let body: SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation!)
if body?.node!.name == PaddleCategoryName {
print("Paddle Touched!")
fingerIsOnPaddle = true
}
if body?.node!.name == PaddleCategoryName2 {
print("Paddle2 Touched!")
fingerIsOnPaddle2 = true
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if fingerIsOnPaddle {
let touch = touches.first as UITouch?
let touchLocation = touch?.locationInNode(self)
let previousTouchLocation = touch?.previousLocationInNode(self)
let paddle = self.childNodeWithName(PaddleCategoryName) as! SKSpriteNode
var newYPosition = paddle.position.y + (touchLocation!.y - previousTouchLocation!.y)
newYPosition = max(paddle.size.height/2, newYPosition)
newYPosition = min(self.size.height - paddle.size.height/2, newYPosition)
paddle.position = CGPointMake(paddle.position.x, newYPosition)
}
if fingerIsOnPaddle2 {
let touch = touches.first as UITouch?
let touchLocation = touch?.locationInNode(self)
let previousTouchLocation = touch?.previousLocationInNode(self)
let paddle2 = self.childNodeWithName(PaddleCategoryName2) as! SKSpriteNode
var newYPosition = paddle2.position.y + (touchLocation!.y - previousTouchLocation!.y)
newYPosition = max(paddle2.size.height/2, newYPosition)
newYPosition = min(self.size.height - paddle2.size.height/2, newYPosition)
paddle2.position = CGPointMake(paddle2.position.x, newYPosition)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
fingerIsOnPaddle = false
fingerIsOnPaddle2 = false
}
Cela devrait être ce que vous cherchez http://stackoverflow.com/questions/27343926/multi-touch-gesture-in-sprite-kit – 0x141E
Merci beaucoup! –