首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matchmakerViewController:接受Invite后未调用didFindMatch

matchmakerViewController:接受Invite后未调用didFindMatch
EN

Stack Overflow用户
提问于 2020-07-22 05:33:13
回答 1查看 65关注 0票数 0

我正在制作一个实时的游戏中心游戏,与GameKit作为一个新的程序员,但我遇到了一些障碍。

我能够正确地初始化本地播放器,呈现匹配viewController,并接收和接受邀请,然而,即使在我接受邀请matchmakerViewController:didFindMatch之后,也没有调用任何匹配返回给我。很有可能我没有正确地接受邀请,但根据我的苹果开发者论坛。如果有人可以审查这段代码,并让我知道如何调用该方法,那就太好了!

代码语言:javascript
复制
let request = GKMatchRequest()

class SecondScreenViewController : UIViewController, UINavigationControllerDelegate, GKMatchmakerViewControllerDelegate, GKLocalPlayerListener {

    let button = UIButton()
    let softRed = UIColor(red: 1, green: 61 / 255, blue: 61 / 255, alpha: 1)
    let softblue = UIColor(red: 38 / 255, green: 149 / 255, blue: 1, alpha: 1)
    
    let TestMatchMaker = GKMatchmaker()
    let TestMatch = GKMatch()
    var TestViewController = GKMatchmakerViewController(matchRequest: request)
    
    var player = GKLocalPlayer.local

    override func viewDidLoad() {
        super.viewDidLoad()
        
        player.register(self)
        setupButton()
        SetupRequest()
        view.backgroundColor = softblue
        
       
    }

    func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
       dismiss(animated: true, completion: nil)
        print("cancelled")
    }
    
    func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
        print(error.localizedDescription + "THIS IS AN ERROR FROM THE VIEWCONTROLLER")
    }
    
    func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
        print("Found Match Successfully")
        let TestMatch = match
        print(TestMatch)
    }
    
    
    func player(_ player: GKPlayer, didAccept invite: GKInvite) {
        print(invite)
        print("Aceppted Invite")
        
        TestViewController?.setHostedPlayer(player, didConnect: true)
        
        
        
    }
    
    func player(_ player: GKPlayer, didRequestMatchWithRecipients recipientPlayers: [GKPlayer]) {
        print("Requested Match")
    }
    
    func SetupRequest() {
        request.minPlayers = 2
        request.maxPlayers = 2
        request.inviteMessage = "test Invite"
    }

    
    func setupButton() {
         button.backgroundColor = UIColor.white
         button.setTitleColor(softblue, for: .normal)
         button.setTitle("MatchMake", for: .normal)
         button.addTarget(self, action: #selector(ButtonTapped), for: .touchUpInside)
         
         view.addSubview(button)
         setupButtonConstraints()
     }
     
     func setupButtonConstraints() {
         button.translatesAutoresizingMaskIntoConstraints = false
         button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
         button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
         button.heightAnchor.constraint(equalToConstant: 100).isActive = true
         button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
         
     }
     
     @objc func ButtonTapped() {
        
        present(TestViewController!, animated: true, completion: {
            self.TestViewController!.delegate = self})
        TestViewController?.matchmakerDelegate = self
        
        
     }
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-22 22:42:47

我看到的唯一可能导致它的事情是当你设置matchmaker委托的时候。这是我用来邀请玩家的代码,它工作正常:

代码语言:javascript
复制
func invitePlayers(){
    let request = GKMatchRequest()
    request.minPlayers = 2
    request.maxPlayers = 4
    request.defaultNumberOfPlayers = 2
    let vc = GKMatchmakerViewController(matchRequest: request)
    vc?.matchmakerDelegate = self
    self.present(vc!, animated: true)
}

请注意,matchmakerDelegate是在显示ViewController之前设置的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63023425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档