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 visithttp://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/<select framework>
Here are the frameworks you’ll need to ensure you’ve included in your XCode project.
- Foundation
- UIKit
- CoreGraphics
- QuartzCore
- Security
- SystemConfiguration
- libsql3.0.dylib (located in (iPhoneSDK Folder)/usr/lib/)
- CFNetwork
- CoreLocation
- MapKit (if building with SDK 3.0 or newer)
And here we see the frameworks that have been added to the XCode project.
For the latest release of OpenFeint, if you have set your ‘iPhoneOS Deployment Target’ to any version before 3.0 you must weak link some libraries. Select ‘Targets’ in the Groups & Files pane.
- Right click your target and select Get Info.
- Select the ‘General’ tab.
- Under ‘Linked Libraries’ change the following libraries from ‘Required’ to ‘Weak’
- UIKit
- MapKit
For my project I’m not building for lesser versions, which from what I’ve heard though does exclude a sizable base of users.
Finally, in step 9 of the README.txt, we need to add the following line to our prefix header:
1 |
#import "OpenFeintPrefix.pch" |
The following screen capture shows the quick update of the project’s prefix header.
At this point we now have the SDK integrated within our XCode project along with some necessary support requirements. Again, this tutorial covers the specific integration of OpenFeint with a Cocs2D-iPhone v8.2 based application. As with most application architectures, there will be various class files such as the application delegate, game scene, and menu scene.
Let’s now add the required OpenFeint code to our project’s AppDelegate.
Within the AppDelegate’s main file we’ll add various code elements ranging from importing other class headers to specific code needed within existing methods. For example, there’s specific OpenFeint code that should be added to applicationWillResignActive, applicationDidBecomeActive, and applicationWillTerminate. These methods can already be found in your Cocos2D application delegate, and we’ll be adding a line here or there to support integration with OpenFeint. The OpenFeint developer documentation details these requirements as it’s expected that you’re taking action based upon the application’s state, such as it shutting down.
Add the following import statements to your AppDelegate’s main file:
2 |
#import "SampleOFDelegate.h" |
Now locate the respective methods within your AppDelegate’s main file, and add the lines identified for OpenFeint.
01 |
- ( void )applicationWillResignActive:(UIApplication *)application { |
02 |
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic]; |
03 |
[[Director sharedDirector] pause]; |
04 |
[OpenFeint applicationWillResignActive]; |
07 |
- ( void )applicationDidBecomeActive:(UIApplication *)application { |
08 |
[[Director sharedDirector] resume]; |
09 |
[OpenFeint applicationDidBecomeActive]; |
11 |
- ( void )applicationWillTerminate:(UIApplication *)application { |
12 |
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic]; |
13 |
[[Director sharedDirector] end]; |
14 |
[[NSUserDefaults standardUserDefaults] synchronize]; |
Next we’ll create a class that will handle some crucial tasks whenever we call the OpenFeint leaderboard. At the time, I had based this on the included SampleOFDelegate files. Pay particular attention to the dashboardDidAppear and dashboardDidDisappear methods. You’ll see that we’re momentarily pausing the Cocos2D director and then re-enabling it once the dashboard disappears. This is a critical step cause otherwise it’s possible that input will be inconsistent or even not captured when the dashboard is displayed. But by pausing the director, we’re ensured that all user input is captured by the dashboard.
Create the following files within your XCode project.
SampleOFDelegate.h
07 |
#import "OpenFeintDelegate.h" |
08 |
@interface SampleOFDelegate : NSObject< OpenFeintDelegate > |
09 |
- ( void )dashboardWillAppear; |
10 |
- ( void )dashboardDidAppear; |
11 |
- ( void )dashboardWillDisappear; |
12 |
- ( void )dashboardDidDisappear; |
13 |
- ( void )userLoggedIn:(NSString*)userId; |
14 |
- ( BOOL )showCustomOpenFeintApprovalScreen; |
SampleOFDelegate.m
07 |
#import "SampleOFDelegate.h" |
09 |
@implementation SampleOFDelegate |
11 |
- ( void )dashboardWillAppear |
15 |
- ( void )dashboardDidAppear |
17 |
[[Director sharedDirector] pause]; |
18 |
[[Director sharedDirector] stopAnimation]; |
21 |
- ( void )dashboardWillDisappear |
25 |
- ( void )dashboardDidDisappear |
27 |
[[Director sharedDirector] resume]; |
28 |
[[Director sharedDirector] startAnimation]; |
31 |
- ( void )userLoggedIn:(NSString*)userId |
33 |
OFLog(@ "New user logged in! Hello %@" , [OpenFeint lastLoggedInUserName]); |
36 |
- ( BOOL )showCustomOpenFeintApprovalScreen |
For my particular Cocos2D application, I have a splash scene that quickly shows the company logo, the Cocos2D logo, and then transitions to the game’s main menu. I initialize OpenFeint during this process just before loading the main menu. With that said, the header file for the splash scene class has the following code. We’re including the SampleOFDelegate class as well as declaring the ofDelegate variable. I’ve streamlined the content to only include the references to the OpenFeint code. I’ve left the reference to the menuScene method though as that’s where I perform the initialization.
06 |
#import <UIKit/UIKit.h> |
08 |
@ class SampleOFDelegate; |
09 |
@interface SplashScene : Scene { |
10 |
SampleOFDelegate *ofDelegate; |
For the main file of the splash scene class, we import the following header files:
2 |
#import "SampleOFDelegate.h" |
Within my method that is called before transitioning to the main menu (menuScene), I initialize OpenFeint. It is here where we can set the orientation, disable chat, and provide our application specific values such as the product key. You might recall that these were provided when first registering the application within OpenFeint’s developer portal as shown in the following:
For the initialization process, we declare a dictionary that will include the details specific to our application. Where ever it is you want to perform the initialization within your application, add the following code.
1 |
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation, [NSNumber numberWithBool:YES], |
2 |
OpenFeintSettingDisableUserGeneratedContent, nil]; |
3 |
ofDelegate = [SampleOFDelegate new ]; |
4 |
OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:ofDelegate]; |
5 |
[OpenFeint initializeWithProductKey:@ "zGMPDEYldUia4j86uV1mA" |
6 |
andSecret:@ "QMjb1n9dV4MVO09U05R56MUCeJf7VZHnKlQIvRKtk" |
7 |
andDisplayName:@ "My Crazy App" |
9 |
andDelegates:delegates]; |
For my particular application, once the initialization has been done, it then transitions to the main menu. If you’re already a registered user you’ll get the welcome back screen or if you’re a new user (or perhaps using a new device), you’ll have the opportunity to either login or register for a new account.
The following shows the OpenFeint screen that is seen when a new user or device is detected. The user can enable OpenFeint or alternatively indicate they don’t want these features right now.
Upon enabling OpenFeint, the application will recognize a device already associated with the user’s account and automatically log them in as shown in the following screen capture.
Additional features in the latest OpenFeint release now provide the ability to connect with your Twitter and FaceBook accounts as well as even share your location for geographic based scoring comparisons.
Once logged in, the user can easily review the scores for the various applications that use OpenFeint. In this case, we’re going to review the leaderboard for Balloon-Boy. The following shows the layout of my particular game’s main menu.
When a user selects the Scores option at the main menu, the following line of of code is executed within the called method. Take note of the text passed text value. This is the lD for our leaderboard that was assigned at the time of creation, which in this case is reflecting the value for the test application registered for this tutorial.
1 |
[OpenFeint launchDashboardWithHighscorePage:(NSString*)@ "181963" ]; |
The code is pretty self explanatory and launches the high score dashboard for our application. You also have the option of defaulting to other screens when initially launching the OpenFeint dashboard, so be sure to check the SDK for such details.
In the following screen capture, we see the various leaderboards tracked via OpenFeint. You might recall that this is what was enabled during the application registration process within OpenFeint’s developer portal. My application is very simplistic and simply tracks the user’s personal score and a global score. The following provides a view of the global leader board for the Balloon-Boy application.
As mentioned earlier, another neat feature available with the more recent OpenFeint version is the ability to share your location to identify other players in your general vicinity.
This all sounds great, but the next question is how do we post these scores to OpenFeint? The process is incredibly simple. For my Balloon-Boy game, after the user’s piloted balloon has had three impacts, the game is over. At this point I give the player the opportunity to play again, but when this method is initially called, I execute the following bit of code to post the user’s score. In particular, I have a variable named currentScore that contains the user’s accumulated score. The currentScore’s value is posted to the OpenFeint leaderboard. It is also important to note the leaderboard ID again. This is the same value given to us when we first created the leaderboard in the developer portal. The leaderboard details are provided again for reference in the following screen capture.
The leaderboard ID is used to identify the specific board for the application that’ll receive the value. The following shows the single line of code used to post the current score to the appropriate leaderboard for our application.
1 |
***************** 181963 is for the marathon leaderboard ************************* |
2 |
[OFHighScoreService setHighScore:currentScore forLeaderboard:@ "181963" onSuccess:OFDelegate() onFailure:OFDelegate()]; |
The following screen capture shows in my application where the user is presented with the opportunity to either play again or return to the main menu. When this overlay is initially shown, there is a brief confirmation message shown at the bottom of the screen as the score is posted to the OpenFeint leaderboard. This is as a result of executing the single line of code noted above.
The integration process is nearly complete with only some minor additional changes required. In order to compile the OpenFeint C code, we have to change the extension all of our main class files from .m to .mm. Meaning, gameScene.m now becomes gameScene.mm. There’s no impact to the existing Objetive-C code, but if not done then there will be a lot of problems when attempting to compile the code otherwise.
Lastly, if you’re using a physics engine such as Chipmunk, there will also be errors at compile time due to the static void method. In particular, you’ll receive an error like request for member shape in something not structure or union. This is an issue of simply needing to cast the shape data and is solved by adding (Sprite *) as shown in the following.
02 |
eachShape( void *ptr, void * unused) |
04 |
cpShape *shape = (cpShape*) ptr; |
05 |
Sprite *sprite = (Sprite *)shape->data; |
07 |
cpBody *body = shape->body; |
08 |
[sprite setPosition: cpv( body->p.x, body->p.y)]; |
09 |
[sprite setRotation: ( float ) CC_RADIANS_TO_DEGREES( -body->a )]; |
This concludes the integration of OpenFeint with a Cocos2D-iPhone application tutorial. Hopefully the process went smoothly and you’re now updating scores to an online leaderboard. Feel free to contact me regarding any feedback or content errors. Also be sure to check OpenFeint’s own knowledgebase and support forums for additional information.
分享到:
相关推荐
- Cocos2D游戏引擎因其与iPhoneSDK的紧密集成,可以轻松整合第三方服务,例如FacebookConnect和OpenFeint。 #### 总结 通过本书,读者将全面了解如何使用Cocos2D游戏引擎,从基本的游戏概念设计到实际编程和应用...
第1章 Cocos2D-X引擎的介绍 1 1.1 何为游戏引擎 1 1.1.1 游戏的核心—引擎 1 1.1.2 引擎的特点 2 1.1.3 知名的引擎介绍 4 1.1.4 引擎的分类 5 1.2 Cocos2D-X引擎的来历 8 1.3 引擎的版本 9 1.4 下载与安装 10 1.5 ...
介绍一些全球,特别是亚洲的典型游戏,在OpenFeint平台的基本数据表现。通过数据分析,挖掘一款成功的游戏,都有哪些具体的数据表现,具体的特征。并给游戏开发者提供一些建议。
- **描述**:OpenFeint游戏平台的缓存文件夹。 - **作用**:存储游戏数据。 - **注意事项**:清理前请考虑是否需要保留特定游戏数据。 #### Picstore - **描述**:图片浏览软件创建的目录。 - **作用**:存储浏览过...
`Android-IceSoap`是Java库`IceSoap`的Android版本,由OpenFeint公司开发,后由其他开发者维护。它主要解决了Android原生对SOAP支持不足的问题,使得开发者可以通过简单的注解方式定义SOAP请求和响应,减少了手动...
常见的游戏引擎如Cocos2D和Unity3D都配备了强大的物理引擎,而Photon则专注于提供网络引擎的功能,与这些游戏引擎无缝集成。 从实例演示中可以看到,许多知名游戏项目都采用了Photon作为其网络解决方案,包括《The ...
香港大不同是一个2011年左右个人开发者开发的找茬类的游戏项目,有米广告SDK使用的是早期的2.2版本,项目还用到了OpenFeint与九城合作的一个SDK,OpenFeint是一个为iOS和andriod系统提供在线游戏竞技的技术平台,...
应用说明:本应用最初是为了演示第九城市的OpenFeint的API调用,故而游戏性不是开发的重点,不过对于Android开发新手来说,作为一个参考教程还是不错的,如果是为了玩俄罗斯方块,请绕行~~ 版权说明:本游戏精简至...
- **飞行模式**: 玩游戏时可将手机设置为飞行模式或关闭蜂窝数据,避免Gamecenter和Openfeint等应用自动联网排名。 **2. 网络状态识别** - **3G/4G标识**: 手机左上角显示3G或4G标识表示正在使用3G/4G网络。 - **...
5. **Fruit Ninja THD** - 这款切水果游戏在Honeycomb平板上有很好的表现,有多种模式和多人对战功能,通过OpenFeint可以与朋友分享成绩。 6. **Galaxy On Fire 2 THD** - 是一款太空模拟游戏,为NVIDIA Tegra优化...
5. GREE以1.04亿美元收购OpenFeint:日本社交游戏平台GREE通过收购OpenFeint进入美国市场,采取了不同于DeNA的策略,更加注重本地化和内容控制。 4. EA以最高13亿美元收购PopCap:电子艺界(EA)对PopCap的高价收购...