Game Center : match delegate’s not called after finding a match
Asked Answered
W

2

6

I'm trying to use game center : multi player

Till now, players are Authenticating to Game center, they can send/read scores, and acheivements. For multiplayer features, I tried both methods : - using Game center interface to find a match. - Find a Match Programmatically.

For both ways I have the following issue: the match delegate’s match:player:didChangeState: method is not called. In apple docs, it's stated that this delegate is called if one player is connected or disconnected.

In my case this delegate is never called. I think that I'm missing a step. here after the implementation of my delegate (as specified in apple doc).

- (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.
    }
}

and also the code to find a match.

-(void) findProgrammaticMatch
{
  GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
  request.minPlayers = 2;
  request.maxPlayers = 2;

  [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request
                                 withCompletionHandler:^(GKMatch *FoundMatch, NSError *error)
  {
    if (error)
    {
      // Process the error.
      StatusLabel.text = @"Match Not Found";
    }
    else if (FoundMatch != nil)
    {
      MultiPlayerMatch = FoundMatch; // Use a retaining property to retain the match.
      StatusLabel.text = @"Match Found";
      MultiPlayerMatch.delegate = self; // start!
      // Start the match.
      // Start the game using the match.
      [self StartMatch];
    }
  }];
}

Thanks for your help.

Woodhouse answered 25/1, 2011 at 13:51 Comment(0)
F
2

It was working all along. The only difference is that... when you use invites the event "didChangeState" doesn't get called. You're connected without notice and you can start to receive data. I never tried to send/receive data because I was expecting the event first, but i did send something by mistake one time, and it worked.

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *) match {    
    //Dismiss window
    [self dismissModalViewControllerAnimated:YES];

    //Retain match
    self.myMatch = match; 

    //Delegate
    myMatch.delegate = self;


    //Flag
    matchStarted = TRUE;

   //Other stuff
}

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state  {
    //This code gets called only on auto-match
}

The above code works as expected.

Fleabane answered 16/6, 2011 at 13:47 Comment(1)
I don't beleive this is true. I just tested with auto match and didChangeState never fires. Only didFindMatch. I think didChangeState may only happen if a player enters GKPlayerStateUnknown and then comes back, or if they are added to a match in progress.Fives
F
0

I think didChangeState: GKPlayerStateConnected may only happen if a player was GKPlayerStateUnknown and then comes back, or if they are added/invited back to a match in progress.

Fives answered 2/12, 2014 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.