ios 使用 Game Center 向其他玩家发送文本聊天消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12603585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending text chat msg to other player with Game Center?
提问by Phil
Is it possible to send text chat msgs to other players via Game Center? is their any dedicated way to do that with the GameKit API? or would it just have to be put into the turn data that is sent between players?
是否可以通过 Game Center 向其他玩家发送文字聊天消息?他们有什么专门的方法可以用 GameKit API 做到这一点吗?还是只需要将其放入玩家之间发送的回合数据中?
回答by nycynik
You have to write your own, there is no method in game center to allow for chatting.
您必须自己编写,游戏中心没有允许聊天的方法。
回答by emreoktem
You can just send the text as normal data during the game. In order to do that
您可以在游戏过程中将文本作为普通数据发送。为了做到这一点
Method to prepare data to send
准备要发送的数据的方法
-(void)sendText:(NSString *) text {
NSString * text2Send = [NSString stringWithFormat:@"%@", text];
[self sendData:[text2Send dataUsingEncoding:NSUTF8StringEncoding]];
}
The send data method will be the normal data sending method of Game center as
发送数据方法将是游戏中心的正常数据发送方法,如
- (BOOL)sendDataToAllPlayers:(NSData *)data withDataMode:(GKMatchSendDataMode)mode error:(NSError **)error;
and in order to resolve the received data
并且为了解析接收到的数据
NSString * rawText = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
if(rawText.length > 0)
{
//Do what ever you want with the text
}
回答by kervich
Like already proposed, you can use GKTurnBasedMatch
's matchData
for that. If your game is turn-based, you can have two game data message types: one for sending game data at the end of each player's turn, and the other for sending chat messages. Or you can combine them so that a message is sent when each player ends her turn. However if you only use matchData
to send chat messages, make sure it doesn't end player's turn, otherwise you will have game synchronization issues. For that use saveCurrentTurnWithMatchData:completionHandler:
method for sending your message; on the other side GameKit will call your turn callback as it receives the message - you should read the updated matchData
and see whether it is a message or a game state update - that's relatively easy if you use JSON or XML or NSDictionary serialization for sending data back and forth - you can introduce something like dataType
property there that would let you distinguish between text message and game state update.
就像已经提出的那样,您可以使用GKTurnBasedMatch
's matchData
。如果您的游戏是回合制的,您可以有两种游戏数据消息类型:一种用于在每个玩家的回合结束时发送游戏数据,另一种用于发送聊天消息。或者您可以将它们组合起来,以便在每个玩家结束她的回合时发送一条消息。但是,如果您仅用于matchData
发送聊天消息,请确保它不会结束玩家的回合,否则您将遇到游戏同步问题。对于saveCurrentTurnWithMatchData:completionHandler:
发送消息的使用方法;另一方面,GameKit 会在收到消息时调用您的轮回回调 - 您应该阅读更新的matchData
并查看它是消息还是游戏状态更新 - 如果您使用 JSON 或 XML 或 NSDictionary 序列化来回发送数据,这相对容易 - 您可以dataType
在那里引入类似属性的东西,让您区分文本消息和游戏状态更新。
There's also an option for changing the match.message
contents, however if you put your short message there, the opponent(s) will only see it when they receive 'your turn' notification from the GameCenter.
还有一个更改match.message
内容的选项,但是如果你把你的短消息放在那里,对手只有在他们收到来自游戏中心的“轮到你”通知时才会看到它。