2011-03-10 60 views
0

按照該文檔中,我使用:遊戲中心多人代碼

- (void) match:(GKMatch*) match player:(NSString*) playerID didChangeState:(GKPlayerConnectionState) state;

來進行最初的遊戲協商。我這樣做的能力範圍:

if (matchStarted_ == NO && [match expectedPlayerCount] == 0) { ... }

我需要決定哪些設備是負責建立遊戲。我通過排序match.playerIDs NSArray的實例和[GKLocalPlayer localPlayer].playerID的NSString在排序後的數組的索引0比較到playerID的NSString做到這一點。該玩家創建遊戲,並將數據發送給所有玩家。

然而,甚至爲0的expectedPlayerCount,該playerIDs陣列在這一點上的零項,給我一個數組溢出。這是爲什麼?我該怎麼做才能做出明確的選擇來生成遊戲?

回答

2

對於決策代碼,看看蘋果公司提供的GKTank例子 - 他們需要的設備ID的哈希值,將其發送到其他客戶端,並取其具有較低的號碼是「主」。這似乎是一個非常堅實的決定方式。

0

這裏是示例代碼做那件事

NSString *uid = [[UIDevice currentDevice] uniqueIdentifier]; 
CoinTossID = [uid hash]; 

現在委託功能

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID 
    { 
     NSMutableArray *ReceivedArray = [[NSMutableArray alloc] init]; 
     ReceivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
     int flag = [[ReceivedArray objectAtIndex:0] intValue]; 
     [ReceivedArray removeObjectAtIndex:0]; 

     int CoinValue = [ReceivedCoinTossID intValue]; 
     if(CoinValue > CoinTossID) 
     { 
      isPlayer1 = YES; 
     } 
     else 
     { 
       isPlayer1 = NO; 
     } 
    }