2011-03-10 88 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; 
     } 
    }