Adding Files to SpriteKit Playgrounds
February 21st, 2019
Filed under: Game Development
I left one important thing out of my SpriteKit playground article: how to add files to the playground. I wanted to put that material in the article, but there was so much to mention that it warranted its own article.
Keep in mind that Swift playgrounds aren’t designed to handle tons of files. If you find yourself creating dozens of files, you’re better off creating an Xcode project for your game.
Playgrounds Have Limited Support for Creating New Files
If you look at the navigator for a playground, you will see that a playground page has a Sources folder and a Resources folder.
You can add a new Swift file to the playground by control-clicking the Sources folder and choosing New File. Control-clicking the Resources folder and choosing New File creates an empty file. Those are the only options for creating a new file in a playground.
Adding New Files to the Playground
How do you add things like new scenes and image files to your playground? You must perform the following steps:
- Put the playground in its own folder.
- Add the files to the playground folder.
- Add the files from the playground folder to the playground.
Putting the playground in its own folder isn’t mandatory, but doing so makes organizing the files in the playground easier.
Adding Files to the Playground Folder
In Xcode choose File > New > File to create a new file. The New File Assistant opens.
Click the iOS button at the top of the window. Select the type of file you want to create. For SpriteKit games the most common files to create are the following:
- Asset catalog
- Cocoa Touch class
- SpriteKit action
- SpriteKit particle file
- SpriteKit scene
- SpriteKit tile set
Click the Next button. Name the file, navigate to the playground folder, and click the Create button.
If the New File Assistant does not open and a new file appears in the playground, close the playground and reopen it. Swift playgrounds can be flaky.
Add the Created Files to the Playground
Now that you created the file, you can add it to the playground. Select the Resources folder or Sources folder (for source code files), right-click, and choose Add Files to. An Open panel opens. Navigate to the playground folder, select the file you want to add, and click the Add button.
If the file you added does not appear in the playground, close the playground and reopen it.