Displaying Game Center User Interface Elements

Game Center provides two distinct types of user interface elements for you to use in your game. The first type is intended to be displayed modally by your game, covering your own game’s user interface and temporarily interrupting the normal flow of your game. Typically these user interface screens allow a player to interact with content loaded from Game Center or to perform Game Center tasks. When the player finishes interacting with one of these screens of content, your game shows the next appropriate screen, either by returning to one of your game screens or by advancing to another screenful of content.

In iOS, a fullscreen user interface is packaged as a view controller, and follows the standard conventions of view controllers on the system. One of your game’s view controllers is expected to present these view controllers when needed, and later respond when the view controller is dismissed. On OS X, a special class provided by Game Kit provides a similar infrastructure so that your game can present the user interface. Other chapters in this document show the iOS code for presenting a view controller; this chapter shows you how to use the GKDialogController class to present one of these view controller on OS X.

The second type of user interface element is a banner that is displayed for a short time to the player. Afterwards, the banner automatically disappears from the screen. While players can interact with some banners, usually banners are simply used to display a message to the player. Game Center displays many banners on behalf of your game, but you can also present your own banners to the player if your game has information you need to display.

Game Center UI Is Displayed by Your View Controller (iOS)

The convention used by Game Kit is for one of your view controllers to present the Game Kit view controller. Your view controller acts as a delegate to the view controller it presents so that the view controller can be informed when the player is finished looking at the presented screen.

Listing 2-1 shows most the common use of this pattern, which is to show the Game Center user interface. The Game Center view controller displays many different pieces of Game Center content, so most games should offer a button that brings the player to this screen, even if the game also shows Game Center content using a custom user interface.

Listing 2-1  Displaying the Game Center user interface

- (void) showGameCenter
{
    GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
    if (gameCenterController != nil)
    {
       gameCenterController.gameCenterDelegate = self;
       [self presentViewController: gameCenterController animated: YES completion:nil];
    }
}

Listing 2-2 shows a typical implementation of the delegate, which simply dismisses the presented view controller.

Listing 2-2  Responding when the player finishes viewing the Game Center content

- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

In most cases, your game would pause gameplay or other real-time behavior when displaying one of these standard user interface classes. When the view controller is later dismissed, it can resume these activities.

Game Center UI Is Displayed by Your Window (OS X)

In OS X, view controllers do not play the same role as they do in iOS. In many cases, some other object responsible for your user interface displays the Game Center control. It does this through the use of the GKDialogController class. This class presents the user interface in a window provided by your game.

Listing 2-3 shows how the code in Listing 2-1 is modified to display in OS X. This code loads the singleton dialog controller and points the dialog controller at the game’s window. Then it presents the view controller. The view controller is instantiated and configured exactly as it was on iOS.

Listing 2-3  Displaying the Game Center user interface in OS X

- (void) showGameCenter
{
    GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
    if (gameCenterController != nil)
    {
       GKDialogController *sdc = [GKDialogController sharedDialogController];
       sdc.parentWindow = myWindow;
       [sdc presentViewController: gameCenterController];
    }
}

Listing 2-4 shows how the delegate changes.

Listing 2-4  Responding when the player dismisses the Game Center content

- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
    GKDialogController *sdc = [GKDialogController sharedDialogController];
    [sdc dismiss: self];
}

Displaying Notification Banners

Listing 2-5 shows how to display a notification banner in your game. It constructs a title and message using private methods and then displays the banner. When the banner disappears from the screen, the completion block is called and the code advances to some other part of the game’s user interface.

Listing 2-5  Displaying a notification banner

- (void) showBanner
{
    NSString* title = [self generateTitle];
    NSString* message = [self generateBannerMessage];
    [GKNotificationBanner showBannerWithTitle: title message: message
        completionHandler:^{
        [self advanceToNextInterfaceScreen]
     }];
}