Beginning Turn-Based Gaming with iOS 5 Part 1
Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, check out iOS 5 by Tutorials Second Edition!
In iOS5, Game Center has a new API that makes it even easier to create another type of game: turn-based games!
This new API is perfect for board games, turn-based strategy, word games, and other types of casual turn-based games. You can take a turn in your game, wait for your friend to take his turn, and then get notified when it’s your turn again!
In this tutorial we will build a simple UIKit based game called “Spinning Yarn”, where you take turns writing a story together with a friend.
This is a two part tutorial series. In the this first part of the series, we are going to focus on getting the basic project and matchmaking working. In the next part of the series, we’ll wrap up sending turns and complete our simple turn-based game!
Note that to get the most out of this tutorial, you will need two different devices for testing and two separate Game Center sandbox accounts. I know this is annoying for testing, but it’s an unfortunate fact of life for Game Center development!
And now it’s your turn: keep reading to make a cool turn-based game!
Turn-Based Gaming Overview
In a turn-based game, only one player can affect the game state at a time. This player will hold the baton, or the game state. They will take a turn, which will change the game state, then pass the turn on with the new game state.
However, other players will have ability to observe matches in between their turns. Because the games, or matches, don’t require our constant attention, but can be visited, played, and the we can do other things while we wait, we can be in the middle of many games at once. Because of this, in our game we’ll give the players the ability to switch between the matches they’re currently in.
Here are the major classes in the Turn-Based Gaming API, which we’ll be covering in detail in this tutorial:
- GKTurnBasedMatch: Contains all the information and game state for a match. We’ll use this object to encapsulate the match players, the match game state, and the important information about who holds the turn and who’s still playing.
- GKTurnBasedMatchmakerViewController: This will be our primary UI element for interacting with matches, switching between matches, and creating new matches. We’ll use it as our command center.
- GKTurnBasedEventHandlerDelegate: We will make a helper class implement this protocol, so we can get notifications when the turn moves from player to player, when we are invited to a new match, or when the game ends.
Note that the Game Center app will automatically keep track of who we are playing with and other data about our game, so users can always switch to Game Center to see their game status.
The SpinningYarn Game
If you harken back to your days in grade school, you may remember a writing exercise where each child started writing a story. After a certain period, you passed your paper to your neighbor and he gave you his. You read what he wrote and continued the story. This continued back and forth until you ended up with a story, often with hilarious results!
This is the turn-based game we’ll be making in this tutorial. I’ve made a starter project that contains the UI already made, so we can keep the focus mainly on the Turn-Based Gaming APIs.
So go ahead and download the resources for this tutorial, and you’ll find a starter project inside. Open it up in Xcode and run it. You should see the following UI:
Right now this project just contains a text view displaying some sample text, and a text field at the bottom of the screen that you can enter text into. Note it doesn’t actually do anything yet – it’s just placeholder code for now. The app also has a pretty paper background (thanks to playingwithbrushes on Flickr!)
Feel free to look at ViewController.xib to see how the UI is arranged, and take a look at ViewController.m to see the code (it’s very simple at this point). We’ll convert the project into a fully functional game from here!
Setting up Game Center
Before you can start writing any Game Center code, you need to do two things:
- Create and set an App ID
- Register your app in iTunes Connect
Let’s go through each of these in turn.
Create and Set an App ID
The first step is to create and set an App ID. To do this, log onto the iOS Dev Center, and from there log onto the iOS Provisioning Portal.
From there, select the App IDs tab, and create a new App ID for your app, similar to the following (except you’ll be choosing different values):
The most important part is the Bundle Identifier – you need to set this to a unique string (so it can’t be the same as the one I used!). It’s usually good practice to use a domain name you control followed by a unique string to avoid name collisions.
Once you’re done, click Submit. Then open the SpinningYarn Xcode project, select the spinningyarn target, and in the Summary tab set your Bundle identifier to whatever you entered in the iOS Provisioning portal, as shown below (except you’ll be entering a different value):
One last thing. Xcode sometimes gets confused when you change your bundle identifier mid-project, so to make sure everything’s dandy take the following steps:
- Delete any copies of the app currently on your simulator or device
- Quit your simulator if it’s running
- Do a clean build with Project\Clean
Congrats – now you have an App ID for your app, and your app is set up to use it! Next you can register your app with iTunes Connect and enable Game Center.
Register your app in iTunes Connect
The next step is to log on to iTunes Connect and create a new entry for your app.
Once you’re logged onto iTunes Connect, select Manage Your Applications, and then click the blue “Add New App” button in the upper left.
On the first screen, enter Spinning Yarn for the App Name, 311 for SKU Number, and select the bundle ID you created earlier, similar to the screenshot below:
Click continue, and follow the prompts to set up some basic information about your app. Note you will probably have to change the name, since I’ve already taken the Spinning Yarn name :]
Don’t worry about the exact values to put in, since it doesn’t really matter and you can change any of this later – you just need to put something (including a dummy icon and screenshot) in to make iTunes Connect happy.
When you’re done, click Save, and if all works well you should be in the “Prepare for Upload” stage and will see a screen like this:
Click the blue “Manage Game Center” button to the upper right, click the big blue “Enable” button, and click “Done.” That’s it – Game Center is enabled for your app, and you’re ready to write some code!
“PROBLEM SOLVED! There is a second place you need to enable Game Center in iTunes Connect. Click “View Details” under your app’s icon (under “Versions”) and enable Game Center there too. This should probably be added to the chapter/tutorial.
Here’s the thing though: I had done that already! Somehow, iTunes Connect turned it back off all on its own. At least now I know what to look for should it happen again.”
By the way – inside the “Manage Game Center” section, you might have noticed some options to set up Leaderboards or Achievements. We won’t be using those in this book, but if you ever need them that’s where they are!