Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Apple Game Centre sample code
Started by gogigoranic8 Feb 02 2016 01:10 PM

- - - - -
1 reply to this topic
game apple centre

Best Answer Rob Miracle , 02 February 2016 - 06:15 PM

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

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

gogigoranic8

[GLOBAL: userInfoPane.html]
gogigoranic8
  • Contributor

  • 149 posts
  • Corona SDK

Hi Coronaville :) !

 

Can somebody please share complete Apple Game Centre sample code?

 

The only one I found in Corona Simulator samples is rather difficult/advanced one (it includes over 300 lines of code and separate library leaderboard file).

 

Are avatars available (can be seen by users) when using Corona gameNetwork library?

 

Many, many thanks!

Goran



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,544 posts
  • Enterprise

  Best Answer

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




[topic_controls]
[/topic_controls]