Creating Your First SpriteKit Project
Table of Contents
Xcode playgrounds are good for prototyping game ideas, but if you want to get a SpriteKit game on your iPhone or iPad, you must create a project in Xcode. This article guides you through creating a SpriteKit project in Xcode.
Launch Xcode and choose File > New > Project to create a project. When you choose File > New > Project the New Project Assistant opens.
At the top of the New Project Assistant is a list of platforms: Multiplatform, iOS, macOS, watchOS, tvOS, and Other. Multiplatform, iOS, macOS, and tvOS all have game project templates. You can choose iOS, but I’m going with Multiplatform in this article for the following reasons:
- On some versions of Xcode, the iOS game project template is missing a launch storyboard. The missing storyboard causes the scene to be much smaller than the screen size, giving you black bars on the edges of the screen. Read the Make Background Fill Entire SpriteKit Scene article for a way to work around the bug.
- I find that building and testing works better on the Mac than the iOS simulator.
- Having a Mac target will make things easier for those of you who don’t have a paid developer account. With a free account you must create a provisioning profile every week and reinstall your games on the device.
After choosing the platform, select the Game template. Click the Next button to move to the next step.
After clicking the Next button, it’s time to configure the project.
Enter the name of the project in the Product Name text field. The project name is also the name of the game.
Choose your developer team from the Team menu. If you do not have a development team, choose None.
Enter an organization identifier in the Organization Identifier text field. The organization identifier takes the following form:
com.CompanyName
If you don’t have a company name, replace CompanyName
with your name.
Choose Swift from the Language menu.
Choose SpriteKit from the Game Technology menu.
If you chose the Multiplatform game project, there will be checkboxes to include the following platforms: iOS, watchOS, tvOS, and macOS. I selected iOS and macOS because I don’t have an Apple Watch or an AppleTV. Click the Next button.
The final step is to choose a location to save the project on your Mac. Click the Create button to finish creating the project.
In the save panel you should see a checkbox to create a git repository for the project. This is a matter of personal preference. If you clicked the Create button and want to create a git repository, choose Source Control > New Git Repositories to create a git repository for the project.
The project navigator on the left side of the project window shows the project’s contents. The following image shows the project navigator for a Multiplatform SpriteKit game with iOS and Mac app targets:
You can see there’s a Shared folder that has the files for both the iOS and Mac versions of the project. Beneath the Shared folder are folders for the iOS version and the Mac version.
The two files you are going to use in this article are the two GameScene files.
The deployment target is the earliest operating system version (iOS, macOS, tvOS, or watchOS) that can run the game. Xcode defaults to using the version of the SDK that comes with the version of Xcode you’re using. Apple hasn’t added anything to SpriteKit since iOS 11 so you can set the iOS deployment target to iOS 11 and the macOS target to macOS 10.13 without doing any damage.
Select the project file from the project navigator to open the project editor. Select the project from the project editor. Use the combo boxes to set the deployment target.
Let’s run the project. If you created a Multiplatform game project, you must choose which version of the project to run. At the top of the project window above the editor is a path control with two sections.
The left section has the name of the project followed by the platform, macOS in the screenshot. Click that section to choose the version to run. Connect an iOS device to your Mac and choose the device from the jump bar to run the project on a device. Otherwise choose a simulator from the jump bar to run the project in the iOS simulator.
Click the Run (Play) button in the project window toolbar to run the project. A Hello, World label appears on the screen. Clicking in the window creates a spinning object. Dragging creates a line of spinning objects.
You’re not going to need the label or the spinning objects in your game. So the next step is to remove the label and the spinning objects.
Select the GameScene.sks
file to open the scene editor. Select the label from the canvas or the object list next to the canvas. Press the Delete key to remove the label from the scene.
Now there’s a lot of code to remove from the GameScene.swift
file. You must remove all the code that accesses the label and the spinning nodes. Start by removing all the code from the setUpScene
function so it looks like the following:
func setUpScene() {
}
Next, remove the makeSpinny
function.
Next, edit the touchesBegan
, touchesMoved
, touchesEnded
, and touchesCancelled
functions so they look like the following:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
}
}
If you have a Mac app target, remove the code inside the mouseDown
, mouseDragged
and mouseUp
functions.
override func mouseDown(with event: NSEvent) {
}
override func mouseDragged(with event: NSEvent) {
}
override func mouseUp(with event: NSEvent) {
}
Click the Run button to make sure there are no build errors. The project should run and show a blank window.
Now you’re ready to start adding your own stuff to the project.
When you create a SpriteKit game project, Xcode includes a scene. But most games require more than one scene so you’ll be adding files to your project.
Choose File > New > File to add a new file to your project. The files you will add to SpriteKit projects are in the Source and Resource sections of the New File Assistant. The source code files you will create are Swift files and Cocoa/Cocoa Touch class files. You would create a Cocoa/Cocoa Touch class file when creating a subclass of a SpriteKit class.
The Resource section has the following files for SpriteKit projects:
- Asset catalog
- SpriteKit action
- SpriteKit particle file
- SpriteKit scene
- SpriteKit tile set
If you created a Multiplatform game project, make sure you add the files to all of the project’s targets.
To add files you’ve already created to a project, choose File > Add Files to ProjectName. An Open panel opens. Navigate to where the files are, select them, and click the Add button.
If you add a file of folders to the project, you have the option of creating groups or folder references. Create a group if you need to edit the files in Xcode. If you are adding a folder of source code files, create a group.
Create a folder reference if you don’t need to edit the files in Xcode. If you are adding a folder of sound or image files, create a folder reference.