2017-10-17 76 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