2017-01-24 3 views
2

この記事の指示に従うと、 Swift-Making an SKNode simply "move forward" on an angle私は角度をランダム化し、私のスプライトがその角度でオフに移動持たなければならないために、このコードを書かれている:Swift-SKSpriteNodeの速度が変更されない

func setDegrees() { 
    let ball1Degrees:UInt32 = arc4random_uniform(360) 
    if ball1Degrees > 300 { 
     ball2Degrees = Int(ball1Degrees) - 360 
     ball3Degrees = Int(ball1Degrees) - 300 
    } 
    else if ball1Degrees > 240 { 
     ball2Degrees = Int(ball1Degrees) + 60 
     ball3Degrees = Int(ball1Degrees) - 360 
    } 
    else { 
     ball2Degrees = Int(ball1Degrees) + 60 
     ball3Degrees = Int(ball1Degrees) + 120 
    } 
} 

func myFunction() { 
    ball1.position = CGPoint(x: 0, y: 0) 
    ball2.position = CGPoint(x: 0, y: 0) 
    ball3.position = CGPoint(x: 0, y: 0) 
    setDegrees() 
    if ball1Degrees < 90 { 
     ball1.physicsBody?.velocity = CGVector(dx: 1, dy: tan(Double(ball1Degrees) * (.pi/180))) 
    } 
    if ball1Degrees == 90 { 
     ball1.physicsBody?.velocity = CGVector(dx: 1, dy: 0) 
    } 
    if ball1Degrees > 90 && ball1Degrees <= 180 { 
     ball1.physicsBody?.velocity = CGVector(dx: -1, dy: tan(Double(180 - ball1Degrees) * (.pi/180))) 
    } 
    if ball1Degrees > 180 && ball1Degrees <= 270 { 
     ball1.physicsBody?.velocity = CGVector(dx: -1, dy: -tan(Double(ball1Degrees - 180) * (.pi/180))) 
    } 
    if ball1Degrees > 270 && ball1Degrees <= 0 { 
     ball1.physicsBody?.velocity = CGVector(dx: 1, dy: -tan(Double(360 - ball1Degrees) * (.pi/180))) 
    } 
    if ball2Degrees < 90 { 
     ball2.physicsBody?.velocity = CGVector(dx: 1, dy: tan(Double(ball2Degrees) * (.pi/180))) 
    } 
    if ball2Degrees == 90 { 
     ball2.physicsBody?.velocity = CGVector(dx: 1, dy: 0) 
    } 
    if ball2Degrees > 90 && ball2Degrees <= 180 { 
     ball2.physicsBody?.velocity = CGVector(dx: -1, dy: tan(Double(180 - ball2Degrees) * (.pi/180))) 
    } 
    if ball2Degrees > 180 && ball2Degrees <= 270 { 
     ball2.physicsBody?.velocity = CGVector(dx: -1, dy: -tan(Double(ball2Degrees - 180) * (.pi/180))) 
    } 
    if ball2Degrees > 270 && ball2Degrees <= 0 { 
     ball2.physicsBody?.velocity = CGVector(dx: 1, dy: -tan(Double(360 - ball2Degrees) * (.pi/180))) 
    } 
    if ball3Degrees < 90 { 
     ball3.physicsBody?.velocity = CGVector(dx: 1, dy: tan(Double(ball3Degrees) * (.pi/180))) 
    } 
    if ball3Degrees == 90 { 
     ball3.physicsBody?.velocity = CGVector(dx: 1, dy: 0) 
    } 
    if ball3Degrees > 90 && ball3Degrees <= 180 { 
     ball3.physicsBody?.velocity = CGVector(dx: -1, dy: tan(Double(180 - ball3Degrees) * (.pi/180))) 
    } 
    if ball3Degrees > 180 && ball3Degrees <= 270 { 
     ball3.physicsBody?.velocity = CGVector(dx: -1, dy: -tan(Double(ball3Degrees - 180) * (.pi/180))) 
    } 
    if ball3Degrees > 270 && ball3Degrees <= 0 { 
     ball3.physicsBody?.velocity = CGVector(dx: 1, dy: -tan(Double(360 - ball3Degrees) * (.pi/180))) 
    } 

} 

myFunction() 

しかし、私のゲームが実行されると、上記速度変化のいずれも適用されているように見えません。スクリーンの中央に位置を変えても正常に動いています。スケーリングのSKActionコマンドもいくつかありますが、これは特に重要ではありません。うまく動作します。だから、なぜ速度が変化しないのですか?コンピュータのシミュレータが処理するコードが多すぎますか?それとも私はどこかでミスをしたことがありますか?私はどんなフィードバックにも感謝しています。

ありがとうございます。

+0

あなたは下剤を作成するのを忘れました体? 'physicsBody'を' physicsBody! 'に変更してください。それがクラッシュするかどうかを確認してください。そうであれば、物理学の作成を忘れたことを意味します。 – Sweeper

+0

私のGameScene.sksの各スプライトのAttributesインスペクタでは、境界円タイプの物理学のボディを割り当てましたが、何も起こりません。私はGameScene.swiftでそれらを作成するために何かを追加する必要がありますか?または、属性インスペクタに、移動を許可するように設定されていることを確認する必要のある設定がありますか? –

+0

私はまたあなたの提案を試みました、そして、いいえ、それはクラッシュしませんか?他のアイデア? –

答えて

0

誤って実行したことがいくつかあります。しかし、それらは本当に簡単に修正できます。 DY、ない値DXとDYの

まず第一に、私の他の答えに、私はDXのを述べました。現在、ノードの速度は非常に遅いので、違いは見えません。 100でDXとDYを掛け:

ball1.physicsBody?.velocity = CGVector(dx: 100, dy: -tan(Double(360 - ball1Degrees) * (.pi/180)) * 100) 

はまた、これは、文が実行されない場合:

if ball1Degrees > 270 && ball1Degrees <= 0 { 

私は、あなたが試してみてくださいと思う:

if ball1Degrees > 270 && ball1Degrees <= 360 { 
+0

ああ、私は参照してください。私は<= 0のものを変更し、dxとdyをそれぞれ50倍しました。しかし、関数が実行されるたびにスプライトは消えます。少なくとも、彼らは指示されたとおりに中央に移動し、スケーリングしました。今は彼らはまったく動かず、中心にではなく、拡大縮小ではなく、ただのものではありません。私はこれが私のコンピュータのシミュレータだけでこのすべてを処理しないかもしれないと思うが、それはまだ変わっている... –

関連する問題