この記事の指示に従うと、 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コマンドもいくつかありますが、これは特に重要ではありません。うまく動作します。だから、なぜ速度が変化しないのですか?コンピュータのシミュレータが処理するコードが多すぎますか?それとも私はどこかでミスをしたことがありますか?私はどんなフィードバックにも感謝しています。
ありがとうございます。
あなたは下剤を作成するのを忘れました体? 'physicsBody'を' physicsBody! 'に変更してください。それがクラッシュするかどうかを確認してください。そうであれば、物理学の作成を忘れたことを意味します。 – Sweeper
私のGameScene.sksの各スプライトのAttributesインスペクタでは、境界円タイプの物理学のボディを割り当てましたが、何も起こりません。私はGameScene.swiftでそれらを作成するために何かを追加する必要がありますか?または、属性インスペクタに、移動を許可するように設定されていることを確認する必要のある設定がありますか? –
私はまたあなたの提案を試みました、そして、いいえ、それはクラッシュしませんか?他のアイデア? –