【发布时间】:2011-01-01 12:58:56
【问题描述】:
编辑: 我做了一个干净的新项目,但仍然无法正常工作。请下载它,有一些代码可供查看,并且可能很容易让专业人士或任何远程体验的人看到我做错了什么。只是想发送那个整数。
http://www.2shared.com/file/fPOCLlg5/gkTest.html
嗨
我正在尝试在我的 iPhone 游戏中实现 Game Center 多人游戏,但我无法理解我手头的 Apple Docs 和第三方关于发送和接收数据的示例。
请在此处解释 Apple 官方文档中的代码示例: http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/MatchesandVoice/MatchesandVoice.html#//apple_ref/doc/uid/TP40008304-CH10-SW4
或者帮我弄清楚我提供的这个示例代码。它是一个预构建的类,用于处理所有游戏中心任务,其中用于发送和接收数据的示例如下:
- (void) sendPosition
{
NSError *error;
PositionPacket msg;
msg.messageKind = PositionMessage;
msg.x = currentPosition.x;
msg.y = currentPosition.y;
NSData *packet = [NSData dataWithBytes:&msg length:sizeof(PositionPacket)];
[match sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error:&error];
if (error != nil)
{
// handle the error
}
}
并接收:
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
Packet *p = (Packet*)[data bytes];
if (p.messageKind == PositionMessage)
// handle a position message.
}
我对官方文档中这段代码的最大疑问是:
PositionPacket/Packet 来自哪里?
并假设当你想发送/接收数据时,你会这样称呼它们:
[self sendPosition];
或
[self match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID];
我应该输入什么作为比赛、数据和玩家 ID?
例如我有一个名为 'score' 的 int,但没有我需要使用的特殊键吗?
【问题讨论】:
标签: iphone objective-c multiplayer game-center