How To Create A Breakout Game Using SpriteKit
Learn how to create a breakout game for iOS using SpriteKit! By Barbara Reichart.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How To Create A Breakout Game Using SpriteKit
40 mins
Winning the Game
To let the player actually win the game, add this method to MyScene.m.
-(BOOL)isGameWon {
int numberOfBricks = 0;
for (SKNode* node in self.children) {
if ([node.name isEqual: blockCategoryName]) {
numberOfBricks++;
}
}
return numberOfBricks <= 0;
}
The new method checks to see how many bricks are left in the scene by going through all the scene’s children. For each child, it checks whether the child name is equal to blockCategoryName
. If there are no bricks left, the player has won the game and the method returns YES
.
Now, go back to didBeginContact:
and replace the TODO
line in the final if
condition with the following:
if ([self isGameWon]) {
GameOverScene* gameWonScene = [[GameOverScene alloc] initWithSize:self.frame.size playerWon:YES];
[self.view presentScene:gameWonScene];
}
The code checks whether the player has won by calling the new method you just implemented. If he has won, you show the GameOverScene with a message indicating victory.
Finishing Touches
As you play the game, you may have noticed that sometimes the ball can get super-fast or super-slow, depending on how you hit it with the paddle.
You can prevent these speed variations by overriding the default update:
method for MyScene.m like so:
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
SKNode* ball = [self childNodeWithName: ballCategoryName];
static int maxSpeed = 1000;
float speed = sqrt(ball.physicsBody.velocity.dx*ball.physicsBody.velocity.dx + ball.physicsBody.velocity.dy * ball.physicsBody.velocity.dy);
if (speed > maxSpeed) {
ball.physicsBody.linearDamping = 0.4f;
} else {
ball.physicsBody.linearDamping = 0.0f;
}
}
update:
is called before each frame is rendered. In the case of MyScene.m, there is no update:
method since the default implementation has been used till now. Here, you override it with your own implementation.
You get the ball and check its velocity
, essentially the movement speed. If it’s too high, you increase the linear damping so that the ball will eventually slow down.
If you compile, run, and play the game, you should see the ball go back to a normal speed level when the speed increases too much.
Gimme the Code!
Here’s the full code for the SpriteKit Breakout Game that you’ve made in this tutorial.
Where To Go From Here?
Obviously, this is a quite simple implementation of Breakout. But now that you have this working, there’s a lot more you can do. You could extend this code to give the blocks hit points, have different types of blocks, and make the ball have to hit some of them (or all of them) a number of times before they are destroyed. You could add blocks which drop bonuses or power-ups, let the paddle shoot lasers toward the blocks, whatever you dream up!
Let me know if you have any tips or suggestions for better ways to do things, and hope this comes in handy!