游戏中心以编程方式查找匹配项 [英] Game Center Finding a Match Programmatically

查看:162
本文介绍了游戏中心以编程方式查找匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚这是如何运作的。我想要做的是让两个玩家玩游戏,如果第三个玩家加入它可以立即加入游戏,如果第四个和最后一个玩家加入它也可以立即加入游戏。他们也可以随时以任何理由离开游戏,如果发生这种情况,应该为另一个人或同一个人重新连接打开一个空间。这就是主意。

I just can't figure out how this works. What I am trying to do is let two players play a game if a third player joins it can instantly join the game, if the fourth and last player joins it can also instantly join the game. They can also leave the game at anytime for whatever reason, if that happens there should be a space open for another person or for the same person to reconnect. That's the idea.

现在我得到的是以下内容。我出于显而易见的原因验证了本地播放器。然后我像这样搜索一个匹配:

Now what I got is the following. I authenticate the local player for obvious reasons. Then I search for a match like so:

if (matchRequest) [matchRequest release];
matchRequest            = [[GKMatchRequest alloc] init];
matchRequest.minPlayers = 2;
matchRequest.maxPlayers = 4;

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:matchRequest withCompletionHandler:^(GKMatch *match, NSError *error) {
    if (error) {
        // An error occured
    } else {
        if (matchCurrent) [matchCurrent release];
        matchCurrent          = [match retain];
        matchCurrent.delegate = self;
    }
}];

如果我在三个不同的设备上执行此部分,其中两个会找到对方,第三个是还在寻找。所以我想在找到匹配请求之后找到了它将被执行一次的最小数量的玩家。所以我需要的是一种方法,它使用我保留的matchCurrent来添加更多玩家。 Luckely这种方法存在,但是如何运作呢?在这种情况下你什么时候打电话?我决定将它放在一个按钮下,这样我就可以在找到匹配项时手动执行它。

If I execute this part on three different devices, two of them will find each other and the third is still looking. So I figured after the find match for request has found the minimum amount of players it will be executed once. So what I needed was a method that used the matchCurrent that I retained to add more players. Luckely that method existed, but how would that work? When do you call it in this case? I decided to put it under a button so I could manually execute it when a match has been found.

我发现当我在第一台设备上按下它时,最后,第三个设备可以找到第一个和第二个设备所在的匹配。事实上,第二个和第三个设备包含所涉及的每个设备的playerID。这是件好事。但是有两个问题。

What I discovered is that when I pressed it on the first device, finally the third device could find the match the first and second device were in. In fact the second and third device contained the playerIDs of every device involved. Which is a good thing. But there are two problems.


  1. 哪个设备实际上应该调用addPlayersToMatch方法?如何将它限制为执行该方法的一个设备?另外你什么时候打电话呢?

  2. 为什么在调用该方法的设备上,不是更新了playerID?

  1. Which device should actually call the addPlayersToMatch method? And how can you restrict it to one device executing that method? Plus when should you call it?
  2. Why, on the device calling that method, isn't the playerIDs updated?

[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:matchCurrent matchRequest:matchRequest completionHandler:^(NSError *error) {
    //matchCurrent.playerIDs is not updated?!
}]; 

实际上它们已更新。当我看到第二个和第三个设备上出现播放器ID时,我手动更新设备1上的matchCurrent.playerIDs,突然它确实识别出播放器。但是,当在设备1上发现新玩家时,甚至不会调用播放器的'didChangeState'。

Actually they are updated. When I see the playerIDs appear on the second and third device I manually update the matchCurrent.playerIDs on device one and suddenly it does recognize the player. However even the 'didChangeState' for player is not called when the new player is discovered on device one.


推荐答案

您使用Apple iOS游戏中心GKMatchmaker类。我假设您正在使用点对点连接,而不是托管。

Your using the Apple iOS Game Center GKMatchmaker class. I'm assuming you are using a peer to peer connection, not hosted.

GKMatch类为您提供playerIDs数组。

The GKMatch class gives the playerIDs array to you.

@property(nonatomic, readonly) NSArray *playerIDs

这是一个有序列表,因此您可以使用它来选择第一个玩家调用addPlayersToMatch。

This is an ordered list, so you might be able to use it to select the first player call addPlayersToMatch.

下面链接的是一些文档。

Linked below is some documentation.

http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKMatchmaker_Ref/Reference/Reference.html

< a href =http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKMatch_Ref/Reference/Reference.html =nofollow> http://developer.apple.com/library /ios/#documentation/GameKit/Reference/GKMatch_Ref/Reference/Reference.html

http://developer.apple.com/library/ios /#documentation/NetworkingInternet/Conceptual/GameKit_Guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008304

这篇关于游戏中心以编程方式查找匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆