使用 GKMatch 的 Game Center 多人游戏但似乎无法连接
大家好,我是 iOS 游戏中心的新人。我正在尝试将使用匹配的多人游戏功能添加到我的游戏中并遵循文档。
到目前为止,我的 2 个客户端可以成功获得匹配,即调用 matchmakerViewController:didFindMatch 回调并传递 GKMatch 对象。
然而,在那之后我似乎永远被困在那里,因为根据文档,我必须等到所有玩家(在我的例子中是 2 个)真正连接后才能开始游戏。但似乎从未调用 match:player:didChangeState 回调来指示连接成功。好吧,我确信我的客户都在同一个 wifi 网络中(或者这是必须的?)任何人都可以启发我了解这种情况吗?我需要做任何额外的事情才能使客户端连接吗?非常感谢您的帮助!
Hi I'm a new bie in Game Center for iOS. I'm trying to add the multiplayer feature using matches to my game and following the documentation.
So far I reached a point where 2 of my clients can successfully get a match, i.e. the matchmakerViewController:didFindMatch callback is called and a GKMatch object is delivered.
However after that I seems to be stuck there forever, because according to the documentation, I'll have to wait until all the players (2 in my case) are actually connected before starting my game. But it seems the match:player:didChangeState callback is never called to indicate a successful connection. Well, I'm sure my clients are all in the same wifi network ( or is it a must?) Could any one enlighten me on this case? Do I have to do any extra things to make the clients to connect? Thanks a lot for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您获取 GKMatch 对象时,请务必检查预期玩家计数属性。其他玩家可能已经连接,因此您将不会在委托上获得 match:player:didChangeState 。
When you get the GKMatch object, be sure to check the expectedPlayerCount property. It is possible that the other player is already connected, and thus you will not get a match:player:didChangeState on the delegate.
我和一个朋友也遇到过同样的问题。解决方案很奇怪,但之后就有效了。在所有设备上,您必须在“设置/通知”选项中启用游戏中心的通知(声音/警报/徽章)。之后我们可以建立连接并收到一个匹配对象
I have had the same problem with a friend. The solution was quite strange but it works afterwards. On all devices you have to enable the Notifications (Sounds/Alerts/Badges) for the Game Center inside the Settings/Notifications options. Afterwards we could establish a connection and did receive a match object
在回调 matchmakerViewController:didFindMatch 中
添加此代码,然后您将看到 GC 调用回调“match:player:didChangeState”
Inside your callback matchmakerViewController:didFindMatch
Add this code then you'll see the callback "match:player:didChangeState" being called by GC
它一直在起作用。唯一的区别是......当您使用邀请时,事件“didChangeState”不会被调用。您已连接,恕不另行通知,并且可以开始接收数据。我从未尝试过发送/接收数据...因为我首先期待该事件,但我确实有一次错误地发送了一些内容,并且它起作用了。 :)
上面的代码按预期工作。
It was working all along. The only difference is that... when you use invites the event "didChangeState" doensn't get called. You're connected without notice and you can start to receive data. I never tried to send/receive data... cuz i was expecting the event first, but i did send something by mistake one time, and it worked. :)
The above code works as expected.
确保您已将您的类设置为
GKSession
的委托。该类需要实现 GKSessionDelegate 协议...否则,它将永远不会收到此回调。这是协议参考。希望这有帮助!Make sure that you've set your class as the delegate for
GKSession
. The class will need to implement theGKSessionDelegate
protocol... otherwise, it'll never receive this callback. Here's the protocol reference. Hope this helps!所以我遇到了这个问题,解决方案(对我来说)有点尴尬。我从苹果文档中复制并粘贴了一堆代码……他们遗漏了一个明显的步骤。他们从未真正设置过比赛的代表!
我现在的代码是:
一旦我实际设置了匹配委托,函数就会被调用。哦。
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:
Once I actually set the match delegate, the functions get called. Doh.