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
SpriteKit is Apple’s new game development framework for iOS and Mac OS. Not only does it come with some great graphics capabilities, but it also includes a physics engine which looks a lot like Box2D. Best of all, you do all the work using straight Objective-C!
There’s a lot you can do with SpriteKit, and a great way to start learning about how it works is to create a simple game.
In this tutorial, you are going to learn how to create a Breakout game using SpriteKit step-by-step, complete with collision detection, ball bouncing using physics effects, dragging the paddle via touches, and win/lose screens.
If you are new to SpriteKit, you should go through the SpriteKit Tutorial for Beginners before proceeding with this tutorial.
The SpriteKit World in Theory
Before you go any further, do you have a moment to talk… about physics?
In SpriteKit you work in two environments. The graphical world that you see on the screen and the physics world, which determines how objects move and interact.
The first thing you need to do when using SpriteKit physics is to change the world according to the needs of your game. The world object is the main object in SpriteKit that manages all of the objects and the physics simulation. It also sets up the gravity that works on physics bodies added to it. The default gravity is -9.81 thus similar to that of the earth. So, as soon as you add a body it would “fall down”.
Once you have created the world object, you can add things to it that interact according to the principles of physics. For this the most usual way is to create a sprite (graphics) and set its physics body. The properties of the body and the world determine how it moves.
Bodies can be dynamic objects (balls, ninja stars, birds, …) that move and are influenced by physical forces, or they can be static objects (platforms, walls, …) that are not influenced by those forces. When creating a body you can set a ton of different properties like shape, density, friction and many more. Those properties heavily influence how the body behaves within the world.
When defining a body, you might wonder about the units of their size and density. Internally SpriteKit uses the metric system (SI units). However within your game you usually do not need to worry about actual forces and mass, as long as you use consistent values.
Once you’ve added all of the bodies you like to your world, SpriteKit can take over and do the simulation. Now that you have a basic understanding of how things should work, let’s see it in code! Time for some Breakout!
Getting Started
Start by creating a new project. For this start up XCode, go to File\New\Project and choose the iOS\Application\SpriteKit Game template. Set the product name to BreakoutSpriteKitTutorial, select Devices>iPhone and then click Next. Select the location on your hard drive to save your project and then click Create.
Your game will need some graphics. You probably want to have at least a graphic for the ball, the paddle, the bricks, and a background image. You can download them from here. Drag and drop the files from Finder on to your XCode project. Make sure that the checkmark for Copy items into destination group’s folder (if needed) is ticked and press the Finish button.
Open MyScene.m. This class creates your game scene. The template includes some extra code that you will not need. So, replace the contents of the file with the following:
#import "MyScene.h"
static NSString* ballCategoryName = @"ball";
static NSString* paddleCategoryName = @"paddle";
static NSString* blockCategoryName = @"block";
static NSString* blockNodeCategoryName = @"blockNode";
@implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKSpriteNode* background = [SKSpriteNode spriteNodeWithImageNamed:@"bg.png"];
background.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addChild:background];
}
return self;
}
@end
The code here is very basic. First, you define a few constants that will help you to identify the game objects. Then, initWithSize:
initializes an empty scene with a specified size, creates a sprite using an image, positions it in the middle of the screen, and adds it to the scene.
Now you have an barebones project which displays a background image. However, the screen is still in portrait mode and you want Breakout to work in landscape mode instead. For this you need to do two things:
Remove the implementation for viewDidLoad
and instead, add the following code:
Setting up your scene in viewWillLayoutSubviews
ensures that the view is in the view hierarchy and hence laid out properly. In contrast, this does not work correctly in viewDidLayout
because the size of the scene is not known at that time. You can find a much more detailed explanation for this in the Switching to Landscape Orientation section of the SpriteKit Tutorial for Beginners.
- Select the project root in Xcode, then select the BreakoutSpriteKitTutorial target, make sure that the General tab is selected, and set the orientation to just landscape by ensuring that only the two landscape checkboxes are checked as shown below:
- If you’ve done the first step and load the app, you will realize that it still looks weird. Parts of the background are not displayed on screen. To fix this, you need to switch to ViewController.m.
Remove the implementation for
viewDidLoad
and instead, add the following code:-(void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; // Configure the view. SKView * skView = (SKView *)self.view; if (!skView.scene) { skView.showsFPS = YES; skView.showsNodeCount = YES; // Create and configure the scene. SKScene * scene = [MyScene sceneWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene]; } }
Setting up your scene in
viewWillLayoutSubviews
ensures that the view is in the view hierarchy and hence laid out properly. In contrast, this does not work correctly inviewDidLayout
because the size of the scene is not known at that time. You can find a much more detailed explanation for this in the Switching to Landscape Orientation section of the SpriteKit Tutorial for Beginners.
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}