为什么GameCenter不能等待足够长的时间来填充所有玩家位置?

时间:2012-11-01 02:36:00

标签: iphone objective-c ipad cocos2d-iphone multiplayer

所以我第一次使用多人游戏,我对整个minplayer / maxplayer选项感到困惑。当我设置minplayer = 2和maxplayer = 4并测试代码时,它可以很好地连接2个玩家,但直接跳入游戏场景而无需等待玩家3-4。在填充所有插槽之前,如何防止代码进入主游戏场景?如果我设置minPlayers = maxPlayers,代码工作正常。我知道match.expectedPlayerCount == 0应该在minPlayers满意时触发,但它并没有等待其他玩家加入。我在这里缺少什么?

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];

查找匹配代码

-(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代码

-(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]];   
}

1 个答案:

答案 0 :(得分:1)

 if (match.expectedPlayerCount==0)
 {
      //start match
      [self schedule: @selector(StartMultiplayerGame) interval:5.];
 }

你自己说过,如果minPlayers玩家已加入(在你的情况下为2),那么expectedPlayerCount为0.所以只要有2名玩家加入,你就开始游戏了。这不是游戏中心的错。

如果expectedPlayerCount为0,您可以等待更长的时间,以允许其他玩家加入。

您的代码也不会考虑第二个玩家可能加入,然后再离开。因此,在这种情况下,你只需要一名玩家即可开始游戏。