ios – GameCenter Multiplayer坚持“开始游戏……”

我目前正在开发我的游戏,我决定在游戏中通过GameCenter启用多人游戏以允许用户玩他们的朋友.我在 RayWinderLinch之前遵循了一个教程,但遇到了问题.

我的问题是,当我加载GKMatchMakingViewController并点击两个设备上的“立即播放”按钮时,它会找到彼此(这意味着发生),并且在设置的游戏中心用户名下,它将显示Ready.

这意味着GameCenter已找到每个玩家并准备开始它应该的比赛,但在我的情况下,比赛永远不会开始.它停留在一个循环上,表示开始游戏……没有任何反应.看来,

func matchmakerViewController(viewController:gkmatchmakerViewController!,didFindMatch theMatch:GKMatch!)

func match(theMatch:GKMatch!,player playerID:String!,didChangeState state:GKPlayerConnectionState)

方法永远不会运行.我完全迷失了正在发生的事情.我一遍又一遍地试过这个来解决这个问题,但没有任何效果.我将附加一个图像,显示我的问题仍然存在的应用程序的屏幕,我还将附加我正在使用的代码.

I am using a framework based of of the GameKitHelper.h In the
mentioned tutorial above. It is written in swift and is called
07001

可以使用前面提到的GitHub链接找到GCHelper的代码

I have cut out code that is unnecessary for this problem

class GameScene : SKScene,GameKitHelper,MultiplayerNetworkingProtocol {

     override func didMovetoView () {

          GCHelper().authenticateLocalUser() //Authenticate GameCenter User
          println("\n \n \n Authenticating local user \n \n \n")

     }

      func startMultiplayer () {

           var vc = self.view?.window?.rootViewController
           GameKitHelper().findMatchWithMinPlayers(2,maxPlayers: 2,viewController: vc!,delegate: self); //Find match and load gkmatchmakerViewController

      }

       func matchStarted() {

           //Delegate method
           println("match started")

       }

       func matchEnded() {

           //Delegate method
           println("match ended")

       }

        func match(match: GKMatch,didReceiveData: NSData,fromPlayer: String){
        //Delegate Method
        println("Did receive data")

}


     override func touchesBegan(touches: Set<NSObject>,withEvent event: UIEvent) {

         for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)


        if self.nodeAtPoint(location) == multiplayer //SKSpriteNode {

             //User clicked on multiplayer button,launch multiplayer Now!
             println("Loading multiplayer")
             startMultiplayer()
      }

 }

图片

UPDATE

我注意到当我使用我的iPhone和模拟器进行测试时,在iPhone上的状态将从Ready变为disconnected但在模拟器上状态仍然就绪,然后我将在iPhone的控制台中收到以下消息

Warning matchmakerViewController:didFindMatch: delegate method not implemented`

即使它是在GCHelper.swift文件中实现的.当我在我的iPhone和iPad Mini上进行测试时,这种情况不会发生,它只是继续说开始游戏……

任何帮助将不胜感激.

解决方法

先决条件

>两个玩家必须在同一个环境中(SandBox进行测试)
> GCHelper.swift中的authenticationChanged不能是私有的.您可能必须删除该关键字.

有几个代表参与,在您的示例中,有一些竞争协议.我的建议是使用简约代码创建一个新的应用程序来追踪startMultiplayer问题.

使用GCHelper的Gamekit Multi Player Step by Step教程

使用相同的产品名称&创建一个新项目(Xcode>文件>新>项目…>单一视图应用程序> …>创建).组织名称作为您的游戏,因此它与App Bundle Identifier和iTunes Game Center参数相匹配.这将允许您无需开销即可运行测试.

使用此Podfile:

platform :ios,'8.0'

use_frameworks!

target 'SO-31699439' do
pod 'GCHelper'
end

使用GCHelperDelegate

创建一个只有最小值的UIViewController(一个Start Multiplayer按钮),并将它连接到这个动作:

@IBAction func startMultiplayerAction(_ sender: AnyObject) {
    GCHelper.sharedInstance.findMatchWithMinPlayers(
        2,viewController: self,delegate: self);
}

这是关键:你传递的代表必须采用GCHelperDelegate.它不必是同一个类,但在上面的示例中它是,并且当前规则不受尊重.对于此示例,ViewController采用GCHelperDelegate:

import UIKit
import GCHelper
import GameKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        GCHelper.sharedInstance.authenticateLocalUser()
    }
}

在扩展中实现所需的GCHelperDelegate方法

由于ViewController采用GCHelperDelegate,因此下面的三个方法必须在同一个类中,并且将被调用

extension ViewController: GCHelperDelegate {
    func matchStarted() {
        print("matchStarted")
    }

    func match(_ match: GKMatch,didReceiveData: Data,fromPlayer: String) {
        print("match:\(match) didReceiveData: fromPlayer:\(fromPlayer)")
    }

    func matchEnded() {
        print("matchEnded")
    }
}

执行

测试:构建,链接,运行,成功匹配.

启动应用程序,点击开始多人游戏按钮,在两台设备(或iPhone模拟器真实设备)上点按立即播放.

日志:

Authenticating local user...
Authentication changed: player not authenticated

Ready to start match!
Found player: SandBoxPlayer
matchStarted

►在GitHub上找到此解决方案,并在Swift Recipes上找到其他详细信息.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...