我在 SKScene 中有一个节点,我根据用户的触摸移动它。基本上,这个角色也应该试图跟随用户的手指(假设手指在屏幕上)。我目前已经这样实现了,效果很好:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
player.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 1))
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
player.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 1))
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
player.removeAllActions()
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
player.removeAllActions()
}
但是,问题是如果用户将他/她的手指放在手机上。 touchesBegan 只被调用一次,那是在点击开始的时候,而不是在它被按住的时候。我希望玩家角色不断地尝试伸手去拿手指。
我将相机放在节点的中心,因此节点应该触摸手指的唯一时间是用户将手指放在节点上/节点中(即与节点相同的位置)。因此,在我运行 SKAction 移动节点后,触摸无效,因为它位于旧位置。
我该怎么做?
最佳答案
你可以像这样注册一个长触摸事件:
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)
func longPressed(sender: UILongPressGestureRecognizer) {
// your code
}
关于ios - 在 Sprite Kit 中检测长触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337608/