Game Center matchData的良好做法 [英] Good practices for Game Center matchData

查看:106
本文介绍了Game Center matchData的良好做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GKTurnBasedMatch的新手,我正在试图找出在转弯期间玩家之间发送的matchData的良好做法。我发现的所有教程主要包括发送一串文本,我想发送更多。如果有人能给我一个更高级的教程,那就太棒了。

I am new to GKTurnBasedMatch and i'm trying to figure out what are good practices for the "matchData" sent between players during turns. All the tutorials i've found mainly cover sending a string of text and I would like to send a lot more than that. It would be great if someone could pint me to a more advanced tutorial.

我想做的一个例子是战斗。这两个玩家有他们的化身,他们有不同的细节(健康,攻击,防御等),我该如何发送这些数据?我认为可能的唯一方法是将所有匹配细节(很多)编成NSDictionary并发送,以便它们可以再次放回到自定义匹配对象中。我应该实施NSCoding吗?

An example of what I would like to do is a battle. The two players have their avatars and they have different details (health, attack, defence, etc), how should I send this data? The only way I see possible is to codify all the match details (a lot of them) into an NSDictionary and send that so that they can be put back into the custom match object again. Should I implement NSCoding?

谢谢!

推荐答案

我会实现一个类,它存储单个转弯所需的所有相关信息,并让类实现NSCoding。这意味着您可以在一个播放器的设备上将对象转换为NSData,然后将其转换回另一侧的对象。

I would implement a class that stores all the relevant information needed for a single turn and have the class implement NSCoding. This means you can convert an object to NSData on one player's device, then convert it back to an object on the other side.

本网站 http:// samsoff .es / posts / archiving-objective-c-objects-with-nscoding 有一个简单的例子可以让你前进,这里是你需要的主要方法的一个例子:

This website http://samsoff.es/posts/archiving-objective-c-objects-with-nscoding has a simple example to get you going and here is an example of the main methods you need:

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.health = [decoder decodeObjectForKey:@"health"];
        self.attack = [decoder decodeObjectForKey:@"attack"];
        isDead = [decoder decodeBoolForKey:@"isDead"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.health forKey:@"health"];
    [encoder encodeObject:self.attack forKey:@"attack"];
    [encoder encodeBool:isDead forKey:@"isDead"];
 }

将对象编码为NSData:

Encoding your object to NSData:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject: object];

转换回对象:

id *object = [NSKeyedUnarchiver unarchiveObjectWithData: inputData];

档案和序列化编程指南也是一个很好的起点。

The Archives and Serializations Programming Guide is also a great starting point.

另一种选择是使用像RestKit这样的库,它的对象映射到/来自JSON或XML。

Another option is using a library like RestKit and it's object mapping to/from JSON or XML.

这篇关于Game Center matchData的良好做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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