游戏中心 - 发送和接收数据 [英] Game Center - Sending and receiving data

查看:194
本文介绍了游戏中心 - 发送和接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:
我做了一个干净的新项目,但仍然无法使其正常运行。请下载它,有一些代码可供查看,可能很容易让专业人士或任何人远程体验,看看我做错了什么。只是试图发送该整数。



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.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(错误!=无)
{
//处理错误
}
}

并且收到:

   - (无效)匹配:(GKMatch * )匹配didReceiveData :( NSData *)数据fromPlayer:(NSString *)playerID 
{
Packet * p =(Packet *)[data bytes];
if(p.messageKind == PositionMessage)
//处理位置消息。
}

关于此代码的重要问题来自官方文档:



PositionPacket / 数据包来自哪里?
并且假设您想要发送/接收数据时,您可以这样称呼它们:

  [self sendPosition]; 

  [self match:(GKMatch *)matchReceiveData:(NSData *)data fromPlayer:(NSString *)playerID]; 

我输入什么作为匹配,数据和玩家ID?



例如我有一个名为'得分'的int,但是我没有需要使用的特殊键吗?

解决方案

在这个例子中, PositionPacket只是一个结构。接下来的行然后将该结构放入一个NSData,它只是一个字节桶对象。

  NSData * packet = [NSData dataWithBytes:& msg length:sizeof(PositionPacket)]; 

所以如果你只是想发送一个int分数,你可以使用这样的sendScore方法:

   - (void)sendScore 
{
NSError * error;
int myScore = self.score;
NSData * packet = [NSData dataWithBytes:& myScore length:sizeof(myScore)];
[match sendDataToAllPlayers:packet withDataMode:GKMatchSendDataUnreliable error:& error];
if(错误!=无)
{
//处理错误
}
}

通常,您需要一个结构,以便有一些额外的信息可以让接收者知道它是什么类型的数据。在这个例子中,这就是这一行的目的:

  msg.messageKind = PositionMessage; 

通常,您可以发送任何您想要封装在NSData对象中的内容,因为它只是一个字节桶。您可以像示例中那样发送诸如整数或结构的原始类型,甚至是NSObject(只要它们实现NSCoding)。您应该阅读NSKeyedArchiver,NSCoding和NSData,以获取有关以这种方式发送和接收NSObject的更多信息。以下是Apple关于 Archving 的参考文档。



至于接收,你不会调用方法,它会被Kit调用。这就是所谓的委托方法(用Cocoa说法)或回调方法。您可以将其视为您的应用可以异步接收的电话。通过实现带签名的方法:

   - (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID; 

...你说我可以收到这种电话。因此,当GameKit代表您从另一个玩家接收数据时,它会看到您希望接收该类型的回调,然后将调用该方法 - 然后由您根据收到的内容更新您的内部应用程序状态。



继续这个例子,如果你发送了上面描述的简单的只有整数消息,你可以像这样实现这个方法:

   - (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID 
{
int * receivedScorePtr =(int *)[data bytes];
int receivedScore = * receivedScorePtr;
[self updateScore:收到forPlayer:playerID];
}

当然,假设您有一个名为updateScore的方法:forPlayer :这将更新分数表。



您可以在此博客条目中找到有关委托和委托方法如何工作的更一般性讨论/解释: http://mohrt.blogspot.com/2010/01/cocoa-and-delegates.html



ADDED:使用提问者发布的代码,我做了一些修改并制作了一个适用于这个裸骨的版本用例。 测试应用程序的工作版本通过GameCenter发送一个整数我没有声明代码的质量,或其适用性,以及任何东西。我没有写99.9% - 请不要把我在这里发布的内容作为对其中出现的任何内容的认可。



吸取的一个教训(即我不知道,所以我放在这里,希望它能帮助其他人)是你不能在模拟器中使用Matchmaking服务。这意味着您需要两个开发配置的iOS设备才能测试这种情况,对于非平凡的程序,可能需要两台开发机器来同时调试这两个设备。在解决这个问题时,这个问题花费了我最多的时间。


EDIT: I have made a clean, new project, but still can't get it working. Please download it, there is a little code to look at and probably easy for a professional or anyone remotely experience to see whats I am doing wrong. Just trying to send that integer.

http://www.2shared.com/file/fPOCLlg5/gkTest.html

Hi

I am trying to implement Game Center multiplayer in my iphone game and having trouble understanding the samples I have at hand in the Apple Docs and from third parties concerning sending and receiving data.

Could someone please explain the code samples in the Official Apple docs here please: http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/MatchesandVoice/MatchesandVoice.html#//apple_ref/doc/uid/TP40008304-CH10-SW4

Or help me figure out this sample code I was supplied with. Its a prebuilt class, made to handle all the game center tasks and a sample from it for sending and receiving data would be this:

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

And receiving:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
    Packet *p = (Packet*)[data bytes];
    if (p.messageKind == PositionMessage)
        // handle a position message.
}

My big question about this code form the official docs is:

Where does PositionPacket/Packet come from? And assuming when you want to send/receive data you call them like so:

[self sendPosition];

or

[self match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID];

What do I enter as the match, data and playerID?

E.g. I have an int named 'score' but is there not a special key I need to use something?

解决方案

In this example, the PositionPacket is just a struct. The following line then puts that struct into an NSData which is just a "byte bucket" object.

NSData *packet = [NSData dataWithBytes: &msg length: sizeof(PositionPacket)];

So if you just wanted to send an int score, you could have a sendScore method that looked like this:

- (void) sendScore
{
    NSError *error;
    int myScore = self.score;
    NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
    [match sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error: &error];
    if (error != nil)
    {
        // handle the error
    }
}

Typically, you'll want a struct so that there's some additional information that lets the receivers know what kind of data it is. In the example, that would have been the purpose of this line:

msg.messageKind = PositionMessage;

In general, you can send anything you want encapsulated in an NSData object, since it's just a byte bucket. You can send primitive types like ints, or structs as in the example, or even NSObjects (as long as they implement NSCoding). You should read up on NSKeyedArchiver, NSCoding, and NSData for more information on sending and receiving NSObjects in this way. Here is Apple's reference document on Archving.

As for receiving, YOU don't call the method, it gets called ON you by the Kit. It's what's called a "delegate method" (in Cocoa parlance) or a "callback method." You can think of it like a "phone call" that your app can receive asynchronously. By implementing a method with the signature:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID;

...you are saying "I can receive this kind of phone call." So when the GameKit receives data on your behalf from another player, it will see that you want to receive callbacks of that kind and will then call that method -- then it's up to you to update your internal application state based on what is received.

To continue with this example, if you had sent the simple "nothing but an integer" message described above, you might implement that method like this:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
    int* receivedScorePtr = (int*)[data bytes];
    int receivedScore = *receivedScorePtr;
    [self updateScore: received forPlayer: playerID];
}

That is, of course, assuming that you have a method called updateScore:forPlayer: that would update a table of scores.

You can find a more general discussion/explanation of how delegates and delegate methods work at this blog entry: http://mohrt.blogspot.com/2010/01/cocoa-and-delegates.html

ADDED: Using code the asker posted, I made a few modifications and produced a version that "works" for this bare bones use case. Working Version of Test App To Send One Integer Through GameCenter I make no claims about the quality of the code, or its suitability for, well, anything at all. I didn't write 99.9% of it - please don't take my posting of it here as an endorsement of anything that appears in it.

One lesson learned (that I didn't know, so I'm putting here in hopes that it helps others) is that you can't use the Matchmaking service with the simulator. This means that you need two development-provisioned iOS devices in order to test this scenario, and likely, for non-trivial programs, two development machines to debug both devices simultaneously. This problem cost me the most time while figuring this out.

这篇关于游戏中心 - 发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆