Manage leaderboards in your HTML5 game with App42 Cloud API
Talking about Facebook, Game development, HTML5 and Javascript.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
Now, do you want a test drive? Let’s have a look at Risky Steps on Facebook, possibly with a desktop computer as I did not optimize the leaderboard for mobile yet.
Just below my CPMSTAR advertising – read How to publish your HTML5 game on Facebook, using Facebook login, Share and CPMStar ads if you need more information – you can see a leaderboard.
This was made first including the JavaScript you can download once you setup your app in App42 Cloud API in your index page:
<script src = "App42-all-3.0.min.js"></script>
function saveScore(n){
if(n){
var gameName = "Risky Steps";
var userName = facebookName;
if(userName == ""){
userName = "Guest";
}
var gameScore = n;
var result;
var scoreBoardService = new App42ScoreBoard()
scoreBoardService.saveUserScore(gameName,userName,gameScore,{ success: function(object){} });
}
}
var scoreBoardService = new App42ScoreBoard();
scoreBoardService is initialized.
To show the top 10 leaderboard, I use:
var scoreBoardService = new App42ScoreBoard();
scoreBoardService.getTopNRankers("Risky Steps", 10,{
success: function(object)
{
var scorelist = "";
var game = JSON.parse(object);
result = game.app42.response.games.game;
var scoreList = result.scores.score;
if (scoreList instanceof Array) {
for (var i = 0; i < scoreList.length; i++) {
scorelist += "<tr><td align = \"left\">" + scoreList[i].userName + "</td><td align = \"right\">" + scoreList[i].value.toString() + "</td></tr>";
}
}
document.getElementById("leaderboard").innerHTML = "<table width = \"100%\"><tr><td colspan = \"2\"><strong>TOP SCORES</strong></td>"+scorelist+"</table>";
},
error: function(error) {
}
});
div element but it’s obvious you can do what you want with it.
I am a bit worried about security though, as the leaderboard is easily hackable, but this is the problem you’ll always have when dealing with 100% JavaScript applications. The good news is you aren’t using your own database so your server will remain safe no matter what happens to leaderboards. Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.