游戏中心多人游戏代码
根据文档,我正在使用:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于决策代码,请查看 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.
执行该操作的示例代码
这是现在在委托函数中
here is sample code to do that thing
now in delegate Function