2

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.

Community
  • 1
  • 1
  • Did you forget to create the physic bodies? Change `physicsBody?` to `physicsBody!`. See if it crashes. If it does, it means that you forgot to create the physics bodies. – Sweeper Jan 24 '17 at 06:49
  • In each sprite's Attributes inspector in my GameScene.sks I've assigned bounding circle type physics bodies, but still nothing happens. Do I need to add something to create them in my GameScene.swift? Or is there a setting in the Attributes inspector that I need to make sure is set a certain way to allow it to move? – Dan Harward Jones Jan 24 '17 at 07:00
  • I also just tried your suggestion, and no, it doesn't crash? Any other ideas? – Dan Harward Jones Jan 24 '17 at 07:01
  • Did my answer work for you? – Sweeper Jan 24 '17 at 08:49
  • See my comment below, so no, not entirely... – Dan Harward Jones Jan 24 '17 at 08:52

1 Answers1

0

There are a few things you did wrongly. But they are really easy to fix.

First of all, in my other answer, I mentioned the ratio of dx : dy, not the values of dx and dy. Currently, your nodes' speeds are really slow so you can't see a difference. Multiply dx and dy by 100:

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

Also, this if statement will not be executed:

if ball1Degrees > 270 && ball1Degrees <= 0 {

I think you should try:

if ball1Degrees > 270 && ball1Degrees <= 360 {
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Ah, I see. I changed the <= 0 thing, and multiplied the dx and dy by 50 each. But now whenever the function is run, the sprites just disappear. At least before they moved to the centre and scaled as instructed. Now they don't move at all, not to the centre, not scaling, just nothing. I suspect this may be just my computer's simulator not handling all of this, but it's still strange... – Dan Harward Jones Jan 24 '17 at 08:50