How to accept an invitation in Game Center

后端 未结 3 1026
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 22:17

I\'m trying to implement invitations with Game Center and there\'s one thing that i don\'t understand. Ok, i\'ve sent an invitation from one device to another. Then i have a

3条回答
  •  春和景丽
    2021-01-02 22:34

    1. I register for invites as soon as the game is loaded and call the delegate when an invite is received
    2. So inviteReceived calls the match maker for dismissing the game center controller and creating the match
    3. And finally, when the match is being found, the method connectionStatusChanged takes care of presenting all the game's views and players and stuff

    Here is the code:

    I register for invites as soon as the game is loaded and call the delegate when an invite is received:

    - (void)registerInvites {
        [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
            self.pendingInvite = acceptedInvite;
            self.pendingPlayersToInvite = playersToInvite;
            [delegate inviteReceived];
        };
    }
    

    So inviteReceived calls the match maker for dismissing the game center controller and creating the match:

    - (void)inviteReceived {
        [[GCMultiplayerHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:(UIViewController*)[self.superview nextResponder] delegate:self];
    }
    
    
    - (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id)theDelegate {
        if (!gameCenterAvailable) return;
    
        matchStarted = NO;
        self.match = nil;
        self.presentingViewController = viewController;
        delegate = theDelegate;
    
        [presentingViewController dismissModalViewControllerAnimated:YES];
        GKMatchmakerViewController *mmvc;
    
        if (pendingInvite != nil) {
    
            mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];
    
        } else {
    
            GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
            request.minPlayers = minPlayers;     
            request.maxPlayers = maxPlayers;
            request.playersToInvite = pendingPlayersToInvite;
    
            mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];    
    
        }
    
        mmvc.matchmakerDelegate = self;
        [presentingViewController presentModalViewController:mmvc animated:YES];
    
        self.pendingInvite = nil;
        self.pendingPlayersToInvite = nil;
    }
    

    And finally, when the match is been found, the method connectionStatusChanged takes care of presenting all the game's views, players and starting the match:

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)theMatch
    {
        self.match = theMatch;
        match.delegate = self;
        if (!matchStarted && match.expectedPlayerCount == 0) {
            NSLog(@"Ready to start match! - didFindMatch");
            [presentingViewController dismissModalViewControllerAnimated:YES];
    
            [self.delegate connectionStatusChanged:CONNECTIONSUCCESS];
        }
    }
    

提交回复
热议问题