问题描述
在watchOS 3中使用SpriteKit时,如何处理触摸事件?我正在从iOS移植SpriteKit游戏,并且下面的代码无法正常工作.还是必须以某种方式控制WKInterfaceController?
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?) {}
推荐答案
在对同一问题感到沮丧之后,我开始使用手势识别器,并从那里开始计算位置.
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
}
}
一个提示:在创建用于处理手势识别器的方法时,我发现了一个奇怪的错误,那就是默认情况下该手势没有被传递到该方法中.我必须按住Ctrl键并拖动以创建方法,然后修改签名以包含手势.如果我创建的方法带有签名中的手势,则Xcode不允许我将手势识别器附加到该方法.
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.
这篇关于watchOS 3 SpriteKit中的TouchEvents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!