Tuesday, 6 May 2014

Testing a Two-Player Game [Networking, The Nerdy Kind]

Please dont throw this book across the room if I tell you theres one more step before you can test the two-player version of SunTouch, but theres one more step before you can test the two-player version of SunTouch.

You’ll first need to get SunTouch running in two iOS devices. Plug both of your iOS devices into your Mac. In Xcode, set the schemes target to the first device and click the Run button, as you’ve done countless
times in thisbook so far. While the first app is still running, change the target of the scheme to the second iOS device, and click the Run button again. Now Xcode is running the same app in two iOS devices simultaneously.

The last step is to create a second sandbox player.
To play a two-player game, you must have two Game Center player accounts, and since both apps are using the sandbox servers, both players must be sandbox players.
Onyour second iOS device, follow the same steps in the “Creating a Test Player” section that you did for the single 
version. Once you have two sandbox player accounts, you can start a two-player game.

With both apps running, tap the “two player” button on both. Both devices will present the matchmaker view controller, as shown in Figure 14-21. Tap the Play Now button on both. This uses Game Centers “auto-match”feature, which connects to the first local player it can locate.


Figure 14-21. Connecting with a second SunTouch player

As soon as both devices have connected, SunTouch is off and running, as shown in Figure 14-22.


Figure 14-22.  Two-player SunTouch communicating  over local Wi-Fi
When you’re done playing, you can stop the apps in Xcode. If you like, unplug the devices and re-launch SunTouch from the springboard.

Advanced Networking

GameKit is a fantastic resource for peer-to peer networking, but its not the only network communications solution available—just the easiest to use.

If you want to create a more general networking solution, possibly connecting and communicating with a custom application running on almost any kind of computer, there are lots of resources and possible solutions. The bestplace to begin
is the Network Overview document that you’ll find in Xcodes Documentation and API Reference window. The three areas of network communication that you most likely want to explore are:

n     The high-level HTTP/URL services for communicating with Internet servers, like those you used in Shorty. These include the NSURLRequest and NSURLConnection classes.

n     The low-level TCP/IP socket APIs for direct connection with almost any networkedevice or service. Start with the Using Sockets and Socket Streams document.

n     The Bonjour service for advertising and discovering local services. If you wanted to perform your own matchmaking, so your users could effortlessly connect to another local computer, Bonjour is the tool ofchoice. (GameKit uses Bonjour.)
On iOS, the Bonjour service also supports Bluetooth, for wireless peer-to-peer Bluetooth discovery. Start with the Bonjour Overview document.


One Last Detail

Theres one aspect (no pun intended) that bothers me about SunTouch. The project settings allow SunTouch to run in portrait or landscape mode on both iPhones and iPads. While there are numerous cosmetic issues that
I’d want to address before declaring this app ready to release,theres one rather glaring problem: If the player starts to play the game in one orientation, and then turns to another, the games gets sort of wonky.
This is a side effect of the unit-space coordinate system the game engine uses. It results in peculiar behavior, like hidden suns now in areas of the screen that have already been blasted.

The project settings determine the allowed orientations for your entire app. Individual view controllers can also stipulate which orientations they support, and they can do so dynamicallyYou’re going to exploit this lastfeature to put a stop to users flipping their devices mid-game.

The set of orientations a view controller is willing to work in is declared in the bits returned by its
-supportedIntefaceOrientations method. iOS queries this when presenting a view controller. If the view controller doesnt support the current orientation, the orientation of the interface is changed
to one that does. Your STFlipsideViewController could benefit from this feature. The game instructions are laid out vertically, and are unsightly when presented in landscape orientation.

Change STFlipsideViewController so that the view only appears in portrait orientation. Find the STFlipsideViewController.m implementation file and add this method (or review the finished project in the Learn  iOS Development Projects  Ch 14  SunTouch-5 folder):

-  (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

This overrides the inherited -supportedInterfaceOrientations, which returnUIInterfaceOrientationMaskAll on the iPad and UIInterfaceOrientationMaskAllButUpsideDown on the iPhone. If you run the app, rotate the device, andpresent the flipside view controller, it still appears in portrait orientation, as shown in Figure 14-23, because thats the only orientation it supports now.


Figure 14-23. Limiting a view controller to portrait orientation

If your view controller supports a combination of orientations, either return one of the combinatioconstants (UIInterfaceOrientationMaskAll) or construct a set of orientations by ORing individual onetogether(UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown).

For the STGameViewController, the problem is a little different. You want to allow landscape, or even upside down, orientations but you dont want the orientation to change once the game begins. The solution is to create a“smart” -supportedInterfaceOrientations method that only supports the orientation it started out with.

In the STGameViewController.h interface file, add a lockedOrientation property to the class:

@property  (nonatomic) UIInterfaceOrientation lockedOrientation;

Switch to the STMainViewController.m implementation file and find the -prepareForSegue:sender: method. In the if block that segues to the STGameViewController, add a line that captures the current orientation of the device (newcode in bold):

gameViewController.twoPlayer = [segue.identifier  isEqualToString:@"twoPlayer"];
gameViewController.lockedOrientation = self.interfaceOrientation;
}

Finally, switch to the STGameViewController.m file and override the -supportedInterfaceOrientation
method:

-  (NSUInteger)supportedInterfaceOrientations
{
switch (self.lockedOrientation)
{
case   UIInterfaceOrientationPortrait:
return  UIInterfaceOrientationMaskPortrait; case   UIInterfaceOrientationPortraitUpsideDown:
return  UIInterfaceOrientationMaskPortraitUpsideDown; case   UIInterfaceOrientationLandscapeLeft:
return  UIInterfaceOrientationMaskLandscapeLeft; case   UIInterfaceOrientationLandscapeRight:
return  UIInterfaceOrientationMaskLandscapeRight;
}
return  UIInterfaceOrientationMaskAll;
}

The new method allows only the orientation that matches the one set in lockedOrientation. Now you can start a SunTouch game in any orientation, but once started it wont respond to changes to the devices orientation untilthe game ends. Wasnt that simple?


Summary

In this chapter you covered a lot of diverse ground. You created a Game Center–aware application, assigned it a unique app ID, registered that ID and your app with Apple, used iTunes Connect to enable and configure GameCenter for your app, implemented the various GameKit requirements, created a sandbox player, got the local player in your app, and reported scores to the worldwide leaderboard.

And all of that was just the prelude to adding real-time network communications to your app! You used the matchmaking feature of Game Center to connect with a second iOS device, sent live status updates to the other player,and processed remote messages received from the other player—all
in real-time. You also learned some basics about serializing information, and constructing and interpreting inter-process data.

This is cause for some celebration and well-deserved congratulations. This was, by far, the most complex and difficult project in the book, and you made it through with flying colors. With the momentum you’ve built up, youcan, honestly, coast through the rest of this book. Later chapters are going to introduce you to even more iOS services,
like maps, and theres a lot of practical information about Objective-C and multi-tasking, but all of that is going to seem simple compared to what you’ve accomplished so far.

Speaking of practical information, the next chapter is going to focus on Interface Builder. Not so much how to use it, as how it works; something thats important to understand if you want
to be an iOS master developer.

No comments:

Post a Comment