我今晚刚刚在我的游戏中使用了这个。您需要进行更多协商才能设置通信通道。返回给邀请者的初始比赛正在等待被邀请者响应......这是我只有两个玩家的过程。以下是我的通信启动正在执行的所有步骤。显然,这里没有包含真正的错误处理:
首先,验证您的播放器
二、认证后立即设置inviteHandler。像这样的东西:
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite* acceptedInvite, NSArray *playersToInvite)
{
if(acceptedInvite != nil)
{
// Get a match for the invite we obtained...
[[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^(GKMatch *match, NSError *error)
{
if(match != nil)
{
[self disconnectMatch];
// Record the new match...
self.MM_gameCenterCurrentMatch = match;
self.MM_gameCenterCurrentMatch.delegate = self;
}
else if(error != nil)
{
NSLog(@"ERROR: From matchForInvite: %@", [error description]);
}
else
{
NSLog(@"ERROR: Unexpected return from matchForInvite...");
}
}];
}
};
第三,获取您的好友 playerIds 列表(不是别名)。
第四,设置你的 GKMatchRequest 像这样......我只邀请一个朋友:
// Initialize the match request - Just targeting iOS 6 for now...
GKMatchRequest* request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = [NSArray arrayWithObject:player.playerID];
request.inviteMessage = @"Let's play!";
// This gets called when somebody accepts
request.inviteeResponseHandler = ^(NSString *playerID, GKInviteeResponse response)
{
if (response == GKInviteeResponseAccepted)
{
//NSLog(@"DEBUG: Player Accepted: %@", playerID);
// Tell the infrastructure we are don matching and will start using the match
[[GKMatchmaker sharedMatchmaker] finishMatchmakingForMatch:self.MM_gameCenterCurrentMatch];
}
};
五、使用请求调用 findMatchForRequest:withCompletionHandler: 类似这样的...
[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch* match, NSError *error) {
if (error)
{
NSLog(@"ERROR: Error makeMatch: %@", [error description] );
[self disconnectMatch];
}
else if (match != nil)
{
// Record the new match and set me up as the delegate...
self.MM_gameCenterCurrentMatch = match;
self.MM_gameCenterCurrentMatch.delegate = self;
// There will be no players until the players accept...
}
}];
第六,这会将请求发送给其他玩家,如果他们接受来自第二步的“inviteHandler”,则会被调用。
第七,第二步的“inviteHandler”得到GKInvite的匹配!
第八,第四步中的“inviteeResponseHandler”被调用,从而完成了比赛!
第九,从 GKMatchDelegate 创建一个 didChangeState 来处理比赛的结束。像这样的东西:
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state{
switch (state)
{
case GKPlayerStateConnected:
// Handle a new player connection.
break;
case GKPlayerStateDisconnected:
// A player just disconnected.
break;
}
if (!self.matchStarted && match.expectedPlayerCount == 0)
{
self.matchStarted = YES;
// Handle initial match negotiation.
if (self.iAmHost && !self.sentInitialResponse)
{
self.sentInitialResponse = true;
// Send a hello log entry
[self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for accepting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
}
}}
第十,这是我的sendMessage:
- (void) sendMessage:(NSString*)action toPlayersInMatch:(NSArray*) playerIds{
NSError* err = nil;
if (![self.MM_gameCenterCurrentMatch sendData:[action dataUsingEncoding:NSUTF8StringEncoding] toPlayers:playerIds withDataMode:GKMatchSendDataReliable error:&err])
{
if (err != nil)
{
NSLog(@"ERROR: Could not send action to players (%@): %@ (%d) - '%@'" ,[playersInMatch componentsJoinedByString:@","],[err localizedDescription],[err code], action);
}
else
{
NSLog(@"ERROR: Could not send action to players (%@): null error - '%@'",[playersInMatch componentsJoinedByString:@","], action);
}
}
else
{
NSLog(@"DEBUG: Message sent to players (%@) - '%@'",[playersInMatch componentsJoinedByString:@","], action);
}}
第十一,从 GKMatchDelegate 创建一个 didReceiveData,如下所示:
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID{
NSString* actionString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
// Send the initial response after we got the initial send from the
// invitee...
if (!self.iAmHost &&!self.sentInitialResponse)
{
self.sentInitialResponse = true;
// Send a hello log entry
[self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for inviting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
}
// Execute the action we were sent...
NSLog(actionString);}
第十二……嗯,现在你已经建立并运行了沟通渠道……做任何你想做的事……