Iphone 如何在游戏中心接受邀请
我试图通过游戏中心实现邀请,但有一件事我不明白。好的,我已经从一个设备向另一个设备发送了邀请。然后我在接收者上有一个UIAlertView,它会问我是否愿意接受或拒绝邀请。当我接受它时,处理方式如下:Iphone 如何在游戏中心接受邀请,iphone,game-center,multiplayer,Iphone,Game Center,Multiplayer,我试图通过游戏中心实现邀请,但有一件事我不明白。好的,我已经从一个设备向另一个设备发送了邀请。然后我在接收者上有一个UIAlertView,它会问我是否愿意接受或拒绝邀请。当我接受它时,处理方式如下: [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { // Ins
[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,然后点击“立即玩”
说我应该选择第二种方式,但当解除警报并点击“立即播放”时,发现发送方设备仍在等待响应,并且不知道我已接受邀请。如果此时点击“Play now”(立即播放),正如我前面所说,它会显示一个警报,表示应用程序正在等待响应。如果你曾经这样做过,那么请解释我该怎么做。谢谢 根据Ray的教程,我已经成功实现了在线游戏中心比赛。您的问题的答案是:您不必向邀请设备发送任何内容。当您调用以下行时:
GKMatchmakerViewController*mmvc=[[GKMatchmakerViewController alloc]initWithMatchRequest:request]autorelease]代码>,matchMakerVController处理连接。但是,您应该尽快编写邀请处理程序,最好使用身份验证更改方法。见我的:
-(void) authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
NSLog(@"Authentication changed: player authenticated.");
userAuthenticated = TRUE;
[self sendUnsentScores];
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite){
NSLog(@"Invite");
if([AppDelegate mainMenuController].presentedViewController!=nil) {
[[AppDelegate mainMenuController] dismissViewControllerAnimated:NO completion:^{
}];
} // if we're not on the main menu, or another game is going on.
// this would be easier to do if you were using a navigation controller
// where you'd just push the multiplayer menu etc.
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
[[AppDelegate mainMenuController] presentViewController:[AppDelegate mainMenuController].multiGameMenu animated:NO completion:^{ // push the multiplayer menu
[[AppDelegate mainMenuController].multiGameMenu duel:nil];
}];
};
}
如果你有兴趣,这里是决斗的方法。非常混乱的代码,但请处理:)
游戏加载后,我立即注册邀请,并致电
收到邀请时委派
所以我接到了媒人取消比赛的电话
中心控制器并创建匹配
最后,当找到匹配项时,方法
connectionStatusChanged负责呈现所有游戏的
观点、玩家和其他东西
代码如下:
我在游戏加载后立即注册邀请,并在收到邀请后致电代表:
- (void)registerInvites {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
[delegate inviteReceived];
};
}
因此,我们接到了match maker的电话,要求其解除game center控制器并创建比赛:
- (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;
}
- (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];
}
}
inviteHandler=^(GKIVITE*acceptedInvite,NSArray*PlayerToInvite)现在不推荐使用。查看注册邀请通知的新方法
请把下面那一段分成几段。
- (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];
}
}