2017-10-17 78 views
1

我正在尝试在一个星球周围制作太空飞船轨道。我目前在做Xcode Swift SKAction.follow起点

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动作,玩家直接远距传物在地球下方,然后开始沿着路径。有没有办法让玩家沿着目前玩家的路径开始玩?我愿意彻底改变我如何让船只在一个圆圈内移动,只要他开始他在哪里,绕着一个星球绕行。

回答

0

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

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

希望这会有所帮助!

+0

不幸的是,这不会达到我期待的目标。它可能在其他情况下工作,但在我的情况下,对象有物理机构,如果我尝试了类似的东西,就会发生碰撞。 (我想让它们碰撞,但不是在这种情况下) –

0

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

+0

Could not you just just done this:let rect = CGRect(x:radius,y:radius,width:2 * radius,height:2 * radius)'let bezPath = UIBezierPath(roundedRect:rect,cornerRadius:0)' – Knight0fDragon