gpt4 book ai didi

ios - SpriteKit - touchesMoved 调用不够频繁 - 快速移动

转载 作者:可可西里 更新时间:2023-11-01 00:37:08 24 4
gpt4 key购买 nike

我正在用 Swift 编写一个基于 SpriteKit 的游戏。 SKScene 包含一个 SKSpriteNode,它包含 SKShapeNodes。其中一种形状是“玩家”。用户应该能够拖放播放器。我实现了不同的方法来让这个工作用户 touchesBegan、touchesMoved、touchesCanceled 和 touches Ended。 基本上一切正常,只要用户以正常速度拖动“播放器”。但是,如果用户拖动“播放器”非常快,则 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 来移动您的播放器。

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)
}

关于ios - SpriteKit - touchesMoved 调用不够频繁 - 快速移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com