我的项目需要在线播放。
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视图,如果按下名为“立即播放”的按钮,它将调用“GKMatchDelegate的[ - (void)匹配:( GKMatch *)theMatch player :( NSString *)playerID didChangeState:(GKPlayerConnectionState)国家]“,它的工作正常。
但现在,我需要使用“以编程方式查找匹配项”,直接在官方视图中查找匹配项。
[[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)匹配:( 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;
}
}