App42 provides a rich set of leaderboard APIs. In this post, I will describe how to integrate your Cocos2D game with the App42 cloud framework and use its leader board APIs. Please see my previous blog post on why a rich leader board is important for your game.
We will use our Ninja Fight iPad demo game to walk you through the integration. Ninja Fight is a simple multiplayer game in which opponents have to strike each other with missiles while avoiding getting hit themselves. The game starts of by asking the player to enter its name. This is the name that will be used later to submit the score for the user.
Once two users have started fighting, the game will transition to the fight scene. This is where the realtime action will happen using AppWarp apis. Using AppWarp with Cocos2D was described in an earlier blog post. Players simply need to tap in the direction of the opponent to fire missiles. You can also move the player by dragging it using your finger on the iPad.
Once the game is over the user’s score is submitted and an option to view the game’s leader board is given. Upon selecting this option, the game’s leader board is displayed using our API. This will simply list the users who have submitted the top scores, their rank and the score.
Now to the actual code itself for saving scores and getting the top scorers! Here is the initial set up that you need to do once before you can use all the leader board APIs.
ServiceAPI *serviceAPIObject = [[ServiceAPI alloc]init]; serviceAPIObject.apiKey = APP42_APP_KEY; serviceAPIObject.secretKey = APP42_SECRET_KEY; ScoreBoardService *scoreboardService = [serviceAPIObject buildScoreBoardService];
To submit the user’s score to the cloud, its one simple API call
[scoreboardService saveUserScore:@"Ninja Fight" gameUserName:userName gameScore:score];
Retrieving your game’s top 20 scorers from the cloud is also straight forward.
Game *game=[scoreboardService getTopNRankers:@"Ninja Fight" max:20]; NSMutableArray *scoreList = game.scoreList;
The API works synchronously so you should perform the API calls on a back ground thread so as to not block your main thread. This is extremely easy using the performSelectorInBackground utility iOS method.
Complete source code for the game can be downloaded from our Git Repo with the Readme file containing the instructions on how to set up and get it working.
App42 provides a complete set of leader board APIs which allow you to add advanced social leader boards based on groups, weekly, daily top scorers etc. Similarly you can use our reward management APIs to manage in-app rewards and purchases. For a complete reference, visit this page.
Please send your feedback and queries to us at email@example.com