0

だから私は初めてマルチプレイヤーで作業していて、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]];   
}
4

1 に答える 1

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

minPlayersプレーヤーが参加している場合(あなたの場合は2)、expectedPlayerCountは0です。したがって、2人のプレーヤーが参加するとすぐに、ゲームを開始します。これはゲームセンターのせいではありません。

expectedPlayerCountが0になったら、他のプレーヤーが参加できるようになるまで、より長い時間を待つことができます。

あなたのコードはまた、2番目のプレーヤーが参加して再び去る可能性があることを考慮していません。したがって、その場合は、1人のプレーヤーでゲームを開始することになります。

于 2012-11-01T10:31:36.580 に答える