Manage leaderboards in your HTML5 game with App42 Cloud API
Talking about Facebook, Game development, HTML5 and Javascript.
Do you like my tutorials?
Then consider supporting me on Ko-fi.

1 | <script src = "App42-all-3.0.min.js" ></script> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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){} }); } } |
1 | var scoreBoardService = new App42ScoreBoard(); |
scoreBoardService
is initialized.
To show the top 10 leaderboard, I use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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.