【问题标题】:Game Center multiplayer code游戏中心多人游戏代码
【发布时间】:2011-03-10 04:34:52
【问题描述】:

根据文档,我正在使用:

- (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 进行比较。该玩家创建游戏,将数据发送给所有玩家。

但是,即使 expectedPlayerCount 为 0,playerIDs 数组此时也有零条目,这给了我一个数组溢出。这是为什么?而我应该怎么做才能明确选择玩家来生成游戏?

【问题讨论】:

    标签: iphone game-center


    【解决方案1】:

    对于决策代码,请查看 Apple 提供的 GKTank 示例 - 他们获取设备 id 的哈希值,将其发送到其他客户端,并且具有较低数字的那个是“主机”。这似乎是一种非常可靠的决定方式。

    【讨论】:

      【解决方案2】:

      这是执行该操作的示例代码

      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;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 1970-01-01
        • 2015-10-20
        相关资源
        最近更新 更多