iOS游戏中心GameKit程序化邀请配对

iOS游戏中心GameKit程序化邀请配对,第1张

概述我正在尝试使用自定义UI(没有GKMatchMakerViewController)实现实时多人游戏.我正在使用startBrowsingForNearbyPlayersWithReachableHandler:^(NSString * playerID,BOOL可达)来查找本地播放器,然后使用GKMatchmaker单例(我已经启动)发起匹配请求. 这是我遇到麻烦的地方.当我发送请求时,完成处理 我正在尝试使用自定义UI(没有gkmatchmakerVIEwController)实现实时多人游戏.我正在使用startbrowsingForNearbyPlayersWithReachableHandler:^(Nsstring * playerID,BOol可达)来查找本地播放器,然后使用gkmatchmaker单例(我已经启动)发起匹配请求.

这是我遇到麻烦的地方.当我发送请求时,完成处理程序几乎立即触发,没有错误,并且它返回的匹配具有预期的玩家计数为零.同时,其他玩家肯定没有回复该请求

相关守则:

- (voID) findMatch {  GKMatchRequest *request = [[GKMatchRequest alloc] init];  request.minPlayers = NUM_PLAYERS_PER_MATCH; //2  request.maxPlayers = NUM_PLAYERS_PER_MATCH; //2  if (nil != self.playersToInvite) {    // we always successfully get in this if-statement    request.playersToInvite = self.playersToInvite;    request.inviteeResponseHandler = ^(Nsstring *playerID,GKInviteeResponse response) {      [self.delegate updateUIForPlayer: playerID accepted: (response == GKInviteeResponseAccepted)];  };}request.inviteMessage = @"Let's Play!";[self.matchmaker findMatchForRequest:request withCompletionHandler:^(GKMatch *match,NSError *error) {  if (error) {    // Print the error    NSLog(@"%@",error.localizedDescription);  } else     if (match != nil) {      self.currentMatch = match;      self.currentMatch.delegate = self;      // All players are connected      if (match.expectedplayerCount == 0) {        // start match        [self startMatch];      }        [self stopLookingForPlayers];      }  }];}

我从上一个问题(iOS Gamecenter Programmatic Matchmaking)中知道我需要包含这个:

- (voID)matchForInvite:(GKInvite *)invite completionHandler:(voID (^)(GKMatch *match,NSError *error))completionHandler

在上面的代码,但我不知道应该包括在哪里.我已经尝试过GKMatchRequest inviteeResponseHandler和matchmaker finMatchForRequest:withCompletionHandler都无济于事.发生的行为是匹配器立即返回匹配(甚至在被邀请者被邀请之前),并且即使在被邀请者点击匹配邀请之后也不会调用matchRequest inviteeResponseHandler.

有人可以就此提出建议吗?谢谢.

…吉姆

解决方法 今晚我的比赛正在进行中.为了获得通信通道设置,您需要进行更多的协商.返回给邀请者的初始匹配正在等待被邀请者回复…这是我的过程只有两个玩家.以下是我的通信启动执行的所有步骤.显然,此处不包含真正的错误处理:

首先,验证您的播放器

第二,在认证设置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 acceptsrequest.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 - '%@'",action);    }}else{    NSLog(@"DEBUG: Message sent to players (%@) - '%@'",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,thanks for inviting,self.MM_gameCenterLocalPlayer.alias] toplayersInMatch: [NSArray arrayWithObject:playerID]];}// Execute the action we were sent...NSLog(actionString);}

第十二……好了,现在你已经开始运行了沟通渠道……做你想做的事……

总结

以上是内存溢出为你收集整理的iOS游戏中心GameKit程序化邀请配对全部内容,希望文章能够帮你解决iOS游戏中心GameKit程序化邀请配对所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102064.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存