Game Center Overview

来源:互联网 发布:js prompt 编辑:程序博客网 时间:2024/06/05 17:44

Game Center Overview

This chapter describes Game Center’s features and explains how to get started developing games that support Game Center.


What Is Game Center?

Game Center is a new social gaming network that is available on supported iOS-based devices running iOS 4.1 and later. A social gaming network allows players to share information about the games they are playing as well as to join other players in multiplayer games. Game Center provides these services over both wireless and cellular networks.

The functionality of Game Center is delivered by three interconnected components:

  • The Game Center app is built into iOS 4.1 and later that provides a single place where players can access all of Game Center’s features.
  • The Game Center service is the online service that both your game and the Game Center app connect to. The online service stores data about each player and provides network bridging between devices on different networks.
  • The Game Kit framework provides classes that you use to add support for Game Center to your game.

So what features does Game Center provide?

  • Authentication allows each player to create and manage an online persona, also known as analias. A player uses an alias to authenticate his or her identity on Game Center, to manage a list of friends and to post status messages that are visible to friends.
  • Leaderboards allow your game to post scores to the Game Center service where they can later be viewed by players. Scores are viewed by launching the Game Center app; your game can also display a leaderboard with just a few lines of code. Leaderboards help foster competition between Game Center players.When you add leaderboards to your game, you decide how the game’s scores are interpreted and displayed. For example, you can customize a score so that it appears as time, money, or an arbitrary value (“points”). You can choose to create multiple categories of leaderboards — for example, you might display a different leaderboard for each level of difficulty your game supports. Finally, your game can retrieve the scores stored in the leaderboard.
  • Achievements are another way your game can measure a user’s skills. An achievement is a specific goal that the user can accomplish within your game (for example, “find 10 gold coins” or “capture the flag in less than 30 seconds”). Your game customizes the text that describes an achievement, the number of points a player earns by completing an achievement, and the icon that is displayed for an achievement.As with leaderboards, a player can see their earned achievements within the Game Center app; they can also compare their achievements with those earned by a friend. Your game can display achievements with just a few lines of code, or you can download the achievement descriptions and use them to create a customized achievement screen.
  • Multiplayer allows players interested in playing an online multiplayer game to discover each other and be connected into a match. Depending on your needs, your game can either use Game Center to connect all the participants together or have Game Center deliver a list of players to your game. In the latter case, you would provide your own network implementation inside your game that connects each device to a server you provide.Matchmaking provides both automated matching and invitations. When a user chooses automated matching, your game creates a request that specifies how many players should be invited into the match, and what types of players are eligible to join the match. Game Center then finds players that meet these criteria and adds them to the match. Invitations allow a player to invite their friends into a match, either from within your game or from within the Game Center app. When a friend is invited, the friend receives a push notification that allows him or her to launch your game and join the match. A push notification can be sent to a player even if the player does not have your game installed on the device. When this happens, the player can launch the App Store directly from the notification.
  • Peer-to-peer networking is provided by the matchmaking service and provides a simple interface to send data and voice information to other participants in a match. Your game can create multiple voice channels, each with a different subset of the players in the match. For example, a game with multiple teams could create separate channels for each team allowing them to converse without allowing non-team members to listen in.

Supporting Game Center increases the visibility of your game. Players can see the games that their friends are playing, or invite friends into a match, giving those friends an opportunity to immediately purchase your game. By connecting game players to each other, you allow news to quickly spread about your game.

Essential Game Center Concepts

This section describes things you should know before adding Game Center support to your game.

All Game Center Games Start by Authenticating a Player

Matchmaking, leaderboards, and achievements all use an authenticated player on the device, known as thelocal player. For example, if your game reports scores to a leaderboard, they are always reported for the authenticated player. Most Game Center classes function only if there is an authenticated player. Before your game uses any Game Center features, it must authenticate the local player. When your game authenticates the local player, it checks to see if a player is already authenticated on the device. If there is not an authenticated local player, Game Kit displays a user interface to allow the player to log in with an existing account or create a new account.

Your game must disable all Game Center features when there is not an authenticated player.

Important: On devices where Game Center is not available, Game Kit returns aGKErrorNotSupported error when your game attempts to authenticate the local player. Your game must handle any authentication errors by disabling all Game Center features.


Your Game Must Already Use a View Controller to Manage Its Interface

All Game Center views are presented modally by your game using your game’s view controller as the hosting view controller. Leaderboards, achievements and matchmaking all provide view controller classes that allow your game to display relevant information to the user. For example, the GKLeaderboardViewController class provides a standard user interface to display your game’s leaderboard information to the user.

Using a view controller to control your interface is a good idea for other reasons. For example, you use a view controller to support orientation changes on the device. For more information on using view controllers in your game, seeView Controller Programming Guide for iOS.

Most Classes that Access Game Center Operate Asynchronously

With few exceptions, classes in Game Kit access the Game Center services asynchronously. These operations include posting data to Game Center, requesting data from Game Center, or asking Game Center to perform a task (such as matchmaking). In all cases, your game posts a request by calling Game Kit. Later, when the operation completes (or fails because of an error), Game Kit calls your game to return the results.

Callbacks for Game Center classes are implemented using block objects. The block object receives an error parameter. Depending on the operation your game requests, the block also receives other parameters to allow Game Kit to return information to your game. You should be comfortable with block objects before attempting to add Game Center to your game. See A Short Practical Guide to Blocks for an introduction to block programming.

Make Sure Your Game Sends Data Again After a Network Failure

When you implement leaderboards or achievements, your game reports a player’s scores or achievement progress to Game Center. However, networking is never perfectly reliable. If a networking error occurs, your game should save the Game Kit object and retry the action again at a later time. Both the GKScore and GKAchievement classes support the NSCoding protocol, so your game can archive those objects when it moves into the background.

Your Game May Received Partial Data After a Network Failure

When your game retrieves data from Game Center (for example, when loading score data from a leaderboard), networking errors can occur. When a networking error occurs, Game Kit returns a connection error. However, where possible, Game Kit caches data it receives from Game Center. In this case, your game may receive both an error and partial results from Game Kit.

Steps for Implementing Game Center Awareness in Your Game

When you are ready to add Game Center support to your game, follow these steps:

  1. Enable Game Center in iTunes Connect. See “Configure Your Application in iTunes Connect.”
  2. Configure the Bundle Identifier for your game. See “Create Your Application’s Bundle Identifier.”
  3. Link to the Game Kit framework.
  4. Import the GameKit/GameKit.h header.
  5. Determine whether your game requires Game Center.
    • If your game requires Game Center, add the Game Center key to the list of capabilities your application requires from the device. See“Requiring Game Center in Your Game.”
    • If your game does not require Game Center, you should establish a weak link from your game to the Game Kit framework. Then, when your game launches, test to make sure that Game Center is supported. See“Optionally Using Game Center In Your Game.”
  6. Authenticate the player as soon as your application has launched far enough to present a user interface to the user. See“Working with Players in Game Center.”

Configure Your Application in iTunes Connect

Before you add any Game Center code to your application, you need to set up your game in iTunes Connect and enable it to use Game Center. This is also where you go to configure your application’s leaderboards and achievement information.

The process for configuring your game to support Game Center is described iniTunes Connect Developer Guide.

Create Your Application’s Bundle Identifier

A bundle identifier is a string you create for an application (such ascom.myCompany.myCoolGame). The bundle identifier is used throughout the application development process to uniquely identify an application. For example, the bundle identifier is used to match the application you built in Xcode to the data you configured in iTunes Connect.

For applications that do not use Game Center, you need only provide a bundle identifier in your Xcode project prior to shipping your application to customers; you can start developing an application without setting a bundle identifier. However, when you develop a Game Center application, you must set the bundle identifier before implementing Game Center in your application. Game Center uses the application’s bundle identifier to retrieve the application’s data from the Game Center service. Your application cannot use matchmaking or retrieve leaderboard or achievement information until you set the application’s bundle identifier in your project.

For more information on setting the bundle identifier, see Information Property List Key Reference.

Requiring Game Center in Your Game

If your game requires Game Center to function (for example, a multiplayer game that requires Game Center to match players), you want to ensure that only devices that support Game Center can download your application. To ensure that your application only runs on supported devices, add the gamekit key to the list of required device capabilities in your application’sInfo.plist file. See “iTunes Requirements” in iOS Application Programming Guide.

Optionally Supporting Game Center In Your Game

If you want your game to use Game Center, but your game does not require Game Center in order to function properly, you canweak link your game to the Game Kit framework and test for its existence at runtime.

Listing 1-1 provides code you should use to test for Game Center support. This function tests for the existence of theGKLocalPlayer class, which is required to perform player authentication, and also for iOS 4.1, which is the first version of iOS that has complete support for Game Center.

Note: Game Center was first previewed in iOS 4.0, and some Game Center classes can be found there. However, the classes changed prior to their official release in iOS 4.1. You should not attempt to use Game Center on devices running iOS 4.0.


Listing 1-1  Testing whether the Game Center classes are available

BOOL isGameCenterAPIAvailable()


{


    // Check for presence of GKLocalPlayer class.


    BOOL localPlayerClassAvailable = (NSClassFromString(@"GKLocalPlayer")) != nil;


 


    // The device must be running iOS 4.1 or later.


    NSString *reqSysVer = @"4.1";


    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];


    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);


 


    return (localPlayerClassAvailable && osVersionSupported);


}


iOS 4.1 may be installed on some devices that do not support Game Center. On those devices, theisGameCenterAPIAvailable function defined Listing 1-1 still returns YES. The next step in confirming whether Game Center may be used on the device is to attempt to authenticate the local player; on devices that do not support game center, your game receives aGKErrorNotSupported error. For more information, see “Authenticating a Local Player.”

Testing a Game Center Application

To help you test your application, Apple provides a sandbox environment for Game Center. This sandbox environment duplicates the live functionality of Game Center, but is separate from the live servers. The sandbox allows you to test your Game Center features without making your application visible to regular users. You should thoroughly test your Game Center application in sandbox before submitting your application for approval.

As a developer, you are required to create a separate Game Center account for Sandbox. At any given time, you must choose whether to log into Sandbox for testing, or into the live environment. Start by launching the Game Center app and logging out the currently authenticated player. After this, run your game or another Game Center enabled application. Depending on how that application is distributed, you enter different credentials. If that app is provisioned for development, enter your test account information (logging you into the Sandbox). Otherwise, enter your live account information (logging you into the live environment. Table 1-1 shows which builds run in which environments.

Important: Always create new test accounts specifically to test your application in Game Center. Never use an existing Apple ID.


Table 1-1  Which build for which audience and environment

Application

Audience

Game Center Environment

Simulator build

Developer

Sandbox Environment

Developer build

Developer

Sandbox Environment

Ad-hoc distribution build

Beta Testers

Sandbox Environment

Signed Distribution build

End Users

Live Environment

The Sandbox does not allow sharing of information about what games are being played. This prevents your testers from revealing the existence of your application to other players.

Testing Your Game in Simulator

Leaderboards and achievements work the same way in Simulator as they do on a device. However, matchmaking invitations may not be sent or received while your application is running in Simulator.

原创粉丝点击