2011-01-09 20 views
6

cercando di implementare un multiplayer. Utilizzando il campione da Game Center - Sending and receiving data.GameCenter Invitation Handler

Tutto sembra a posto, ma in apple documentation si parla anche del gestore di inviti.

[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { 
    // Insert application-specific code here to clean up any games in progress. 
    if (acceptedInvite) { 
     GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease]; 
     mmvc.matchmakerDelegate = self; 
     [self presentModalViewController:mmvc animated:YES]; 
    } else if (playersToInvite) { 
     GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
     request.minPlayers = 2; 
     request.maxPlayers = 4; 
     request.playersToInvite = playersToInvite; 

     GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease]; 
     mmvc.matchmakerDelegate = self; 
     [self presentModalViewController:mmvc animated:YES]; 
    } 
}; 

Il problema è abbastanza semplice: non so dove aggiungere questo codice.

+0

ESATTO stessa domanda mi è venuta in mente;) \ – Mazyod

risposta

11

Come indicato nella documentazione

L'applicazione dovrebbe impostare il gestore invito il più presto possibile dopo l'applicazione viene avviata ; un luogo appropriato per impostare il gestore si trova nel blocco di completamento fornito che viene eseguito dopo che il lettore locale è autenticato.

Da qualche parte nel codice, si dovrebbe aver autenticato il giocatore locale con qualcosa di simile

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { 
    if (error == nil) { 
     // Insert your piece of code here 
    } else { 
     // Handle the error 
    } 
}]; 

Speranza che aiuta

+0

Il mio problema. Non ho letto tutto. Ci scusiamo per una domanda sciocca. – 0xDE4E15B

+1

Abbiamo fatto tutto questo, non un grosso problema :) – Jilouc

+0

Ciao. Potresti vedere la mia domanda? http://stackoverflow.com/questions/10970538/how-to-accept-an-invitation-in-game-center –

1

Il mio codice è al di sotto, e funziona molto bene. Nel authenticateLocalUser, aggiungere il codice qui sotto:

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { 
    [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { // Add for invite handler 
     // Insert application-specific code here to clean up any games in progress. 
     if (acceptedInvite) { 
      GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] ; 
      mmvc.matchmakerDelegate = self; 
      // [self presentModalViewController:mmvc animated:YES]; 
      [_delegate matchStart]; 
     } else if (playersToInvite) { 
      GKMatchRequest *request = [[GKMatchRequest alloc] init] ; 
      request.minPlayers = 2; 
      request.maxPlayers = 2; 
      request.playersToInvite = playersToInvite; 

      GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request] ; 
      mmvc.matchmakerDelegate = self; 
      // [self presentModalViewController:mmvc animated:YES]; 
      [_delegate matchStart]; 
     } 
    }; 

    [self callDelegateOnMainThread:@selector(processGameCenterAuth:) withArg:NULL error:error]; 
}];