Swift MultiTouch对象

时间:2015-02-07 02:24:58

标签: swift ios8 multi-touch

这是我在Swift的第一场比赛。我想用SpriteKit制作更高级的突围游戏,就像在tutorial

中一样

我想创建4个拨片并用4个手指单独移动它们,我试着这样做但是当我移动一个拨片时,第二个拨片也会移动...我该怎么办?

import SpriteKit
import AVFoundation

class GameScene: SKScene {

    var fingerIsOnPaddle1 = false
    var fingerIsOnPaddle2 = false
    var fingerISOnPaddle3 = false
    var fingerIsOnPaddle4 = false

    let shurikenCategoryName = "shuriken"
    let paddleCategoryName = "paddle"
    let paddle2CategoryName = "paddle2"
    let paddle3CategoryName = "paddle3"
    let paddle4CategoryName = "paddle4"
    let brickCategoryName = "brick"

    let backgroundMusicPlayer = AVAudioPlayer()

    override init(size: CGSize){
        super.init(size: size)

        let bgMusicURL = NSBundle.mainBundle().URLForResource("bgMusicMP3", withExtension: "mp3")

        backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: bgMusicURL, error: nil)
        backgroundMusicPlayer.numberOfLoops = -1
        backgroundMusicPlayer.prepareToPlay()
        backgroundMusicPlayer.play()

        let backgroundImage = SKSpriteNode(imageNamed: "bg")
        backgroundImage.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3)
        self.addChild(backgroundImage)

        self.physicsWorld.gravity = CGVectorMake(0, 0)

        let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody = worldBorder
        self.physicsBody?.friction = 0

        let shuriken = SKSpriteNode(imageNamed: "shuriken")
        shuriken.name = shurikenCategoryName
        shuriken.position = CGPointMake(self.frame.size.width/2, self.frame.size.height / 2)
        self.addChild(shuriken)

        shuriken.physicsBody = SKPhysicsBody(circleOfRadius: shuriken.frame.width / 2)
        shuriken.physicsBody?.friction = 0
        shuriken.physicsBody?.restitution = 1
        shuriken.physicsBody?.linearDamping = 0
        shuriken.physicsBody?.applyImpulse(CGVectorMake(2, 2))
        shuriken.physicsBody?.allowsRotation = true

        let paddle = SKSpriteNode(imageNamed: "paddle")
        paddle.name = paddleCategoryName
        paddle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)/4)
        self.addChild(paddle)

        paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
        paddle.physicsBody?.friction = 0.4
        paddle.physicsBody?.restitution = 0.1
        paddle.physicsBody?.dynamic = false

       //paddle.position = CGPointMake(CGRectGetMidX(self.frame)/2, CGRectGetMidY(self.frame)/4)
       //self.addChild(paddle)



        let paddle2 = SKSpriteNode(imageNamed: "paddle")
        paddle2.name = paddle2CategoryName
        paddle2.position = CGPointMake(CGRectGetMidX(self.frame)/5, CGRectGetMidY(self.frame))
        paddle2.zRotation = CGFloat(M_PI_2)
        self.addChild(paddle2)
        paddle2.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
        paddle2.physicsBody?.friction = 0.4
        paddle2.physicsBody?.restitution = 0.1
        paddle2.physicsBody?.dynamic = false

        let paddle3 = SKSpriteNode(imageNamed: "paddle")
        paddle3.name = paddle2CategoryName
        paddle3.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)/4*7)
        self.addChild(paddle3)

        let paddle4 = SKSpriteNode(imageNamed: "paddle")
        paddle4.name = paddle2CategoryName
        paddle4.position = CGPointMake(CGRectGetMidX(self.frame)/5*9, CGRectGetMidY(self.frame))
        paddle4.zRotation = CGFloat(M_PI_2)
        self.addChild(paddle4)        

   }

   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touch = touches.anyObject() as UITouch
        let touchLocation = touch.locationInNode(self)

        let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)

        if body?.node?.name == paddleCategoryName {
           println("Paddle 1 touched")
           fingerIsOnPaddle1 = true
        }

        if body?.node?.name == paddle2CategoryName{
           println("Paddle 2 touched")
           fingerIsOnPaddle2 = true
        }
   }

   override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
       if fingerIsOnPaddle1{
           let touch = touches.anyObject() as UITouch
           let touchLoc = touch.locationInNode(self)
           let prevTouchLoc = touch.previousLocationInNode(self)

           let paddle = self.childNodeWithName(paddleCategoryName) as SKSpriteNode

           var newXPos = paddle.position.x + (touchLoc.x - prevTouchLoc.x)

           paddle.position = CGPointMake(newXPos, paddle.position.y)
    }

    if fingerIsOnPaddle2{
        let touch2 = touches.anyObject() as UITouch
        let touchLoc2 = touch2.locationInNode(self)
        let prevTouchLoc2 = touch2.previousLocationInNode(self)

        let paddle2 = self.childNodeWithName(paddle2CategoryName) as SKSpriteNode

        var newYPos = paddle2.position.y + (touchLoc2.y - prevTouchLoc2.y)

        paddle2.position = CGPointMake(paddle2.position.y, newYPos)
     }

   }

   override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {


   }

   required init?(coder aDecoder: NSCoder){
       super.init(coder: aDecoder)
   }
}

1 个答案:

答案 0 :(得分:0)

问题的症结在于touchesBegan函数,它需要Set的修饰。这是一种非常常见的模式,只使用anyObject()就可以采用第一个模式,但是您可以像这样循环遍历它们:

   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        for touch in touches as? UITouch {
            let touchLocation = touch.locationInNode(self)

            let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)

            if body?.node?.name == paddleCategoryName {
                println("Paddle 1 touched")
                fingerIsOnPaddle1 = true
            }

            if body?.node?.name == paddle2CategoryName{
                println("Paddle 2 touched")
                fingerIsOnPaddle2 = true
            }
        }
   }