gpt4 book ai didi

iphone - 如何在 Game Center 中接受邀请

转载 作者:行者123 更新时间:2023-12-03 18:43:24 24 4
gpt4 key购买 nike

我正在尝试通过 Game Center 实现邀请,但有一点我不明白。好的,我已从一台设备向另一台设备发送了邀请。然后我在接收器上有一个 UIAlertView 询问我是否愿意接受或拒绝邀请。当我接受它时,它的处理方式如下:

[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) 
{
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite)
{
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
}
else if (playersToInvite)
{
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 4;
request.playersToInvite = playersToInvite;

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

}
};

嗯,那太好了,但是接下来呢?发送者设备显然正在等待某种标准类型的响应,因为它还会显示一条警报,告诉我如果我点击“立即播放”,则有一些邀请尚未得到答复。

那么我该如何接受邀请呢?我应该发回什么样的数据(以及如何)?我在接收方到底应该做什么?游戏应该在点击“接受”后立即开始,还是应该先关闭 AlertView,然后点击“立即玩”?

Ray Wenderlich's tutorial说我应该选择第二种方式,但是当解除警报并点击“立即播放”时,事实证明发送者设备仍在等待响应并且不知道我已经接受了邀请。如果我此时点击“立即播放”,正如我上面所说,它会显示一条警报,表明应用程序正在等待响应。因此,如果您曾经这样做过,请解释一下我应该做什么。谢谢!

最佳答案

  1. 游戏加载后我就注册邀请并调用收到邀请时委托(delegate)
  2. 因此inviteReceived 会调用匹配系统以取消比赛中心 Controller 并创建匹配
  3. 最后,当找到匹配项时,该方法connectionStatusChanged 负责呈现游戏的所有状态观点、球员和其他东西

这是代码:

我在游戏加载后立即注册邀请,并在收到邀请后调用委托(delegate)人:

- (void)registerInvites {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
[delegate inviteReceived];
};
}

因此,inviteReceived 会调用匹配器来关闭游戏中心 Controller 并创建匹配:

- (void)inviteReceived {
[[GCMultiplayerHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:(UIViewController*)[self.superview nextResponder] delegate:self];
}


- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCMultiplayerHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;

matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;

[presentingViewController dismissModalViewControllerAnimated:YES];
GKMatchmakerViewController *mmvc;

if (pendingInvite != nil) {

mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];

} else {

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

mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];

}

mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];

self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}

最后,当找到匹配时,connectionStatusChanged 方法会负责显示所有游戏 View 、玩家并开始比赛:

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)theMatch
{
self.match = theMatch;
match.delegate = self;
if (!matchStarted && match.expectedPlayerCount == 0) {
NSLog(@"Ready to start match! - didFindMatch");
[presentingViewController dismissModalViewControllerAnimated:YES];

[self.delegate connectionStatusChanged:CONNECTIONSUCCESS];
}
}

关于iphone - 如何在 Game Center 中接受邀请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com