The sample app you’re looking at has code beyond just working with game center. It has to produce a user interface. It also serves as an example of using Composer, our scene manager.
Your game is likely going to be several hundred to several thousand lines of code before you’re done. Your app is likely going to have multiple screens, which means you will likely want to use a scene manager like Composer. It will be in your long term best interest to learn how to use Composer. That is what leaderboards.lua and achievements.lua are. They are scenes that let you test granting achievements.
Game Center is actually pretty easy. You have to:
a. Initialize it. ( a few lines of code )
b. Login to it. ( maybe 20-30 lines )
c. When you want to record a high score, set the high score (maybe 5 lines of code)
d. When you want to record an achievement, set the achievement (maybe 5 lines of code)
e. When you want to show a leaderboard or achievement call the appropriate API’s ( a couple of lines of code)
The basic initialization and login code is something like this:
local gameNetwork = require("gameNetwork") local function loadPlayerRequestCallback(event) playername = event.data.alias playerID = event.data.playerID end local function gameCenterLoginCallback(event) gameNetwork.request( "loadLocalPlayer", { listener=loadPlayerRequestCallback } ) return true end gameNetwork.init("gamecenter", gameCenterLoginCallback)
Here is some code to that will save a high score.
local function postScoreSubmit(event) if event.errorCode then native.showAlert("Error", event.errorMessage, { "Ok"}) end end gameNetwork.request("setHighScore", { localPlayerScore = { category="highscores", value=tonumber(score) }, listener=postScoreSubmit})
Now of course this assumes your leaderboard is called “highscores” (the category) and that your score is in a variable called score. You don’t really need the listener, but its there if you want to do work if needed.
Rob