Now that everything is enabled, the app itself needs to request a connection to the specified set of social features. It will use Corona's gameNetwork
library to sign into the remote service, so that later calls will make requests to the right host.
Near the top of main.lua
, select a module based on the operating system:
do local valid, loader = pcall(require, "social."..system.getInfo("platformName"):gsub("%s", "_")) if valid then social = loader else social = {loaded = false} end end local storyboard = require "storyboard"
Inside the social
folder in your project directory, create a new text file, iPhone_OS.lua
, and open it for editing.
First, create a local table that can be used to share states with outside code:
local gameNetwork = require "gameNetwork" local status = {} return status
Register a system event listener so that we can connect after the rest of our app has finished loading:
local status = {} local function loader(event) end Runtime:addEventListener('system', loader) return status
When Game Center finishes loading, Corona will call the function, if any, that you provided when you requested the initialization, and provide it with the results of the initialization attempt, using the value init
for the event.type
field when initialization is finished:
local status = {} local function register( event ) if event.type == 'init' then end end local function loader(event)
We can use this to record whether we connected to the service successfully, using the local table we recorded earlier:
local function register( event )
if event.type == 'init' then
status.loaded = event.data
end
end
Finally, we'll combine this tracker function with the response we prepared to the application loading so that we actually request a connection to the game network:
local function loader(event) if event.type == 'applicationStart' then gameNetwork.init("gamecenter", register) Runtime:removeEventListener('system', loader) end end Runtime:addEventListener('system', loader)
We made a connection to the Game Center service once all the application resources were loaded and are now ready to receive it. We also left ourselves a way to determine whether or not this connection was completed.
App functionality that depends on Game Center can't be tested in the Corona Simulator, because Game Center depends on your app being built with the correct bundle ID to connect it to the desired features (leaderboards and achievements). You need to build it for your device or for the XCode Simulator in order to test Game Center features.
18.221.129.19