sprite-kitwatchkitwatchos-3

TouchEvents in watchOS 3 SpriteKit?


When using SpriteKit in watchOS 3, how do you handle the touch events? I am porting the SpriteKit games from iOS and the codes below won't work. Or you have to control the WKInterfaceController somehow?

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

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)  {}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)  {}

Solution

  • After a lot of frustration with the same issue, I wound up using gesture recognizers, and calculating the position from there.

    @IBAction func handleSingleTap(tapGesture: WKTapGestureRecognizer) {
        let location = tapGesture.locationInObject()
        if yourSpriteNodeRect.contains(location) {
            //do stuff
        }
    }
    

    One tip though: A strange bug I found though when creating the method to handle the gesture recognizer was that the gesture wasn't being passed by default into the method. I had to ctrl + drag to create the method, and then modify the signature to contain the gesture. If I created the method with the gesture in the signature, Xcode wouldn't allow me to attach the gesture recognizer to it.

    It would seem that there would be a better way to detect the touches, but that's the workaround I found for now.