cocos2d v0.99.0 released. Release Notes: 0_99_0
High Score / Social Networks
High Score / Social Networks sorted alphabetically.
Integrating AGON Online into Cocos2D-iPhone
Welcome to this guide, where we will create a simple Cocos2D iPhone game that uses AGON Online to save and display scores.
I assume that you have downloaded the cocos2D-iphone sdk and have run the installer which enables Cocos2D template projects in XCode.
Getting Started
Signup as a developer AGON online http://developer.agon-online.com/join
follow the steps to setup a game on the AGON servers: http://developer.agon-online.com/documentation/latest/game_registration.html
Open XCode and create a new Cocos2D template app. This will create a “Hello World” sample application.
Download the AGON SDK http://devdb.agon-online.com/developer/dashboard/downloads
Follow the steps to get AGON integrated to your project. http://developer.agon-online.com/documentation/latest/quick_start.html
Important:
A Cocos2D project has 2 build targets. One target is for Cocos2D and the other is for your app. Settings defined on project level will be inherited by the targets. As we only want to apply setting for the app target, You must add the AGON library on your app's target node - not the top project node.
If you have trouble with linking when building the project, you have most likely overlooked some of the steps in the integration guide. Make sure you have:
- Added all required frameworks
- Added all parameters in build options
- That your OS version selection, Base SDK and Deployment target have been set properly.
- That your library search path actually points to your downloaded AGON SDK libs folder :)
Download the AgonPackage.bundle for your app from your developer page and add it as resource to your XCode project
Initializing AGON
At this point you should have a “Hello World” app, that builds with the AGON library. Now we add some AGON functionality.
Initialize AGON by calling AgonCreate() somewhere in your startup code for the application. Remember to give the correct game I suggest placing the call in “applicationDidFinishLaunching” in your AppDelegate.m file. Also, it is a good idea to configure AGON to print out debug messages to the console by calling AgonShowLogs(YES). This log can be helpful if something does not work as expected.
Menu with AGON Options
Next, we will create a menu with some buttons to activate AGON. We will do this by adding our own menu class derived from the Cocos2d CCMenu class. This provides an easy way to create menu items.
In “HelloWorldScene.h” we insert our new MenuLayer interface declaration before the “HelloWorld” interface declaration. so we get the following:
HelloWorldScene.h
#import "cocos2d.h" @interface MenuLayer : CCLayer { } -(void)playGame: (id)sender; -(void)showLeaderboards: (id)sender; @end // HelloWorld Layer @interface HelloWorld : CCLayer { } // returns a Scene that contains the HelloWorld as the only child +(id) scene; @end
We need to add the implementation for the new class. In “HelloWorldScene.m” we implement our “MenuLayer” class so that in the “init” function we create menu items that triggers the functions “playGame” and “showLeaderboards”.
To actually show our menu, we instantiate it and add it to the HelloWorldScene. This happens in the “init” function of the HelloWorldScene.
so the implementation for the class looks something like this:
HelloWorldScene.m
#import "HelloWorldScene.h" #import "AGON.h" @implementation MenuLayer - (id) init { self = [super init]; if (self != nil) { [CCMenuItemFont setFontSize:20]; [CCMenuItemFont setFontName:@"Helvetica"]; CCMenuItem *randomGame = [CCMenuItemFont itemFromString:@"Play Random Game" target:self selector:@selector(playGame:)]; CCMenuItem *leaderboardsItem = [CCMenuItemFont itemFromString:@"Show Leaderboards" target:self selector:@selector(showLeaderboards:)]; CCMenu *menu = [CCMenu menuWithItems:randomGame,leaderboardsItem,nil]; [menu alignItemsVertically]; [self addChild:menu]; } return self; } -(void)showLeaderboards: (id)sender { } -(void)playGame: (id)sender { } @end // HelloWorld implementation @implementation HelloWorld +(id) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node]; // 'layer' is an autorelease object. HelloWorld *layer = [HelloWorld node]; // add layer as a child to scene [scene addChild: layer]; // return the scene return scene; } // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init] )) { [self addChild:[MenuLayer node] z:1]; } return self; } // on "dealloc" you need to release all your retained objects - (void) dealloc { // in case you have something to dealloc, do it in this method // in this particular example nothing needs to be released. // cocos2d will automatically release all the children (Label) // don't forget to call "super dealloc" [super dealloc]; } @end
Playing a Game and Adding a Score Next, we will fill out the “playGame” function. As this is a simple guide, we will keep this super simple. Playing the game simply consists of getting a random number as your score, when tapping the button. You will add your real game flow here, like playing a level, and then submit a score when you are done.
AGON operates with the concept of GameSessions to facilitate offline storage in a performance friendly way. This means that when you start a game level, you should start with calling AgonBeginGameSession(), and as the final step in the game level, you should call AgonEndGameSession(). If you do not do this, you cannot submit scores or awards.
When submitting the score, you also have to specify a leaderboard ID, as you can have leaderboards for each level or difficulty. You setup your leaderboards on the developer site (http://developer.agon-online.com/documentation/latest/game_registration.html).
So our “playGame” function will look like the following:
-(void)playGame: (id)sender { // Emulate a game session resulting in a score that is stored by AGON. // It is important to encapsulate a game session with AgonStartGameSession() and AgonEndGameSession() AgonStartGameSession(); int score = rand(); AgonSubmitScore(score,[NSString stringWithFormat:@"%d",score],MY_LEADERBOARD_ID); AgonEndGameSession(); }
Showing the Score
Finally, we can now open the AGON interface and see the leaderboard with scores submitted by playing our game:
-(void)showLeaderboards: (id)sender { // Open AGON and show the leaderboard for which the game has submitted scores AgonShowLeaderboard(MY_LEADERBOARD_ID, YES); }
Special Note about AGON and Directors
AGON is built with UIKit from the iPhone SDK. Cocos2d has different directors to run the main loop which may affect performance of UIKit severely. This is especially true for CCDirectorTypeMainLoop combined with the compiler flag CC_DIRECTOR_DISPATCH_FAST_EVENTS. If you encounter problems with performance inside AGON, it would be a good idea before opening AGON to stop Cocos2d by calling
[[CCDirector sharedDirector] stopAnimation]
When AGON hides again resume Cocos2d by calling
[[CCDirector sharedDirector] startAnimation]
This will pause Cocos2d completely while AGON is showing and not starve the UIKit message pump.
Final words
This completes our guide of integrating AGON into a Cocos2d app with the most basic stuff: submitting scores. From here, you can explore the AGON SDK Documentation and learn more about how to:
- Submit awards
- Change profiles
- Connect game specific data to online profiles using cloud blobs plus other fancy stuff.
See you on the leaderboards!
Integrating cocos2d with cocosLive
Integrating cocos2d with OpenFeint
The following provides an overview on integrating OpenFeint with the Cocos2D-iPhone framework.
Integrating cocos2d with Plus+
Integrating cocos2d with Scoreloop
Ad Networks
Integrating cocos2d with Admob
The following provides an overview on integrating Admove with the Cocos2D-iPhone framework.
Trace: » social_networks