gpt4 book ai didi

swift - 将 SKSpriteNode 旋转到另一个 SKSpriteNode

转载 作者:行者123 更新时间:2023-11-28 05:40:22 25 4
gpt4 key购买 nike

我正在尝试将一个 sprite 节点旋转到另一个。它旋转但不好。我该如何解决?

我试过:

let blaster = self.childNode(withName: blaster)
let currentBlasterPosition = blaster!.position
let angle = atan2(currentBlasterPosition.y - cubes[0].position.y, currentBlasterPosition.x - cubes[0].position.x)
let rotateAction = SKAction.rotate(toAngle: angle + 90, duration: 0.0)

blaster!.run(SKAction.sequence([rotateAction]))

SKSpriteNode 从它应有的位置(取决于它的位置)旋转大约 -30 到 30 度。

game sprites screenshot

最佳答案

与其自己计算角度,不如使用 SKConstraint为了那个任务。假设您的 cubes 是一个 SKNodes 数组:

let constraint = SKConstraint.orient(to: cubes[0], offset: SKRange(constantValue:0))
blaster!.constraints = [constraint]

您只需执行一次,而不是每一帧。约束会自动应用于每一帧。要删除它,请将 blaster 的约束设置回 nil:

blaster!.constraints = nil

关于swift - 将 SKSpriteNode 旋转到另一个 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56986152/

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