When you multiply a number by itself, it will always be positive; therefore if you take the squareroot of that number, that will also be positive.
This is because the xSpeed/ySpeed variables are only reading the velocity and not being reassigned, so in the “if xSpeed < 10.0” section, if it just reads velocity, it will be negative whilst moving left; therefore causing the ball to jump around in random directions.
if xSpeed <= 10.0 {
ball.physicsBody!.applyImpulse(CGVector(dx: randomDirection(), dy: 0.0))
}
if ySpeed <= 10.0 {
ball.physicsBody!.applyImpulse(CGVector(dx: 0.0, dy: randomDirection()))
}
If the ball’s initial speed is (3.0, 3.0), then the code above will be executed. If the randomDirection() method returns -3.0, will the ball’s speed become (0.0, 3.0)? In this situation, will the ball run straight up?
This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]