Online Book Reader

Home Category

iPhone Game Development - Chris Craft [95]

By Root 1561 0
chapter we listed the available connection options:

Head-to-head

Peer-to-peer

Internet

Massive multiplayer

For our example we are going to support head-to-head and peer-to-peer.

Head-to-head does not really require a connection and therefore is fairly straightforward to implement. However, do not dismiss the importance it carries. Most players will have only one device to play on, so this style may make up the bulk of your market.

At the time of this writing, peer-to-peer is the new kid on the block. Peer-to-peer provides a great gaming experience for those with two compatible devices.

Note

Peer-to-peer support requires iPhone OS 3.0 and is only available for the iPod touch second generation, iPhone 3G, and above.

Internet and massive multiplayer connections would be an awesome addition to the game, but the topic is too broad to cover here, especially connecting a real-time game this way.

Hooking Up with Your Peers

Now we are going to walk you through the basics of peer-to-peer connections. In the next section we will take you through the code for AmuckPuck, but for now we need a smaller example that is not cluttered with concerns outside of peer-to-peer. At the time of this writing, a simple example of peer-to-peer was not available from Apple. We have supplied the example “P2P Chat,” which you can download from http://appsamuckcom/p2pchat.

Running and reviewing P2P Chat

Peer-to-peer is at the core of the Game Kit framework introduced in the iPhone 3.0 SDK. Apple has provided documentation and getting started videos that outline the features and uses of tools available in Game Kit. Also, Game Kit was new at the time of this writing, so we recommend that you read through the material and watch the videos Apple provides to see what may have been added since this book was written.

Load the project in Xcode and run it in the Simulator so you can get your first experience with the app. To get the full experience, you will need an additional device to connect, but thankfully Apple was kind enough to enable the Simulator to connect with an external device as two physical devices would.

Tip

Remember that the Simulator is just that—a simulator, not an emulator. There is a difference between the two. The Simulator utilizes the processor and other resources directly. An actual device will not perform at the same level. On a physical device, graphics will be rendered more slowly and packets will take longer to arrive. The Simulator is a great way to test, debug, and tune; however, you should always perform testing on a physical device (or two) before you submit your app to the App Store.

Once you have loaded the project, click Build and Go. You will see a simple screen in text view with a Find button, a text field, and a Speak button (Figure 7.5).

Click the Find button to launch the peer picker (Figure 7.6). The peer picker is a nice controller that Apple has provided for you in the SDK. It takes care of all the logic and workflow necessary to connect two players in a peer-to-peer session.

FIGURE 7.5

The main screen of P2P Chat


FIGURE 7.6

The peer picker's select connection type screen


The first screen in the peer picker workflow allows the player to select the connection type. Currently, if a user selects Online, the peer picker informs you of this decision and it is up to you to complete the connection. P2P Chat does nothing but close the picker since it is just an example. However, if the user clicks Nearby, this tells the picker that the player wants to connect over Bluetooth using peer-to-peer. If Bluetooth is not enabled, the player will be given an opportunity to turn it on (Figure 7.7).

FIGURE 7.7

The Bluetooth prompt of the peer picker


If Bluetooth is already on, you will not receive this prompt. If you really want to see it anyway, you can force it to prompt you by going to the Settings application on your device and turning off Bluetooth (Figure 7.8). Then try P2P Chat again, and you will be prompted to turn it on.

FIGURE 7.8

Turning off Bluetooth in device settings


One way

Return Main Page Previous Page Next Page

®Online Book Reader