2015-10-18 1 views
1

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

Cela devrait être ce que vous cherchez http://stackoverflow.com/questions/27343926/multi-touch-gesture-in-sprite-kit – 0x141E

+0

Merci beaucoup! –

Répondre

0

Je sais que c'est un peu en retard, mais j'ai pensé que j'essaierais de réduire les tracas pour quiconque dans le futur.

La façon dont je le faire est d'abord enlever

let touch = touches.first as UITouch? 

puis mettre toute votre fonction touchesMoved dans ce bloc de code:

for touch in touches 
{ 

} 

Vous pouvez maintenant utiliser touche de la même manière que vous avez utilisé votre touche originale, mais multi-touch sera mis en œuvre.