基本上都是利用gameNetwork的功能即可,
實作不難,考慮同時支援Apple和Google,
先將程式碼貼上再來講解:
local gameNetwork = require( "gameNetwork" ) local playerName local playerId local leaderboardsListId= { ["HighestScore"]= { ["Description"] = "Highest Score", ["Identifier"] = "com.gmail.mycom.demo.Leaderboards.HighestScore" } } local achievementsListId = { ["FinishLevel1"]= { ["Description"] = "Finish Level 1", ["Identifier"] = "com.gmail.mycom.demo.Achievements.FinishLevel1" }, ["FinishLevel2"]= { ["Description"] = "Finish Level 2", ["Identifier"] = "com.gmail.mycom.demo.Achievements.FinishLevel2" } } if(isAndroid) then leaderboardsListId= { ["HighestScore"]= { ["Description"] = "Highest Score", ["Identifier"] = "Cgkxxxxxxxx" } } achievementsListId = { ["FinishLevel1"]= { ["Description"] = "Finish Level 1", ["Identifier"] = "Cgkxxxxxxx" }, ["FinishLevel2"]= { ["Description"] = "Finish Level 2", ["Identifier"] = "Cgkxxxxxxxx" } } end function showLeaderboards( event ) print("showLeaderboards()") if (playerId ~= nil) then if ( isAndroid ) then gameNetwork.show( "leaderboards" ) else gameNetwork.show( "leaderboards", { leaderboard = {timeScope="AllTime"} } ) end else gameNetworkSetup() end return true end local function postScoreSubmit( event ) --whatever code you need following a score submission... return true end local function updateLeaderBoards(type, myScore) if (playerId == nil) then return end gameNetwork.request( "setHighScore", { localPlayerScore = { category=leaderboardsListId[type]["Identifier"], value=tonumber(myScore) }, listener = postScoreSubmit } ) print("gameNetwork.request( setHighScore) finished") end function showAchievements( event ) print("showAchievements()") if (playerId ~= nil) then gameNetwork.show( "achievements" ) else gameNetworkSetup() end return end local function achievementRequestCallback( event ) return true end local function updateAchievement(type) if (playerId == nil) then return end gameNetwork.request( "unlockAchievement", { achievement = { identifier=achievementsListId[type]["Identifier"], percentComplete=100, showsCompletionBanner=true }, listener = achievementRequestCallback } ) end local function loadScoresCallback( event ) if(event.data == nil) then print("event.data is nil") return end for i=1,25 do if(event.data[i] == nil) then break end print("event.data[",i,"].playerID:",event.data[i].playerID) print("event.data[",i,"].category:",event.data[i].category) if(event.data[i].playerID == playerId) then print("matched playID...........") break end end end local function loadLocalPlayerCallback( event ) if(event.data == nil) then print("event.data is nil") return end playerId = event.data.playerID playerName = event.data.alias print("loadLocalPlayerCallback(),playerName:",playerName,",playerId:",playerId) if(playerId == nil) then return end myGameSettings.gameCerterHasEverLoggedIn = true saveData() gameNetwork.request( "loadScores", { leaderboard = { category = leaderboardsListId["HighestScore"]["Identifier"], playerScope = "Global", -- Global, FriendsOnly timeScope = "AllTime", -- AllTime, Week, Today --range = {1,5}, playerCentered = true, }, listener = loadScoresCallback }) end local function loadAchievementCallback( event ) if(event.data == nil) then print("event.data is nil") return end for i=1,#event.data do if(event.data[i] ~= nil) then print("event.data[",i,"].identifier:",event.data[i].identifier) print("event.data[",i,"].title:",event.data[i].title) print("event.data[",i,"].isCompleted:",event.data[i].isCompleted) end end end local function gameNetworkLoginCallback( event ) if(isAndroid==false) then if(event.data) then print("User has logged into iOS Game Center") else print("User has NOT logged into iOS Game Center") return end end gameNetwork.request( "loadLocalPlayer", { listener=loadLocalPlayerCallback } ) gameNetwork.request( "loadAchievements", { listener=loadAchievementCallback } ) return true end local function gpgsInitCallback( event ) if(event.data) then print("User has logged into Google App Play Center") gameNetwork.request( "login", { userInitiated=true, listener=gameNetworkLoginCallback } ) else print("User has NOT logged into Google App Play Center") end end function gameNetworkSetup() if ( isAndroid ) then myGameSettings.gameCerterHasEverLoggedIn = false gameNetwork.init( "google", gpgsInitCallback ) else gameNetwork.init( "gamecenter", gameNetworkLoginCallback ) end end }初始化
一般來說,我們可以在main.lua裡呼叫gameNetworkSetup(),
對於Android系統,在gpgsInitCallback()裡可以確認是否登錄成功
對於iOS系統,在gameNetworkLoginCallback()裡可以確認是否登錄成功,
至於後面的gameNetwork.request( "loadLocalPlayer", { listener=loadLocalPlayerCallback } ),
是用來確認目前登錄遊戲中心的使用者,有沒有呼叫它都可以,
gameNetwork.request( "loadAchievements", { listener=loadAchievementCallback } )則是用來回復使用者的紀錄, 例如使用曾經安裝過這個APP,移除它後又重新下載, 如果不想幫使用者回復紀錄,不執行也可以
顯示排行榜(leaderboard)
想要顯示排行榜,只要呼叫showLeaderboards()即可, 通常這個動作是在使用者按下某個按鈕時
顯示任務(achievement)
想要顯示排行榜,只要呼叫showAchievements()即可, 通常這個動作是在使用者按下某個按鈕時
更新排行榜 想要更新排行榜,只要呼叫updateLeaderBoards()即可, 通常這個動作是在使用者完成遊戲時
更新任務 想要更新排行榜,只要呼叫updateAchievement()即可, 通常這個動作是在使用者完成某個關卡時
以下是Apple的Game Center,和Android的Google Play Game Service稍有不同的地方
Apple Game Center
leaderboardsListId{}和achievementsListId{}裡的資料,
例如"com.gmail.mycom.demo.Leaderboards.HighestScore",
這是使用者自己決定的編碼,然後在iTunes Connect裡設定即可,
不重覆就行
Google Play Game Service
Google Play Game Service
leaderboardsListId{}和achievementsListId{}裡的資料,
例如"Cgkxxxxxxxx", 這是使用者在play store publish的遊戲中心設定後系統給的編碼, 另外,對於Google Play Game Service, 我們還需要在build.settings增加:
例如"Cgkxxxxxxxx", 這是使用者在play store publish的遊戲中心設定後系統給的編碼, 另外,對於Google Play Game Service, 我們還需要在build.settings增加:
android = { usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE", "com.android.vending.BILLING", }, googlePlayGamesAppId = "123456789012" },usesPermissions無所謂,
googlePlayGamesAppId是在play store publish的遊戲中心連結應用程式後系統給的編碼
沒有留言:
張貼留言