使用 ARC 通过结构发送玩家别名(游戏中心)

发布于 2025-01-04 20:39:05 字数 294 浏览 3 评论 0原文

由于 ARC/指针,我无法将 NSString 放入我的结构中,因此将本地玩家的别名(或与此相关的任何文本)发送到另一个设备的最佳方法是什么?

到目前为止,我已经尝试转换为 &从 char 数组,使用 __unsafe_unretained- 选项并尝试创建一个类来放入文本。虽然所有这三个尝试都通过编译完成,但它们使设备崩溃(模拟器继续运行,但没有别名显示。)

正在发送文本使用ARC进行多人游戏真的很难吗?我面临的问题很可能是由于我在编程方面经验不足......所以如果有人能指出我正确的方向或为我提供一些代码片段,我将非常感激。

What is the best approach for sending the local player's alias (or any text for that matter) to another device, since I can't put an NSString in my struct because of ARC/pointers?

So far, I've tried converting to & from a char array, using the __unsafe_unretained- option and trying to create a class to put the text in. Though all three of these attempts worked through compiling, they crashed the device (simulator keeps running but no alias displays.)

Is sending text in multiplayer games really difficult when using ARC? The issues I'm facing are most likely a result of the fact that I am not very experienced at programming... so if anyone could point me in the right direction or provide me with some snips of code, I'd really appreciate it.

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

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

发布评论

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

评论(1

会发光的星星闪亮亮i 2025-01-11 20:39:05

您可以轻松地将字符串编码和解码为 NSData 对象,并通过 Game Center 发送它们。

编码:在字符串上使用此方法

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

编码:NSUTF8StringEncoding

这将返回一个 NSData 对象

解码;

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

编辑:

本质上你永远不需要显式发送玩家别名。有两种情况:

1:GKTurnBasedMatch

如果您使用这个,那么这里是如何获取所有别名(包括您的)的列表

NSMutableArray *playerIds = [NSMutableArray arrayWithCapacity:match.participants.count];

for (GKTurnBasedParticipant *part in match.participants) {

if([participant.playerID isKindOfClass:[NSString class]]){
                [playerIds addObject:part.playerID];
            }
 } //at this point you have an array full of playerID strings, next you call this:

[GKPlayer loadPlayersForIdentifiers:(NSArray *)playerIds withCompletionHandler:(void (^)      (NSArray *players, NSError *error))completionHandler {

for (GKPlayer *playa in players) {

NSLog(@"%@",playa.alias); // here i'm just logging the aliases but you can do whatever..
}


}];

2.GKMatch :这种情况更容易,因为您的 GKMatch 已经有playerIDs数组,与前:

[GKPlayer loadPlayersForIdentifiers:(NSArray *) match.playerIDs withCompletionHandler:(void (^)(NSArray *players, NSError *error))completionHandler {

//again you get the array players full of GKPlayer objects , simply pull the alias you want

}];

You can easily encode and decode strings to NSData objects and send them over Game Center.

To encode: use this method on a string

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

With encoding:NSUTF8StringEncoding

This will return a NSData object

To decode;

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Edit:

Essentially you'll never have to send player aliases explicitly. There are 2 cases:

1: GKTurnBasedMatch

If you're using this, then here's how to get a list of all of the aliases (including yours)

NSMutableArray *playerIds = [NSMutableArray arrayWithCapacity:match.participants.count];

for (GKTurnBasedParticipant *part in match.participants) {

if([participant.playerID isKindOfClass:[NSString class]]){
                [playerIds addObject:part.playerID];
            }
 } //at this point you have an array full of playerID strings, next you call this:

[GKPlayer loadPlayersForIdentifiers:(NSArray *)playerIds withCompletionHandler:(void (^)      (NSArray *players, NSError *error))completionHandler {

for (GKPlayer *playa in players) {

NSLog(@"%@",playa.alias); // here i'm just logging the aliases but you can do whatever..
}


}];

2.GKMatch : this case is much easier since your GKMatch has already playerIDs array, same as before:

[GKPlayer loadPlayersForIdentifiers:(NSArray *) match.playerIDs withCompletionHandler:(void (^)(NSArray *players, NSError *error))completionHandler {

//again you get the array players full of GKPlayer objects , simply pull the alias you want

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