Sahbi Belgacem
Sahbi Belgacem

Reputation: 674

Sprite-kit: detecting left & right touches

In the new game that I'm trying to build, I want to know when the user has touched BOTH right and left side of the screen to do some logic.

My code:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        var isRight : Bool = false
        var isLeft : Bool = false

        if(location.x < self.size.width/2){
            isLeft = true
            println("Left")
        }

        if(location.x > self.size.width/2){
            //
            isRight = true
            println("Right")
        }
        if (isRight && isLeft){
            println("Both touched")
            // do something..
        }
    }
}

Console:

Right
Left

My code doesn't seem to work. What am I doing wrong here ? Does anybody have a better solution ?

Upvotes: 4

Views: 871

Answers (1)

Shoaib
Shoaib

Reputation: 2294

DO this;

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

   var isRight : Bool = false
   var isLeft : Bool = false

   for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

        if(location.x < self.size.width/2){
            isLeft = true
            println("Left")
        }

        if(location.x > self.size.width/2){
            //
            isRight = true
            println("Right")
        }
    }

   if (isRight && isLeft){
      println("Both touched")
      // do something..
   }

}

Updated: Swift 2.2

For SKView:

class skView:SKView {

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */

        var isRight : Bool = false
        var isLeft : Bool = false

        for touch in touches {
            let location = touch.locationInView(self);

            if(location.x < self.frame.size.width/2){
                isLeft = true
                print("Left")
            }

            if(location.x > self.frame.size.width/2){
                //
                isRight = true
                print("Right")
            }
        }

        if (isRight && isLeft){
            print("Both touched")
            // do something..
        }

    }
}

For SKNode:

class skNode: SKNode {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        var isRight : Bool = false
        var isLeft : Bool = false

        for touch in touches {
            let location = touch.locationInNode(self);

            if(location.x < self.frame.size.width/2){
                isLeft = true
                print("Left")
            }

            if(location.x > self.frame.size.width/2){
                //
                isRight = true
                print("Right")
            }
        }

        if (isRight && isLeft){
            print("Both touched")
            // do something..
        }
    }
}

Upvotes: 5

Related Questions