gpt4 book ai didi

swift - 如何沿圆形路径旋转 Sprite ?

转载 作者:行者123 更新时间:2023-11-28 08:06:19 25 4
gpt4 key购买 nike

(Click here for the sprite image)

这是一个由四个单独的 Sprite 组成的轮子。我的目标是将它们作为轮子旋转。我已经尝试过“UIBezier”arccenter 类,但最终结果是单个 Sprite 沿中心移动但也旋转其形状,导致不想要的结果。这是相关代码。 'orientToPath' 设置为 true 时,旋转 Sprite 本身。

//path rotation
let dx = whlPurple.position.x - self.size.width/2
let dy = whlPurple.position.y - (whlRed.size.height/2 + (whlPurple.position.y - whlRed.size.height))
let rad = atan2(dy, dx)
let radius = sqrt((dx*dx) + (dy*dy))
let path = UIBezierPath(arcCenter: CGPoint(x: self.size.width/2, y: (whlRed.size.height/2 + (whlPurple.position.y - whlRed.size.height))), radius: radius, startAngle: rad, endAngle: rad + CGFloat(M_PI * 6), clockwise: true)

let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 200)

whlPurple.run(SKAction.repeatForever(follow))

最佳答案

创建一个 SKNode 并将其放在圆的中心。

然后将您的 Sprite 添加到 CGPoint(x:radius,y:0) 的位置 (addChild)。您可以设置 SKNode 的 zRotation 以选择您在圆上的起始位置。

当您旋转 SKNode 时,您的 sprite 将以完美的圆形移动。例如,要让恶意在 5 秒内传播,您可以使用 SKAction.rotate(by: 2*pi, duration:5) 并永远重复(在包含 Sprite 的 SKNode 上(而不是在 Sprite 本身上))。

关于swift - 如何沿圆形路径旋转 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002670/

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