Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)  {}
like image 648
Lim Thye Chean Avatar asked Jul 01 '16 04:07

Lim Thye Chean


1 Answers

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.

like image 196
LMVogel Avatar answered Oct 31 '22 08:10

LMVogel