4

嗨,我是 iOS 游戏中心的新手。我正在尝试将多人游戏功能添加到我的游戏中并遵循文档。

到目前为止,我的两个客户可以成功获得匹配,即调用 matchmakerViewController:didFindMatch 回调并传递 GKMatch 对象。

但是在那之后我似乎永远被困在那里,因为根据文档,我必须等到所有玩家(在我的情况下为 2 个)真正连接后才能开始我的游戏。但似乎从未调用 match:player:didChangeState 回调来指示连接成功。好吧,我确定我的客户都在同一个 wifi 网络中(或者它是必须的吗?)有人可以在这种情况下启发我吗?我是否需要做任何额外的事情才能使客户端连接?非常感谢您的帮助!

4

6 回答 6

3

So I was running into this and the solution (for me) was somewhat embarrasing. I had copied and pasted a bunch of the code from the Apple docs..and they left out an obvious step. They never actually set the match's delegate!

My code now is:

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {
[self dismissModalViewControllerAnimated:YES];
self.myMatch = match; // Use a retaining property to retain the match.
self.myMatch.delegate = self;  // THIS LINE WAS MISSING IN THE APPLE DOCS.  DOH.
// Start the game using the match.
NSLog(@"Match started! Expected Player Count:%d  %@",match.expectedPlayerCount, match.playerIDs);}

Once I actually set the match delegate, the functions get called. Doh.

于 2011-01-18T22:25:05.833 回答
2

当您获得 GKMatch 对象时,请务必检查 expectedPlayerCount 属性。其他玩家可能已经连接,因此您不会在委托上获得 match:player:didChangeState。

于 2010-12-13T19:06:17.283 回答
1

它一直在工作。唯一的区别是......当您使用邀​​请时,不会调用事件“didChangeState”。您已连接,恕不另行通知,您可以开始接收数据。我从来没有尝试过发送/接收数据......因为我首先期待这个事件,但我确实有一次错误地发送了一些东西,并且它起作用了。:)

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *) match {    
    //Dismiss window
    [self dismissModalViewControllerAnimated:YES];

    //Retain match
    self.myMatch = match; 

    //Delegate
    myMatch.delegate = self;


    //Flag
    matchStarted = TRUE;

   //Other stuff
}

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state  {
    //This code gets called only on auto-match
}

上面的代码按预期工作。

于 2011-06-16T13:45:15.480 回答
1

在你的回调 matchmakerViewController:didFindMatch 里面

添加此代码,然后您将看到 GC 调用的回调“match:player:didChangeState”

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
    request.minPlayers = 2;
    request.maxPlayers = 2;
    [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:request completionHandler:^(NSError* error) {
        if(error)
            NSLog(@"Error adding player: %@", [error localizedDescription]);
    }];
于 2011-02-25T04:54:41.913 回答
1

我和朋友有同样的问题。解决方案很奇怪,但之后就可以了。在所有设备上,您必须在设置/通知选项中为游戏中心启用通知(声音/警报/徽章)。之后我们可以建立连接并收到一个匹配对象

于 2010-12-29T21:40:49.280 回答
0

确保您已将您的班级设置为GKSession. 该类将需要实现GKSessionDelegate协议...否则,它将永远不会收到此回调。这是协议参考。希望这可以帮助!

于 2010-12-02T16:49:42.393 回答