2

我的项目需要在线播放。

    GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
    request.minPlayers = 2;     
    request.maxPlayers = 2;        

    GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
    mmvc.matchmakerDelegate = self;        
    [presentingViewController presentModalViewController:mmvc animated:YES];

上面的代码创建了一个 GameCenter 视图,如果我按下名为“Play now”的按钮,它将调用“GKMatchDelegate 的 [- (void)match:(GKMatch *)theMatch player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state]” ,它的工作正常。

但是现在,我需要使用“以编程方式查找匹配项”,直接查找匹配项而无需官方视图。

[[GKMatchmaker sharedMatchmaker]findMatchForRequest:request withCompletionHandler:^(GKMatch *hasmatch, NSError *error) {
        if(error)
        {
            NSLog(@"has error match!!!");
        }
        else if(hasmatch)
        {
            NSLog(@"has match!!!");//in test we really find the match.
            [presentingViewController dismissModalViewControllerAnimated:YES];
            self.match = hasmatch;
            self.match.delegate = self;
        }
    }];

现在“GKMatchDelegate 的 [- (void)match:(GKMatch *)theMatch player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state]”不起作用。

我应该怎么做,开始在线比赛?

- (void)match:(GKMatch *)theMatch player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {

if (self.match != theMatch) return;

switch (state) {
    case GKPlayerStateConnected: 
        // handle a new player connection.
        NSLog(@"Player connected!");

        if (!matchStarted && theMatch.expectedPlayerCount == 0) {
            NSLog(@"Ready to start match!");
            [self lookupPlayers];//i start game here
        }

        break; 
    case GKPlayerStateDisconnected:
        // a player just disconnected. 
        NSLog(@"Player disconnected!");
        matchStarted = NO;
        [delegate matchEnded];
        break;
}                 

}

4

0 回答 0