- Xcode Spritekit Game Tutorial
- Xcode Game Tutorial Online
- Xcode Game Center Tutorial
- Xcode Game Tutorial Free
- Xcode 3d Game Tutorial
- Xcode Game Tutorial Download
In Xcode, a texture atlas looks like any other folder, with a name that ends in.atlas. The special name tells Xcode to pack the images into a texture atlas when it builds the game, dramatically improving performance. To learn more about texture atlases, check out our SpriteKit Animations and Texture Atlases tutorial. Home » App Development » Create an iOS Game with Swift and Xcode. Create an iOS Game with Swift and Xcode Written by Reinder de Vries on May 7 2020 in App Development, iOS. Let’s build a fun iOS game with Swift and Xcode! Fire up your Xcode, get your Swift hat on and get hacking with this fun game called Add 1. In this tutorial, you. Creating the App. Okay, let’s get started and build the game. Open Xcode and create a new Project, choose the template Single View Application as shown below: Creating Simple Maze Game. In the next screen enter Maze as the product name and set com.appcoda in the company identifier field. Xcode’s iOS game template comes with a Metal option, but you won’t choose that here. This is because you’re going to put together a Metal app almost from scratch, so you can understand every step of the process. Download the files that you need for this tutorial using the Download Materials button at. Xcode only runs on macOS, which means you need a Mac if you want to build iOS apps with Xcode. Xcode won’t run on iPad. A few alternatives for Windows/PC are available, but they’re far from ideal. The next step in this tutorial, is installing Xcode. Let’s get to it!
You can use playgrounds in Xcode to quickly write some Swift code, experiment with new Swift syntax, or work on your Swift algorithms skills. Most of all, playgrounds in Xcode are a great way to learn Swift programming.
In this tutorial, you’ll learn how to get started with playgrounds in Xcode. I’ll give you a brief overview of Xcode playgrounds, so you can quickly get started with Swift programming.
Prerequisites for this tutorial:
- A Mac with macOS and Xcode 8 or higher (Xcode 11 is recommended)
- A bit of free time, to play around with code
Ready? Let’s go.
Getting Started with Playgrounds
Let’s get started with playgrounds in Xcode. Here’s what you do:
Xcode Spritekit Game Tutorial
- Open Xcode on your Mac
- When the Welcome to Xcode window appears, click on Get started with a playground
If you’re not seeing the Welcome to Xcode window, then simply create a new playground by choosing File -> New -> Playground...
.
The following window appears:
Here’s what you do next:
- Select the Blank template, and make sure that iOS is selected, then click Next
- Give your playground a name, like
SwiftExample
, and save it in a convenient folder, then click Create
You now see this:
You can create four types of playgrounds: a blank Swift file, a Game playground that uses SpriteKit
, a map-based playground that uses MapKit
, a playground with SwiftUI, and a View Controller playground that uses UIKit
. It’s easiest to start with a Blank playground.
Let’s do a quick tour of this playground in Xcode. A few things stand out:
- At the top-left of the window you see some Swift code, in the editor. This editor is where you’ll do most of your programming work.
- At the bottom-left of the window you see the Console (or Debug Area). This is where the output of your code appears.
- On top of the Console you see two buttons: the left one is to show or hide the Debug Area, and the right one (looks like a Play button) is to execute the playground.
- At the right of Xcode you see a sidebar. When your code has run, you (usually) see intermediate results here. For instance, you can inspect the value of variables in your code. The Live View also appears here, where you can see the visual results of your code.
- At the top of Xcode you see a status bar. This simply indicates that your code runs OK, or that it contains errors. And at the top-right of the screen you see a few buttons. You can show and hide some of Xcode’s panes and inspectors with it.
OK, now go ahead and run the Swift code in the editor. Simply click the Run button at the bottom-right of the playground, and wait for execution to complete.
Xcode now compiles your code, and shows you its result. The default code doesn’t output anything to the Console, but you should be able to see the value of the variable str
in the sidebar on the right of Xcode.
Run Your Code in Xcode
OK, let’s play with some more interesting Swift code in the playground. Here’s how you (manually) reverse an array in Swift:
Replace the code in the playground with the above code. Make sure you also include the line import Foundation
at the top of the file.
Now run the code by clicking the Play button! The following output appears in the Console:
See how the order of the names in the names
array has been reversed? Here’s how that works:
- First, we’re create an array called
names
and fill it with a bunch of strings. We’re also creating an empty array calledreversedNames
. - Then, we’re coding a for loop for the range
1...names.count
. This loop essentially iterates over every string in the array. - Then inside the loop, we’re getting a name from
names
with the codenames[names.count - i]
, and append it to thereversedNames
array. By usingnames.count - i
we’re essentially starting at the end of the array, going back-to-front. - Finally, we’re printing out the items in the
reversedNames
array. This output ends up in the Console.
Awesome!
So, how does that for loop really work?
- First, note that the length of the
names
array is8
. This is the value ofnames.count
. - The for loop runs 8 times, increasing
i
every time it runs, soi
starts at1
and stops after8
(1 to 8 inclusive). - Inside the loop, we use subscript syntax to get an item from the
names
array by its index number. This index isnames.count - i
. - So, for every iteration we’re subtracting
i
fromnames.count
. This value essentially goes from7
to0
. - Because we’re adding the last item from
names
as the first item inreversedNames
– and so forth – thenames
array gets reversed.
We’re reversing the array manually here, for fun and for learning purposes. When you really need to reverse an array, simply use array.reverse()
or array.reversed()
.
See Runtime Values in a Playground
As your code runs, you can inspect the values of variables and constants in the playground. For example, you can check out the different values of names
and reversedNames
in Xcode’s sidebar.
You can even see the different values of names[names.count - i]
inside the for loop. Simply double-click on the small eye icon in the sidebar, next to where it says “8 times”. A gizmo with the different values shows up. (This only works in a playground!)
You can inspect most values with the sidebar:
- Mouse over an item and double-click the eye icon to Quick Look
- Double-click the rectangular button to Show Result inline in your code
Another way is to debug your code is to simply use print()
. Add this line inside the for loop:
When you run the playground again, you see how the items are added in reverse order:
OK, one last thing… Hold the Command
key while clicking on one of the variables in your code. A tiny menu pops up. This works in Xcode, as well as in a playground. You can choose from a few options:
- Jump to Definition will jump to where that variable was first defined in your code
- Show Quick Help will show you information and code documentation about a symbol (variable, function, etc.)
- Callers helps you identify the bits of code that call a particular function
In a playground, you can even run your code up to a particular line. Mouse over to the “gutter”, the line numbers on the left of the editor. A blue play button appears when you hover over a line number. Clicking the Play button on that line will run your code up to that point. That’s super convenient to run your code line-by-line, and see intermediary results!
Further Reading
Playgrounds in Xcode isn’t just helpful to play around with Swift code, but its tools also make understanding your code easier. You can inspect the values of variables as your code runs, and find out what every line of code does.
Want to play with Swift code in Xcode Playgrounds? Check these tutorials:
Want to learn more? Check out these resources:
Getting started
In order to better understand the concepts behind Scene Kit, you can draw some mental parallels between a Scene Kit scene and a typical Hollywood movie scene.
- A movie scene has basic components such as lights, cameras and objects used to build the sets, as well as actors and actions for the actors to perform when the director shouts 'Lights! Camera! Action!'
- When you build a Scene Kit scene from scratch, you'll add the same types of components as you build up your project. Scene Kit organizes these components into a node-based hierarchy known as the scene graph. A scene starts with a root node that defines the coordinate system; you add content nodes underneath the root node to form a tree structure. These content nodes are your basic building blocks for the scene and can include elements such as lights, cameras, geometry or particle emitters.
Consider the image below which shows a typical scene in Scene Kit:
Note the node-based hierarchical structure on the left; it serves as a good example of the tree-like scene graph you'd construct within Scene Kit. This particular screenshot is of Xcode's built-in Scene Kit editor; hopefully this whets your appetite for things to come! :]
Working with your game project
Now that you've covered a few basic concepts of Scene Kit, you'll learn best if you dive right in and create your first Scene Kit project. The sections below will take you through the process of creating a project using Xcode's built-in project template.
Creating your Scene Kit game project
Open up Xcode and select FileNewProject from the main menu. If you want to become an Xcode ninja, use the shortcut command: ⇧⌘N.
Select the iOSApplicationGame template and click Next to continue:
Now you need to provide some basic details about your project. Enter GeometryFighter for the Product Name, select Swift for Language, SceneKit for Game Technology, Universal for Devices, uncheck the unit tests and click Next:
The final step is to choose a convenient location to save your project. Pick a directory and select Create; Xcode will work its magic and generate your project.
Building your Scene Kit game project
Now that you've generated your Scene Kit game project from the template, you'd probably like to see it in action! :]
First, choose the iPhone 6 simulator from the toolbar, then press the Play button at the top to build and run your project. Xcode ninjas can simply press ⌘R:
You'll see the simulator spin up, and your first 3D Scene Kit game will appear. You can rotate the view of your 3D spaceship in the game. Simply drag around the screen in different directions to change the camera angle:
Cool! It's okay to take a moment and do a little happy dance in your seat, then continue on with the rest of the chapter.
Challenge - deciphering supporting files
It's time for your first mini-challenge! Before you move on, have a read through the game template project. Pay careful attention to the following key files and folders under the project navigator:
• art.scnassets
• ship.scn
• GameViewController.swift
** • Assets.xcassets**
• LaunchScreen.storyboard
You might not understand how everything works at the moment, but try to figure out what you think each file might do at a high level. You'll be cleaning up the project in the next section, so take a look at the files and folders while they're still around. :]
Cleaning up your game project
There are a few components you need to remove in order to start with a clean Scene Kit game project. Don't worry; you'll re-create all the content from scratch so you can learn where it comes from.
Removing unnecessary folders
The first thing to get rid of is the art.scnassets folder. Right click the folder, select Delete and then click Move to Trash:
Cleaning up the main project files
The GameViewController.swift file is a key component of your game; it's where all your game logic and code will live. Before you can start coding, you need to purge all the code the Xcode Scene Kit game template created for you.
Replace the contents of GameViewController.swift with the following:
The old code generated the spaceship; you've replaced that code with an empty slate. shouldAutorotate() handles device rotation and and prefersStatusBarHidden() hides the status bar.
Setting up Scene Kit
Earlier, you learned how Scene Kit uses the scene graph to display content on the screen. The SCNScene class represents a scene; you display the scene onscreen inside an instance of SCNView. Your next task is to set up a scene and its corresponding view in your project.
Xcode Game Tutorial Online
Setting up your project's view
Add the following property to GameViewController.swift, just above viewDidLoad():
Xcode Game Center Tutorial
Here you declare a property for the SCNView that renders the content of the
SCNScene on the display.
Next, add the following function just below prefersStatusBarHidden():
Here, you cast self.view to a SCNView and store it in the scnView property so that you don't have to re-cast it ever time you need to reference the view. Note that the view is already configured as an SCNView in Main.storyboard.
Setting up your project's scene
It's time to set up your scene. Add the following property to GameViewController.swift, just below the scnView property:
Here you declare a property for the SCNScene in your game. You will add components like lights, camera, geometry, or particle emitters as children of this scene.
Now add the following function below setupView():
This code creates a new blank instance of SCNScene and stores it in scnScene; it then sets this blank scene as the one for scnView to use.
Adding finishing touches
Now that you've created functions to set up instances of SCNView and SCNScene, you'll need to call them from somewhere during the initialization step. A good place to do that is just after the view finishes loading.
Add the following lines to viewDidLoad(), just after the call to super:
Xcode Game Tutorial Free
For the final finishing touch, you'll add an app icon to your game. Take a look under the Resources folder; you'll find app icons of various sizes in there for your use.
Xcode 3d Game Tutorial
To set an image as the icon for your game, open the Assets.xcassets folder, select the AppIcon entry and drag each file from the Resources folder to the appropriate spot. Your AppIcon pane should look like the following when you're done:
Build and run your project, and stand in awe of the black screen of opportunity! :]
Xcode Game Tutorial Download
In all fairness, this might not seem very impressive at first, but you've come a long
way already:
- You've created a basic Scene Kit game project that uses built-in Scene Kit game template.
- You also learned how to clean out the project by removing unnecessary folders such as art.scnassets.
- Finally,youlearnedhowtoinitializeanSCNViewwithablankSCNScene.
Now that you have a blank slate to start with, keep reading to get started making
your first SceneKit game!