游戏中心多人游戏代码

发布于 2024-10-21 18:27:55 字数 554 浏览 0 评论 0原文

根据文档,我正在使用:

- (void) match:(GKMatch*) match 玩家:(NSString*) 玩家ID didChangeState:(GKPlayerConnectionState) state;

执行初始游戏协商。我在以下范围内执行此操作:

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

我需要决定哪个设备负责设置游戏。我通过对 match.playerIDs NSArray 实例进行排序,并将 [GKLocalPlayer localPlayer].playerID NSString 与排序数组索引 0 处的playerID NSString 进行比较来实现此目的。该玩家创建游戏,将数据发送给所有玩家。

然而,即使预期玩家计数为 0,playerIDs 数组此时也有零个条目,导致数组溢出。这是为什么?我应该怎么做才能明确选择玩家来生成游戏?

As per the docs, I'm using:

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

to carry out the initial game negotiation. I do this in the scope of:

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

I need to decide which device is responsible for setting up the game. I do this by sorting the match.playerIDs NSArray instance and comparing the [GKLocalPlayer localPlayer].playerID NSString to the playerID NSString at index 0 of the sorted array. This player creates the game, sending out the data to all players.

However, and even with an expectedPlayerCount of 0, the playerIDs array has zero entries at this point, giving me an array overrun. Why is that? And what should I do instead to make a well-defined choice of player to generate the game?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2024-10-28 18:27:55

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

For the decision code, take a look at the GKTank example provided by Apple - they take a hash of the device id, send it to the other client, and whichever has the lower number is the "host". That seems like a pretty solid way to decide.

暮年 2024-10-28 18:27:55

执行该操作的示例代码

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

here is sample code to do that thing

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

now in delegate Function

- (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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文