Initializing a game network connection

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.

Getting ready

Return to your project directory and open the main.lua file.

Getting on with it

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"

Note

Because system.getInfo can return a platform name containing spaces, the gsub call is there to avoid conflicts with tokens or other issues.

Registering application load

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

Tracking connection progress

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

Requesting a Game Center connection

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)

What did we do?

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.

What else do I need to know?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.129.19