【问题标题】:SpriteNode movement and rotation in Swift 4.1Swift 4.1 中的 SpriteNode 移动和旋转
【发布时间】:2019-04-09 02:54:50
【问题描述】:

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

【问题讨论】:

  • 这个没有关系,不过angle - -(CGFloat(Double.pi / 2))可以改成angle + CGFloat.pi / 2更清楚。
  • 嗯,这可能不相关,但它现在正在旋转,它落后了一些,但比我所拥有的有所改进。非常感谢。
  • 你的项目肯定是有问题的。它经常发生在 Xcode 上。我谈到的代码在输出上是等效的。如果您不希望 SpriteKit 出现延迟,请在真实设备上运行您的项目。
  • 如果您从touchesMoved 调用moveAndRotate,您可能会在最后一个操作完成之前再次调用该方法。您可以通过使用键运行操作来避免这种情况,如果操作已经存在(当调用 moveAndRotate 时),请在创建/运行新操作之前将其删除。
  • 你正在堆叠你的动作,你需要删除之前的轮换,然后再添加一个新的

标签: swift sprite-kit sprite skspritenode skaction


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多