blue
blue

Reputation: 7375

Swift sprite kit game: detect touch and execute separate code for different parts of screen?

Essentially, I need to know how to say if the user touched the left side of the screen, execute this code, but if they touched the right side of the screen, execute this code.

As it is, I have code that is executed when the screen is tapped anywhere because I have it within:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {

            let location = touch.locationInNode(self)
}
}

But I need to know how to make the the location of the tap more specific, i.e. right or left. Is there a way to do this by altering the code or do I have to use buttons? How do I do this?

Upvotes: 1

Views: 464

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You can write a condition like this

let location = touch.locationInNode(self)
if location.x < self.size.width/2 {
    // left code
}
else {
   // right code
}

Upvotes: 3

Related Questions