Upon instruction from this post here; Swift-Making an SKNode simply "move forward" on an angle I have written this code, to have an angle randomised and to have my sprite move off in that angle:
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()
But when my game is run, none of the above velocity changes appear to be being applied. The repositioning to the centre of the screen runs successfully, and there's even a few scaling SKAction commands that aren't particularly relevant here, that work just fine. So why is the velocity not changing? Is it too much code for my computer's simulator to handle? Or have I made a mistake somewhere? I'd be grateful for any feedback at all.
Thanks in advance.