Swift 4.1中SpriteNode的移动和旋转

时间:2018-11-05 17:51:51

标签: swift sprite-kit sprite skspritenode skaction

我试图让我的精灵移动并旋转到触摸的位置,并随着手指在屏幕上的移动而跟随。运动部件工作正常,但我似乎无法使其旋转。我的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)
}

1 个答案:

答案 0 :(得分:0)

持续时间在rotateAction上设置为o.5,将其更改为0即可正常工作。