我试图让我的 Sprite 移动并旋转到我触摸的位置,并随着我在屏幕上移动手指而跟随。运动部分工作正常,但我似乎无法让它旋转。我的 moveAndRotate 函数如下。
func moveAndRotate(spriteNode: SKSpriteNode, toPosition position: CGPoint) {
let angle = atan2(position.y - player.position.y, position.x - player.position.x)
let rotateAction = SKAction.rotate(toAngle: angle - -(CGFloat(Double.pi / 2)), duration: 0.05, shortestUnitArc: true)
player.run(rotateAction)
let offsetX = position.x - player.position.x
let offsetY = position.y - player.position.y
let normal = simd_normalize(simd_double2(x: Double(offsetX), y: Double(offsetY)))
velocity = CGVector(dx: CGFloat(normal.x) * movePointsPerSecond, dy: CGFloat(normal.y) * movePointsPerSecond)
}
最佳答案
rotateAction 的持续时间设置为 o.5,将其更改为 0 即可正常工作。
关于swift - Swift 4.1 中的 SpriteNode 移动和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53159624/