Xcode Swift SKAction.follow起点

时间:2017-10-17 20:33:48

标签: ios swift xcode sprite-kit skaction

我正试图围绕一颗行星制造宇宙飞船轨道。我正在做

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动作时,玩家会直接传送到行星下方,然后开始沿着路径行进。有没有办法让它开始遵循玩家目前所在的路径?我愿意彻底改变我如何使船只在一个圆圈中移动,只要他从他所在的地方开始,绕着一个星球旋转。

2 个答案:

答案 0 :(得分:1)

解决方案是使用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度旋转,最后回到玩家的位置。

答案 1 :(得分:0)

如果我理解正确,您希望玩家从当前位置移动到路径上的某个位置,然后开始沿着该路径行进。

如果是这样,您可以考虑运行另一个动作,首先将玩家从当前位置移动到路径上的某个起点,例如diff。然后,当玩家在该点上时,运行您已定义的CGPoint(x: planetPos.x - radius, y: planetPos.y - radius)动作。您可以使用move依次运行操作。

希望这有帮助!