2017-04-06 32 views
0

因此,我試圖通過重新創建流行的App Store遊戲來挑戰自我,One More Line使用SpriteKit和Swift 3;然而,我立即有一個大問題。如何讓Sprite節點繞另一個節點旋轉? (One More Line)

1)如何讓我的節點圍繞另一個節點旋轉,當用戶敲擊

2)我希望我的節點繼續按照其面對同一方向移動,當用戶釋放他們的手指(希望這就說得通了...)。

還挺喜歡這個

enter image description here

在我GameScene.sks我創建了一個不斷旋轉的中心節點和球員​​。然後,我這樣做:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    player.move(toParent: center) 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    player.move(toParent: self) 
} 

override func update(_ currentTime: TimeInterval) { 
    let dx = 5 * cos(player.zRotation) 
    let dy = 5 * sin(player.zRotation) 
    player.physicsBody?.applyImpulse(CGVector(dx: dx, dy: dy)) 

} 

球員向右移動,當我點擊它環繞在節點周圍,當我釋放它繼續向右移動。

enter image description here

我知道我會對此完全錯誤的。任何幫助將非常感激。謝謝!

+0

這一點是不正確的?在圓圈中移動是否正確,但是當你停止觸摸時移動是錯誤的? –

+0

是的,當我放開它時,它會向右移動,但不會保留它所面對的方向。它也越來越接近我不想要的中心節點,但我想我可以通過在中心節點的子節點停止衝動來解決這個問題。 –

+0

有什麼方法可以使用SKPhysicsJointLimit?那樣我就不需要讓它成爲中心節點的孩子了? –

回答

0

我不確定我完全理解你在找什麼,但是如果你只是想讓一個精靈在中心精靈周圍均勻旋轉,添加中心精靈,另一箇中心精靈(不可見),並將你的外部精靈添加到無形的中心精靈。然後旋轉你的兩個中心精靈。調整outerObject的x位置以使outerObject距離中心對象更近或更遠。

import SpriteKit 
import GameplayKit 

class GameScene: SKScene { 

    override func didMove(to view: SKView) { 

     anchorPoint = CGPoint(x: 0.5, y: 0.5) 

     let centerObject = SKSpriteNode(texture: nil, color: UIColor.blue, size: CGSize(width: 20, height: 20)) 
     addChild(centerObject) 

     let invisibleParent = SKSpriteNode() 
     addChild(invisibleParent) 

     let outerObject = SKSpriteNode(texture: nil, color: UIColor.red, size: CGSize(width: 50, height: 50)) 
     outerObject.position.x = 100 
     invisibleParent.addChild(outerObject) 

     centerObject.run(SKAction.repeatForever(SKAction.rotate(byAngle: 1, duration: 1))) 
     invisibleParent.run(SKAction.repeatForever(SKAction.rotate(byAngle: -1, duration: 1))) 

    }  

} 

enter image description here

相關問題