SpriteKit - touchesMoved 调用不够频繁 - 快速移动
SpriteKit - touchesMoved not called often enough - fast movement
我正在 Swift 编写基于 SpriteKit 的游戏。 SKScene 包含一个 SKSpriteNode,它包含 SKShapeNodes。其中一种形状是 "player"。用户应该能够拖放 播放器。我实现了不同的方法来让这个工作用户 touchesBegan、touchesMoved、touchesCanceled 和 touchesEnded。 基本上一切正常,只要用户以正常速度拖动 "player"。但是如果用户拖动 "player" 非常快 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
感谢任何形式的帮助。谢谢
你还没有发布你是如何移动你的球员的,所以我假设你只是改变了你的球员位置。我建议您使用 SKAction
s 来移动您的播放器。
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)))
}
world - SKNode
of exactly same size as your 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)
}
Darvydas 为我指明了正确的方向,我能够像这样解决问题:
问题不在于我移动播放器的方式,而是 touchesMoved 中的这一行:
if(self.player.containsPoint(touchLocation))
当用户移动得非常快时,touchLocation 不在播放器内部(因为位置无法快速更新)。我更改了语句中的条件,因此它只检查玩家当前是否正在被拖动(或允许被拖动)。
- touchesBegan 将布尔值设置为 true 以指示玩家被触摸
- 只要 bool 为真,touchesMoved 总是会更新玩家位置(无论触摸发生在何处)
- touchesEnded 将布尔值设置为 false
谢谢。
我正在 Swift 编写基于 SpriteKit 的游戏。 SKScene 包含一个 SKSpriteNode,它包含 SKShapeNodes。其中一种形状是 "player"。用户应该能够拖放 播放器。我实现了不同的方法来让这个工作用户 touchesBegan、touchesMoved、touchesCanceled 和 touchesEnded。 基本上一切正常,只要用户以正常速度拖动 "player"。但是如果用户拖动 "player" 非常快 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
感谢任何形式的帮助。谢谢
你还没有发布你是如何移动你的球员的,所以我假设你只是改变了你的球员位置。我建议您使用 SKAction
s 来移动您的播放器。
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)))
}
world -
SKNode
of exactly same size as yourSKScene
并像这样使用它:
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)
}
Darvydas 为我指明了正确的方向,我能够像这样解决问题:
问题不在于我移动播放器的方式,而是 touchesMoved 中的这一行:
if(self.player.containsPoint(touchLocation))
当用户移动得非常快时,touchLocation 不在播放器内部(因为位置无法快速更新)。我更改了语句中的条件,因此它只检查玩家当前是否正在被拖动(或允许被拖动)。
- touchesBegan 将布尔值设置为 true 以指示玩家被触摸
- 只要 bool 为真,touchesMoved 总是会更新玩家位置(无论触摸发生在何处)
- touchesEnded 将布尔值设置为 false
谢谢。