So I'm working with multiplayer for the first time and I'm confused about the whole minplayer/maxplayer options. When I set minplayer=2 and maxplayer=4 and test the code, it connects 2 players just fine, but jumps directly into the game scene without waiting for players 3-4. How do I keep the code from progressing to the main game scene before all the slots are filled? The code works fine if I set minPlayers=maxPlayers. I know match.expectedPlayerCount==0 is supposed to fire once minPlayers is satisfied, but it isn't waiting at all for additional players to join. What am I missing here?
GKMatchRequest * matchRequest = [[[GKMatchRequest alloc] init] autorelease];
matchRequest.minPlayers = 2;
matchRequest.maxPlayers = 4;
gameCenterManager.matchController = [[GKMatchmakerViewController alloc] initWithMatchRequest:matchRequest];
gameCenterManager.matchController.matchmakerDelegate = self;
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[delegate.viewController presentViewController:gameCenterManager.matchController animated:YES completion:nil];
Find Match Code
-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{
TXGameCenterManager *gameCenterManager = [TXGameCenterManager sharedTXGameCenterManager];
gameCenterManager.multiplayerMatch = match;
// The delegate of the match is HelloWorldLayer
gameCenterManager.multiplayerMatch.delegate = self;
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[delegate.viewController dismissModalViewControllerAnimated:NO];
if( match.expectedPlayerCount==0 )
{
// Launching the game without waiting for connection change messages
NSLog(@"Begin game without waiting for match connection change messages");
// Determine the host, local or remote
NSArray * playerIds = match.playerIDs;
NSLog(@"Number of players: %d", [playerIds count]);
NSLog(@"ID of player: %@", [playerIds lastObject]);
NSLog(@"I got the player ids");
[GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError * error)
{
//bunch of code that gets player aliases and set host player
//start match
[self schedule: @selector(StartMultiplayerGame) interval:5.];
}
ChangeState code
-(void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state
{
NSArray * playerIds = [NSArray arrayWithObject:playerID];
switch (state)
{
case GKPlayerStateConnected:
// handle a new player connection.
NSLog(@"Player connected!");
[GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError * error)
{
//bunch of code that gets player aliases and set host player
if (match.expectedPlayerCount==0)
{
//start match
[self schedule: @selector(StartMultiplayerGame) interval:5.];
}
}];
break;
case GKPlayerStateDisconnected:
// a player just disconnected.
NSLog(@"Player disconnected!");
break;
}
-(void)StartMultiplayerGame
{
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}
if (match.expectedPlayerCount==0)
{
//start match
[self schedule: @selector(StartMultiplayerGame) interval:5.];
}
You said it yourself, if minPlayers players have joined (which is 2 in your case) then expectedPlayerCount is 0. So as soon as 2 players have joined, you're starting the game. This is not Game Center's fault.
You could wait for a longer amount of time once expectedPlayerCount is 0 to allow other players to join.
Your code also does not consider that a second player might join, then leave again. So in that case you would be starting the game with just one player.