Adri
Adri

Reputation: 243

how to treat touches separately

I'm making a little game and I have come across a problem that I haven't been able to solve. The thing is that when I touch the screen an action occours. Well, I want that if there are 2 differents touches the action to happen 2 times, not only on the first one. Right now, if there are 2 players on the same device, the one who touches first is the one who wins, because the second player isn't able to even call the action. How can I invoke the action two times treating every touch recived as input?

In other words, I want to detect when there are two fingers on the screen and split every "finger" in the normal action for one finger.

Of course, my actions takes place here:

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //code to get input info
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
 //calling actions with the arguments of touchesEnded and with the ones of touchesBegan
}

Upvotes: 0

Views: 41

Answers (1)

NobodyNada
NobodyNada

Reputation: 7644

In your storyboard, you need to enable "Multiple Touch" for your view (it's in the Interaction section of the Attributes Inspector.) You can also set this property programmatically:

view.multipleTouchEnabled = true

However, if multiple touches happen at the same time, they will be passed in a single touchesBegan event. To make sure your app detects both touches, iterate over all of the touches in the set in touchesBegan:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        //code to get input info
    }
}

Upvotes: 2

Related Questions