[OpenFeint-cocos2d]整合教程~稍后

Integrating OpenFeint with Cocos2D-iPhone Applications

OpenFeint is a service that enables your iPhone/iPod Touch application the ability to provide online score tracking.  The service is free and can easily be incorporated into your applications.   This tutorial will cover the integration of OpenFeint 2.4.3 with the Cocos2D-iPhone framework.  Before getting started, it is assumed that you already have familiarity with:

  • Objective-C
  • XCode
  • Cocos2D-iPhone framework

The expectation is that you already have a working Cocos2D based application to which you’d like to enable leaderboard functionality.  If you’re new to Cocos2D-iPhone, then you should visit http://www.cocos2d-iphone.org/ to review the available documentation and download the latest framework.  You do not have to be a seasoned veteran to use Cocos2D, and the framework makes it very easy to create high quality games.  At the time of this writing, version 8.2 of the Cocos2D-iPhone framework was the most stable version, and hence it provides the basis for this tutorial.

There are several features available within OpenFeint in terms of score and event management.  Beyond just tracking high scores, the developer can establish goals such that the player can earn achievements or even initiate online challenges.  For this tutorial, the focus will be to simply enable an online leaderboard.  The player will be able to track their own progress as well as compare their scores against other players via a global leaderboard.

There is also the ability to enable a chat function between players.  While I have not tried it, I have been told by other developers that by enabling this feature you’re required to assign a mature rating.  The mentality is that unrestricted chat within a game could expose minors to unscrupulous users and content.

Specifically, the OpenFeint capabilities consist of the following features:

Access to the OpenFeint code is done through the OpenFeint developer portal, and there is no charge to enroll in the developer program. Visit http://www.openfeint.com// to sign-up for access.

Once authenticated with the portal, you’ll have access to the developer home page and will be able to download the OpenFeint SDK.

When enabling an application to use the OpenFeint service, you register it within the developer portal. Selecting the green plus button at the top of the page allows you to start this process. In this example we’ll add a test application, My Crazy App, so that you can see the various screens. Yet, the specific code examples shown later on will reflect the integration process with one of my OpenFeint enabled games, Balloon-Boy.

The following screen capture shows the entry of the application details to register the application.

After selecting submit, the application is given a Client Application ID, Product Key, and Product Secret. As we’ll later see, these values are needed when initializing OpenFeint within our application. Keep them handy as we’ll need them when it comes time to add the respective OpenFeint code to the application.

The following shows a closer look at the application’s specific product key and secret. They are unique for each registered application and are used to identify the specific application when communicating with the OpenFeint servers.

The following shows the creation of the game’s leaderboard. It’s a straight forward process initiated by selecting ‘Basic Features’ and then Leaderboards.

After selecting submit, we see that our leaderboard has been created. It is important to note the leaderboard ID as we’ll need this value later on when attempting to post user’s scores to the board. The value identifies the specific leaderboard and is a required element for the code that is executed when we initiate onlinescore updates.

Now that we’ve enrolled in the OpenFeint program, downloaded the SDK, and created an entry for our application, it is time to start the integrating process within our Cocos2D-iPhone application.

There are some preliminary steps before inclusion of code within your class files.  If you’re upgrading from a prior version, the process is relatively straight forward.  You simply delete the old OpenFeint folder from your XCode project and need to ensure that you add some additional frameworks, which will be detailed in the following section.  Note: it is also important that you go into your project’s directory and also delete both the OpenFeint and build directories.

Let’s review the the OpenFeint README.txt for information on integration of the application.

If you’ve downloaded and extracted the OpenFeint SDK, you’re ready to begin the process.  For step 4 of the README file, we’re to drop the unzipped OpenFeint folder into our XCode project. And since the game is landscape only, we’ll remove the Portrait folder found under the Resources directory as suggested for step 5.  The following shows the inclusion of the OpenFeint folder within our XCode project.

Continuing on with Step 6, we right click on our project icon within XCode and select Get Info. In looking at the build tab, we added the Linker Flags the value -ObjC as well as selected ‘Call C++ Default Ctors/Dtors in Objective-C’. These are shown in the following screen capture.

For step 7, the README.txt notes the following frameworks that must be included within the project.  And if you’re like me, I can never remember the all too convenient path so here it is for reference.  Right click on frameworks and add existing framework.  Navigate to:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulatorx.y.z.sdk/System/Library/Frameworks/