我正在关注RayWenderlich GameCenter教程:Raywend GameCenter Tutorial但我正在尝试将其转换为Swift 3.0。基本上它只是验证本地播放器,打开匹配界面,然后一旦两个玩家找到对方就应该调用matchmakerViewController
函数。
播放器已成功通过验证,并显示了matchmakerViewController,玩家可以相互查找。但是,在显示找到的对手并在其名称旁边放置蓝色复选标记后,应用程序会调用matchmakerViewControllerWasCancelled
而不是matchmakerViewController(viewController: GKMatchmakerViewController,didFindMatch match: GKMatch)
这对我来说很奇怪,因为我很确定该方法只会被调用基于用户交互(它没有调用错误方法)。
运行项目的objective-c版本,但我的快速转换有这个问题。以下是findMatchWithMinPlayers()
方法的两个版本。我假设我在这里改了一些错误:
夫特
func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate) {
if !enableGameCenter {
return
}
self.matchStarted = false
self.match = nil
self.delegate = delegate
viewController.dismiss(animated: false, completion: { _ in })
var request = GKMatchRequest()
request.minPlayers = minPlayers
request.maxPlayers = maxPlayers
var mmvc = GKMatchmakerViewController(matchRequest: request)
mmvc?.matchmakerDelegate = self
viewController.present(mmvc!, animated: true, completion: { _ in })
}
目标C
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers
viewController:(UIViewController *)viewController
delegate:(id<GameKitHelperDelegate>)delegate {
if (!_enableGameCenter) return;
_matchStarted = NO;
self.match = nil;
_delegate = delegate;
[viewController dismissViewControllerAnimated:NO completion:nil];
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
GKMatchmakerViewController *mmvc =
[[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
[viewController presentViewController:mmvc animated:YES completion:nil];
}
唯一可能与此不同的是:
目标C
+ (instancetype)sharedGameKitHelper
{
static GameKitHelper *sharedGameKitHelper;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedGameKitHelper = [[GameKitHelper alloc] init];
});
return sharedGameKitHelper;
}
夫特
相反,我只使用了名为sharedGameKitHelper
的全局变量,并且在代码中随时使用[GameKitHelper sharedGameKitHelper]
我使用了sharedGameKitHelper
。他们将该方法的目的描述为创建GameKitHelper类的“单例对象”。
如果您发现这些没有错,这里是我的两个“GameKitHelper”类的完整Pastebin,其中包含所有GameKit代码。
objectice-c swift
我已多次检查每个方法并从头开始重新启动项目,因此我必须对Objective-c转换的知识有所了解。感谢您提供任何帮助!