gpt4 book ai didi

ios - Xcode Swift SKAction.follow 起点

转载 作者:可可西里 更新时间:2023-11-01 02:00:31 26 4
gpt4 key购买 nike

我正在尝试让宇宙飞船绕行星运行。我目前正在做

let playerPos = player.position
let planetPos = planet.position

let radius = playerPos.x - planetPos.x

let rect = CGRect(x: planetPos.x - radius, y: planetPos.y - radius, width: 2 * radius, height: 2 * radius)

let bezPath = UIBezierPath(roundedRect: rect, cornerRadius: 0)
let path = bezPath.cgPath

let shape = SKShapeNode(path: path)
shape.strokeColor = .blue
shape.zPosition = 10
self.addChild(shape)

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

这确实创建了正确的路径,screenshot

但是,当我尝试运行 move Action 时,玩家直接传送到行星下方,然后开始沿着路径前进。有没有办法让它开始跟随玩家当前所在的路径?我愿意完全改变我让飞船绕圈运动的方式,只要他从他所在的地方开始,绕着一个星球转一圈。

最佳答案

解决方案是使用 CGMutablePath相反

let dx = playerPos.x - planetPos.x
let dy = playerPos.y - planetPos.y
let currentTheta = atan(dy / dx)
let endTheta = currentTheta + CGFloat(Double.pi * 2)

let newPath = CGMutablePath.init()
newPath.move(to: player.position)
newPath.addArc(center: planetPos, radius: radius, startAngle: currentTheta, endAngle: endTheta, clockwise: false)

let move = SKAction.follow(newPath, asOffset: false, orientToPath: true, speed: 200)
player.run(SKAction.repeatForever(move))

newPath.move(to: player.position) 行在船的位置开始路径,newPath.addArc 行从玩家位置绘制一个圆圈,围绕地球进行 360 度旋转,最终回到玩家的位置。

关于ios - Xcode Swift SKAction.follow 起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46798753/

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