Bucket
Bucket

Reputation: 56

touching multiple nodes with swift - IOS

EDIT to solve my issue i forgot to include this code into my code originally, I'm placing it here for anybody with the same issues later.

override func didMoveToView(view: SKView) {
    self.view?.multipleTouchEnabled = true // --------- set multiple touch
}

i am making a game with 3 buttons (nodes), a button to move left, right and a fire button for all my shooting needs. Anyway, i want to allow for my game to recognise both a shooting and movement touch at the same time. here's all my touch code:

override func touchesBegan(touches: Set<UITouch>, withEvent event:   UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let touches = self.nodeAtPoint(location)

        if (touches.name == "rightBtn") {
            rightDown = true
        }

        if (touches.name == "leftBtn") {
            leftDown = true
        }

        if (touches.name == "fireBtn") {
            shooting = true
        }
        if (touches.name == "fireBtn" && touches.name == "rightBtn") {
            rightDown = true
            shooting = true
        }
        if (touches.name == "fireBtn" && touches.name == "leftBtn") {
            leftDown = true
            shooting = true
        }

    }

}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let touches = self.nodeAtPoint(location)
        let prevLocation = touch.previousLocationInNode(self)
        let prevLocationTouches = self.nodeAtPoint(prevLocation)

        if (touches.name == "rightBtn" && prevLocationTouches.name == "rightBtn") {
            rightDown = true
            leftDown = false
        } else if (touches.name == "leftBtn" && prevLocationTouches.name == "leftBtn") {
            rightDown = false
            leftDown = true
        } else {
            rightDown = false
            leftDown = false
        }

        if (touches.name == "fireBtn" && prevLocationTouches.name == "fireBtn") {
            shooting = true
        }
        if (touches.name == "fireBtn" && prevLocationTouches.name == "rightBtn") {
            shooting = true
            rightDown = true
        }
        if (touches.name == "fireBtn" && prevLocationTouches.name == "leftBtn") {
            leftDown = true
            shooting = true
        }
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let touches = self.nodeAtPoint(location)

        if (touches.name == "rightBtn") {
            rightDown = false
        } else if (touches.name == "leftBtn") {
            leftDown = false
        } else {
            shooting = false
        }

    }
}

this code doesn't allow for two simultaneous touches. is there anything I'm missing?

Upvotes: 0

Views: 629

Answers (1)

Sean Park
Sean Park

Reputation: 36

Maybe you have forgotten to enable multiple touches in the view?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled

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.

Upvotes: 2

Related Questions