Game Center Tutorial: How To Make A Simple Multiplayer Game with Sprite Kit: Part 2/2

Learn how to make a simple multiplayer racing game with Sprite Kit in this Game Center tutorial! By Ali Hafizji.

Leave a rating/review
Save for later

Create a simple multiplayer game with Game Center and SpriteKit!

Create a simple multiplayer game with Game Center and SpriteKit!

Update 3/1/14: Updated for iOS 7 and Sprite Kit.

This is the second part of a two part Game Center tutorial series on creating a simple networked game with Game Center matchmaking.

In the first part of the Game Center tutorial series, you learned how to enable Game Center for your app and find matches with other players using the built-in GKMatchmakerViewController.

In this second and final part of the Game Center tutorial series, you’ll learn how to look up player aliases and how to send and receive data between devices.

In the end, you’ll have a fully functional (but simple) networked game that uses SpriteKit and Game Center that you can play with your friends!

Start by downloading the starter project for part 2 of this tutorial. Even though this tutorial is a continuation of the previous Game Center tutorial, I’ve built a slightly modified version of the final project of the last tutorial. Don’t worry, there are hardly any changes – just a few small things to keep the focus of the tutorial squarely on Game Center. I’ll describe the changes in more detail in the next section.

Multiplayer Starter Project

First things first – as soon as you open the starter project, open your target settings and update your Bundle Identifier to whatever you set last time. This is important, so it might be a good idea to open your old project, and double check it matches exactly. Then restart Xcode so the changes are sure to be applied.

Next, let’s take a moment and go through some of the changes I made to the project from the previous tutorial.

  • Open GameViewController.m: you will notice that this class no longer implements the GameKitHelperDelegate protocol.
  • In the GameKit group you will notice a new class i.e. MultiplayerNetworking. This class will contain all the networking code for the game. It will notify the game of important events through a delegate.
  • GameScene now implements the MultiplayerNetworkingProtocol.
  • Since the MultiplayerNetworking class is responsible for managing all networking activity, naturally it will implement the GameKitHelperDelegate protocol.

Those are all the changes to the final project of the last tutorial. Before you proceed make sure that you go through the code a couple of times, so that you’re comfortable with the changes.

Finally, run the project on two devices and tap “Play Now” to find a match. Verify that everything works as before, and that it finds a match and displays this in the console:

CatRaceStarter[3712:60b] Ready to start match!

Network Code Strategy: Picking Players

Let’s talk about the strategy you’re going to take with the network code, starting with how to pick players.

You have two players in this game: player 1 (the dog) and player 2 (the kid). The problem is, how do you decide who should be the dog and which the kid?

The strategy you’re going to take is each side will generate a random number on startup, and send it to the other side. Whichever side has the larger random number will be player 1, and the other side will be player 2.

In the rare event each side generates the same random number, you’ll just try again.

Whichever player is player 1 will get some special “privileges.” First, that side signals the game should start by sending a message to the other side. That side also is the one who is responsible for checking when the game ends, and sending a message to the other side when the game is over (and who won).

In other words, “player 1” takes the role of the “server”. He’s the final authority on what goes on for this game.

Network Code Strategy: Player Aliases

Since it’s random which player is the dog and which is the kid, you need to have some way of showing the player which one he is.

For this game, you’ll just print the player’s alias right on top of the character they are, so they can tell who they are.

If you don’t know what a player alias is, it’s the nickname that you set up for your account in Game Center. When a match is made, you don’t get these automatically – you have to call a method so Game Center will send them to you.

Network Code Strategy: Game States

One of the challenges with making networked code is that things can happen in different orders than you might expect.

For example, one side may finish initializing the match and send the random number to the other side before the other side even finishes initializing the match!

So if you’re not careful, you can get into strange timing issues in your code that causes problems. One good way to keep things clean is to keep careful track of what state each side of the game is currently in.

The following diagram illustrates the states Cat Race will need:

Game states for this simple networked game

Let’s go through each of these:

  • Waiting for Match: The game is waiting for a match to be connected, and for the player aliases to be looked up. When both of these are complete, it checks to see if it’s received the random number from the other side yet, and advances to the next state appropriately.
  • Waiting for Random #: The game has a match and has the player aliases, but is still waiting to receive a random number from the other side.
  • Waiting for Start: The game is waiting for the other side to start the game (this occurs when the local player is player 2).
  • Active: The game is currently playing – no winner has been found yet. Every time a player moves, they will tell the other side that they’ve moved by sending a message.
  • Done: The game is over (player 1 has sent a message to player 2 to tell him that the game is over). No more messages are sent and the player can then restart the game.

OK now that you have a plan in mind, let’s move onto the first step, which is retrieving the player aliases!

Ali Hafizji


Ali Hafizji


Over 300 content creators. Join our team.