我在Swift中编写一个基于SpriteKit的游戏。 SKScene包含一个包含SKShapeNodes的SKSpriteNode。其中一个形状是"播放器"。用户应该能够拖放播放器。我实现了不同的方法来让这个工作用户 touchesBegan,touchesMoved,touchesCanceled和触及结束。基本上一切正常,只要用户拖动"播放器"以正常速度。但是如果用户拖动"播放器" 非常快 touchesMoved方法通常不会被调用。我认为这可能是因为我使用SKShapeNodes但基于输出(如下)似乎没有是这样的。
代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchLocation = touches.first!.locationInNode(self)
if(self.player.containsPoint(touchLocation)){
self.startedTouchingPlayer()
}
super.touchesBegan(touches, withEvent: event)
print("TOUCHES BEGAN")
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchLocation = touches.first!.locationInNode(self)
if(self.player.containsPoint(touchLocation)){
self.isTouchingPlayer(touchLocation)
print("TOUCHES MOVED \(touchLocation)")
}
super.touchesMoved(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.endedTouchingHelper()
super.touchesEnded(touches, withEvent: event)
print("TOUCHES ENDED")
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
print("TOUCHES CANCELED")
self.endedTouchingHelper()
super.touchesCancelled(touches, withEvent: event)
}
列印声明:
正常运动:
TOUCHES BEGAN
TOUCHES MOVED (-4.0, -241.499984741211)
TOUCHES MOVED (0.500015258789062, -239.0)
TOUCHES MOVED (4.0, -237.000015258789)
TOUCHES MOVED (11.0, -234.0)
TOUCHES MOVED (19.5, -229.499984741211)
TOUCHES MOVED (27.0000152587891, -225.499984741211)
TOUCHES MOVED (34.0, -221.499984741211)
TOUCHES MOVED (41.0, -217.499984741211)
TOUCHES MOVED (48.0, -213.499984741211)
TOUCHES MOVED (53.4999847412109, -210.0)
...
TOUCHES MOVED (77.5, -177.000015258789)
TOUCHES MOVED (77.5, -178.0)
TOUCHES ENDED
快速运动:
TOUCHES BEGAN
TOUCHES MOVED (6.5, -277.5)
TOUCHES MOVED (3.0, -261.000030517578)
[*** at this point the finger is at a different position as the "player" ***] TOUCHES ENDED
感谢任何形式的帮助。感谢。
答案 0 :(得分:3)
你还没有发布你如何移动你的玩家,所以我假设你只是改变你的玩家位置。我建议您使用SKAction
来移动播放器。
func moveToPosition(oldPosition: CGPoint, newPosition: CGPoint) {
let xDistance = fabs(oldPosition.x - newPosition.x)
let yDistance = fabs(oldPosition.y - newPosition.y)
let distance = sqrt(xDistance * xDistance + yDistance * yDistance)
let sceneDiagonal = sqrt(world.frame.size.width * world.frame.size.width + world.frame.size.height * world.frame.size.height)
self.runAction(SKAction.moveTo(newPosition, duration: Double(distance / sceneDiagonal / 2)))
}
世界 -
的尺寸完全相同SKNode
与SKScene
并像这样使用它:
touchesMoved
:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchLocation = touches.first!.locationInNode(self)
if(self.player.containsPoint(touchLocation)){
moveToPosition(self.player.position, newPosition: touchLocation)
}
答案 1 :(得分:2)
问题不在于我移动玩家的方式就是这一行在touchesMoved:
if(self.player.containsPoint(touchLocation))
当用户移动得非常快时,touchLocation不在播放器内部(因为该位置无法快速更新)。我在声明中更改了条件,因此只检查播放器当前是否被拖动(或允许拖动)。
感谢。