IOS GameCenter程式設計入門
注意本篇為基礎教程,主要你用GameCenter的現有組成原件進行程式設計,如果希望進行高度自定義化的程式設計請參考蘋果的 GameCenter Programming Guide
本篇部落格分為以下幾個部分:
1.GameCenter中的幾個核心概念:
//初始化認證相關
GKPlayer
//通路GameCenter中心有關
GKGameViewController
GKAchievement
GKChallenge
GKLeadBoard
//通訊有關
GKMatchRequest
GKMatch
GKMatchMaker
GKMatchMakerViewController
2.GameCenter的具體執行個體:
初始化
檢視leadBoard/challenge
比對
通訊
GameCenter核心概念
GKPlayer
(資料包裝類)
概述:用來指代GameCenter中的玩家,裡面主要包含了關于遊戲玩家的ID,姓名,是否好友,等等資訊,具體請檢視reference
GKLocalPlayer繼承自GKPlayer
(功能類)
概述:用來指代本地玩家,即玩家自己
與GKPlayer不同點:GKPlayer主要是一個資料類,而GKLocalPlayer可以說是一個功能類.
主要功能:
1.進行本地GameCenter的登入,隻有登入後,才能進行後面的一系列的操作
2.擷取好友自己的好友資訊
3.擷取自己的leadBoard的資訊
4.注冊listener(監聽有沒有人向自己發起遊戲邀請)
5.進行遊戲儲存方面的處理
GKGameViewController
(控制器類)
概述:管理系統自帶的控制中心的界面在程式中的顯示,提供了顯示,排行榜,成就,挑戰等功能
主要功能:
通過對viewState的選擇來控制顯示leadBoard,Achievements,Challenges界面
GKAchievement
GKChallenge
GKLeadBoard
概述:擷取成就,挑戰,積分榜資訊
GKMatchRequest
概述:遊戲比對申請,在這裡面自己調整參數,來生成自己想要的遊戲申請,比如兩個人的對戰,三個人的對戰
GKMatchMaker
概述:進行遊戲比對
GKMatchMakerViewController
概述:系統已經幫我們定義好的遊戲比對界面,我們隻需要向它送出,GKMatchRequest就可以進行比對申請,其中可以選擇是邀請好友,或是自動比對對手
GameCenter流程圖

GameCenter具體執行個體
1.LocalPlayer的認證
let lp = GKLocalPlayer.localPlayer() //lp:=LocalPlayer
//下面的過程在程式中完成的位置越早越好,比如在AppDelegate中,向localPlayer傳送一個closure,localplayer會在需要的時候自動調用closure
lp.authenticateHandler = {(VC,error) in
//VC是一個用來進行GameCenter登入的視圖控制器,如果VC不為空,那麼就顯示視圖控制器來要求使用者登入
if VC != nil {
self.window?.rootViewController?.presentViewController(VC, animated: true, completion: nil)
}else if lp.authenticated{
println("Player authenticated")
}else {
println("Player didn't authenticated")
}
}
2.檢視leadBoard 或是Chanllenge或是Achievement
let gameVC = GKGameCenterViewController()
gameVC.gameCenterDelegate = self
gameVC.viewState = GKGameCenterViewControllerState.Leaderboards//屬性還可以是GKGameCenterViewControllerState.Achievements或是GKGameCenterViewControllerState.Challenges
self.presentViewController(gameVC, animated: true, completion: nil)
3.比對
//初始化matcheRequest
let matchRequest = GKMatchRequest()
matchRequest.minPlayers = ;
matchRequest.maxPlayers = ;
matchRequest.defaultNumberOfPlayers = ;
//通過matchRequest初始化GKMatchmakerViewController
let mmVC = GKMatchmakerViewController(matchRequest: matchRequest)
mmVC.matchmakerDelegate = self;
self.presentViewController(mmVC, animated: true, completion: nil)
接下來會通過delegate傳回GKMatch對象
func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch match: GKMatch!) {
self.dismissViewControllerAnimated(true, completion: nil)
self.match = match
match.delegate = self
if !matchStarted && match.expectedPlayerCount == {
matchStarted = true
}
}
4.通訊
//發送
let helloString = "hello"
let dataToSend = helloString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
match.sendDataToAllPlayers(dataToSend, withDataMode: GKMatchSendDataMode.Reliable, error: NSErrorPointer())
//接受通過GKMatch的delegate進行接收
func match(match: GKMatch!, didReceiveData data: NSData!, fromPlayer playerID: String!) {
if let helloString = NSString(data: data, encoding: NSUTF8StringEncoding){
println(helloString)
}else{
println("can't resolve recieved data")
}
}
文章到這裡就完了,如果要了解更多的資訊,可以參考蘋果的GameKit開發指南,如果文中有任何不對的,歡迎指正,還有就是這是我第一篇部落格的啦,如果有小錯誤的話,希望大家能多多包涵