Skip to main content
 首页 » 操作系统

swift之如何在swift中接受Game Center邀请

2024年01月01日46飞鱼

我在快速接受邀请时遇到了一些麻烦。

有人可以帮我正确编码吗?这是我的

GKMatchmaker.sharedMatchmaker().matchForInvite(Invitation!, completionHandler = {(InvitedMatch:GKMatch!, error: NSError!) -> Void in 
        if InvitedMatch != nil { 
            myMatch=match 
 
            LocalGame=false 
 
            if let scene = GameScene.unarchiveFromFile(environment_Prefix!+"GameScene") as? GameScene { 
                // Configure the view. 
                let skView = self.view as SKView! 
                //skView.showsFPS = true 
                //skView.showsNodeCount = true 
 
                /* Sprite Kit applies additional optimizations to improve rendering performance */ 
                skView.ignoresSiblingOrder = true 
 
                /* Set the scale mode to scale to fit the window */ 
                scene.scaleMode = .Fill 
 
                skView.presentScene(scene, transition: SKTransition.flipVerticalWithDuration(2.0)) 
 
            } 
        } 
    }) 

谢谢

请您参考如下方法:

我终于找到了有效的解决方案。我必须像这样实现 GKLocalPlayerListener 并在委托(delegate)函数中调用邀请匹配。

   func player(player: GKPlayer!, didAcceptInvite invite: GKInvite!) { 
 
    GKMatchmaker.sharedMatchmaker().matchForInvite (invite, {(InvitedMatch, error) in 
 
        if InvitedMatch != nil { 
            myMatch=InvitedMatch 
 
            LocalGame=false 
 
            if let scene = GameScene.unarchiveFromFile(environment_Prefix!+"GameScene") as? GameScene { 
                // Configure the view. 
                let skView = self.view as SKView! 
                //skView.showsFPS = true 
                //skView.showsNodeCount = true 
 
                /* Sprite Kit applies additional optimizations to improve rendering performance */ 
                skView.ignoresSiblingOrder = true 
 
                /* Set the scale mode to scale to fit the window */ 
                scene.scaleMode = .Fill 
 
                skView.presentScene(scene, transition: SKTransition.flipVerticalWithDuration(2.0)) 
 
            } 
        } 
    }) 
} 

要调用播放器函数,我必须像这样在本地播放器身份验证 block 中注册监听器:

localPlayer.registerListener(self) 

现在游戏邀请完美运行。