Marshall D
Marshall D

Reputation: 454

Detecting multiple touches at once (Swift)

Im using Xcode 7, Swift, and SpriteKit and I'm attempting to allow the user to use two fingers at once in my app.

Basically I have two halves to my screen, and I want separate touch-recognition for each side, and simultaneously.

Here is my current code below :

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        guard let touch = touches.first else {
            return;
        }
        let location = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(location)
        if (touchedNode == touch1){
            //code1
        }
        else if (touchedNode == touch2){
            //code2
        }
    }

touch1 and touch2 are SkSpriteNodes that each take up a different half of the screen.
This code works well, as long as you only have 1 finger on the screen at a time.
However if there are two(1 for each half), which ever one was placed on the screen first is the one that is registered.

How do I make it so that both are being registered, and therefore code1 and code2 are being run?

Upvotes: 0

Views: 2158

Answers (1)

Whirlwind
Whirlwind

Reputation: 13675

You need multipleTouchEnabled property set to true. From the docs about this property :

When set to YES, the receiver receives all touches associated with a multi-touch sequence. When set to NO, the receiver receives only the first touch event in a multi-touch sequence. The default value of this property is NO.

EDIT:

Based on your comments, you might try this (making sprites responsive to touches):

class Button:SKSpriteNode {

    init(size:CGSize, color:SKColor) {

        super.init(texture: nil, color: color, size: size)

        userInteractionEnabled = true
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if let name = self.name {
            print("Button with \(name) pressed")
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let name = self.name {
            print("Button with \(name) pressed")
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let name = self.name {
            print("Button with \(name) released")
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class GameScene: SKScene, SKPhysicsContactDelegate {

    override func didMoveToView(view: SKView) {

        let left = Button(size: CGSize(width: frame.size.width/2.0, height: frame.size.height), color: .blackColor())
        left.name = "left"
        left.position = CGPoint(x: left.size.width/2.0, y: frame.midY)

        let right = Button(size: CGSize(width: frame.size.width/2.0, height: frame.size.height), color: .whiteColor())
        right.name = "right"
        right.position = CGPoint(x:frame.maxX-right.size.width/2.0, y: frame.midY)

        addChild(left)
        addChild(right)

    }
}

Upvotes: 1

Related Questions