kodeco.com Forums

How To Create a Breakout Game with Sprite Kit and Swift: Part 2

Learn how to make a breakout game for iOS with Sprite Kit and Swift via a fun hands-on tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1160-how-to-make-a-breakout-game-with-spritekit-and-swift-part-2

Another great and fun tutorial from RW. In particular, a little sample of what GameplayKit does was helpful. Thanks!

I can’t understand this part of the code:

`let xSpeed = sqrt(ball.physicsBody!.velocity.dx * ball.physicsBody!.velocity.dx)
let ySpeed = sqrt(ball.physicsBody!.velocity.dy * ball.physicsBody!.velocity.dy)`

You get the square root of a number powered to 2. Which means you’re getting the same number.

Yet, if I change the code to “let xSpeed = ball.physicsBody!.velocity.dx” it behaves completely different

What am I getting wrong ?

It took us a while to realise, but…

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.

I can’t understand this part of the code:

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?

Hi.
Sometimes the ball always flies out of the screen, why is that? The project is compiled entirely from your code.
Thanks.

You can simply take the function abs ()

this game is not working well in ipad what changes i need for that.

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! :]