2013-03-25 12 views
7

मैं एक कस्टम यूआई (कोई GKMatchMakerViewController) के साथ एक वास्तविक समय मल्टीप्लेयर गेम को लागू करने की कोशिश कर रहा हूं। मैं StartBrowsingForNearbyPlayersWithReachableHandler का उपयोग कर रहा हूं:^(एनएसएसटींग * प्लेयरआईडी, बूल पहुंचने योग्य) स्थानीय खिलाड़ी खोजने के लिए, और उसके बाद जीकेमैचमेकर सिंगलटन (जिसे मैंने पहले ही शुरू किया है) के साथ एक मैच अनुरोध शुरू किया है।आईओएस गेम सेंटर गेमकिट प्रोग्रामेटिक आमंत्रण मैचमेकिंग

यहां मुझे परेशानी हो रही है। जब मैं एक अनुरोध भेजता हूं, तो समापन हैंडलर बिना किसी त्रुटि के लगभग तुरंत आग लग जाता है, और जिस मैच में यह लौटाता है वह शून्य की अपेक्षित खिलाड़ी गणना करता है। इस बीच, अन्य खिलाड़ी निश्चित रूप से अनुरोध करने के लिए जवाब नहीं दिया है

प्रासंगिक कोड:

- (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 आमंत्रितeResponseHandler दोनों की कोशिश की है, और matchmaker finMatchForRequest में: कॉम्प्लेशन हैंडलर के साथ कोई फायदा नहीं हुआ। ऐसा व्यवहार होता है कि मेलमेकर तुरंत एक मैच देता है (यहां तक ​​कि आमंत्रित करने से पहले भी आमंत्रित किया जाता है) और matchRequest आमंत्रितeResponseHandler को कभी भी आमंत्रित नहीं किया जाता है जब आमंत्रण मैच आमंत्रण को टैप करता है।

क्या कोई इस पर सलाह दे सकता है? धन्यवाद।

... जिम

+0

आप कैसे शुरू किया BrowsingForNearbyPla yersWithReachable हैंडलर काम करने के लिए? मुझे इससे कोई कॉलबैक नहीं मिला ?? – bobmoff

उत्तर

15

मुझे यह आज रात मेरे खेल पर काम कर रहा है। संचार चैनल सेटअप प्राप्त करने के लिए आपको और अधिक बातचीत करने की आवश्यकता है। आविष्कार में लौटा प्रारंभिक मैच आमंत्रित करने के लिए आमंत्रित करने का इंतजार कर रहा है ... यहां केवल दो खिलाड़ियों के साथ मेरी प्रक्रिया है। मेरे संचार स्पिन-अप प्रदर्शन करने वाले सभी चरण यहां दिए गए हैं। जाहिर है, कोई वास्तविक त्रुटि हैंडलिंग यहाँ शामिल:

सबसे पहले, सही प्रमाणीकरण सेट 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 (नहीं उर्फ) की अपनी सूची प्राप्त करें।

// 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... 
    } 
}]; 

चौथा, सेटअप इस तरह से अपनी GKMatchRequest कुछ ... मैं केवल एक दोस्त को आमंत्रित कर रहा हूँ

छठा, यह दूसरे खिलाड़ी से अनुरोध भेजता है और यदि वे दूसरे चरण से "आमंत्रण हैंडलर" को स्वीकार करते हैं।

सातवां, दूसरे चरण से "आमंत्रण हैंडलर" जीकेइन्वाइट के लिए मैच प्राप्त करता है!

आठवां, चौथे चरण से "आमंत्रण रिस्पॉन्स हैंडलर" कहलाता है जिसने मैच समाप्त किया!

नौवां, मैच के अंतिम रूप को संभालने के लिए जीकेमैचडिलेगेट से किया गया चेंजस्टेट बनाएं।कुछ इस तरह:

- (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); 
}} 

ग्यारहवीं, इस तरह की एक didReceiveData GKMatchDelegate से कुछ बनाना:

- (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)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);} 

बारहवीं ... खैर अब आप संचार चैनल ऊपर और चल रहे हैं ... जो भी आप चाहते हैं ...

+0

अच्छा जवाब !!!!! –

+0

@ GoRose-Hulman क्या आप आईओएस 7 के लिए अपना उत्तर अपडेट कर सकते हैं? –

+0

क्या आपको आईओएस 9 में ऐसा करने का सही तरीका पता है? मैंने अभी एक समान प्रश्न पूछा है: http://stackoverflow.com/questions/36728503/gkmatchmaker-findmatchforrequest-invite-never-received – mark

संबंधित मुद्दे