Search code examples
iphoneobjective-cipadgame-center

Game Center - Connecting for Multiplayer


I have been trying to implement Game Center multiplayer and struggling to get anything beyond a GKMatchViewController.

I have two views, one is my main menu, this is where multiplayer is launched and the player gets the GKMatchViewController. Then behind this, the user doesn't know it but the view changes to the multiplayer view where they actually play so when the GKMatchViewController is dismissed they are in the game view rather than menu.

Here I launch multiplayer (yes I am using cocos2d):

-(void)mpGo:(id)sender{
    GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
    request.minPlayers = 2;
    request.maxPlayers = 2;

    [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request
                                   withCompletionHandler:^(GKMatch *returnedMatch, NSError *error) 
     {
         if (error) NSLog(@"match error: %@", error);
         else if (returnedMatch != nil)
         {
             match = [returnedMatch retain]; 
             match.delegate = self; // start!
         }
     }];

    tempVC = [[UIViewController alloc] init];

    GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
    if (mmvc != nil)
    {
        mmvc.matchmakerDelegate = self;
        [[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
        [tempVC presentModalViewController:mmvc animated: YES];
    }

    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[mpView node] withColor:ccWHITE]];
}

When pressing 'Play Now' I get this error:

match error: Error Domain=GKErrorDomain Code=2 "The requested operation has been cancelled." UserInfo=0x2248d0 {NSLocalizedDescription=The requested operation has been cancelled.}

Then it just stays on that 'Finding Players...' view.


Solution

  • I never done something with GameCenter but maybe I can help anyway. When I get the error

    "The requested operation has been cancelled"

    in reverse geocoder I have figured out, that the app had not the time to perform the request. You get the error by executing findMatchForRequest:, so maybe your request variable is not set completely, check that. You can also try to put a NSLog() in first line (in the block) and look in your passed variable returnedMatch and error