我在 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()
}
Run Code Online (Sandbox Code Playgroud)
然而,问题是如果用户将手指放在手机上。TouchesBegan 仅被调用一次,即点击开始时,而不是按住时。我希望玩家角色不断地尝试到达手指。
我将相机集中在节点上,因此节点应该触摸手指的唯一时间是用户将手指放在节点上/节点内(即与节点相同的位置)。因此,在我运行 SKAction 来移动节点后,触摸无效,因为它位于旧位置。
我该怎么做?