在watchOS 3中使用SpriteKit时,如何处理触摸事件?我正在从 iOS 移植 SpriteKit 游戏,下面的代码不起作用。或者你必须以某种方式控制 WKInterfaceController ?
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?) {}
在对同一问题感到沮丧之后,我最终使用了手势识别器,并从那里计算位置。
@IBAction func handleSingleTap(tapGesture: WKTapGestureRecognizer) {
let location = tapGesture.locationInObject()
if yourSpriteNodeRect.contains(location) {
//do stuff
}
}
不过,有一个提示:在创建处理手势识别器的方法时,我发现了一个奇怪的错误,那就是手势默认情况下并未传递到该方法中。我必须按住 Ctrl 键并拖动来创建方法,然后修改签名以包含手势。如果我使用签名中的手势创建方法,Xcode 将不允许我将手势识别器附加到它。
似乎有更好的方法来检测触摸,但这就是我目前找到的解决方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)