为什么我不能让设备通过 Game Center Multiplayer 连接在一起?

Why can't I get the devices to connect together through Game Center Multiplayer?

我正在制作一个使用 GameCenter Multiplayer 的游戏,但我现在卡住了,试图弄清楚当我在模拟器上移动节点时如何让节点在我的设备上移动,反之亦然。我让 Game Center 进行身份验证、查找匹配项、查找玩家并找到玩家,但是当我开始玩游戏时,两台设备之间没有多人游戏 activity。有人可以帮我弄这个吗?对不起所有的代码。谢谢!

func lookupPlayers() {
    println("Looking up \(match.playerIDs.count) players...")

    GKPlayer.loadPlayersForIdentifiers(match?.playerIDs) { (players, error) -> Void in
        if error != nil {
            println("Error retrieving player info: \(error.localizedDescription)")
            self.matchStarted = false
            self._delegate?.matchEnded()
        }
        else {
            self.playersDict = NSMutableDictionary(capacity: players.count)
            for player in players {
                println("Found player: \(player.alias)")
                self.playersDict?.setObject(player, forKey: player.playerID)
            }
        }
        self.playersDict?.setObject(GKLocalPlayer.localPlayer(), forKey: GKLocalPlayer.localPlayer().playerID)
        self.matchStarted = true
    }
}





func match(match: GKMatch!, didFailWithError error: NSError!) {
    println("error!")
}


func match(match: GKMatch!, shouldReinviteDisconnectedPlayer player: GKPlayer!) -> Bool {
    return true
}




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

    println("what!")

    self.lookupPlayers()
}


func match(match: GKMatch!, player: GKPlayer!, didChangeConnectionState state: GKPlayerConnectionState) {
    println("connects or disconnects")






}

func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch match: GKMatch!) {

    println("match found")

    var goToMatch = GamePlay(size: self.size)
    var transitionToMatch = SKTransition.fadeWithDuration(1.0)
    goToMatch.scaleMode = SKSceneScaleMode.AspectFill
    self.scene!.view?.presentScene(goToMatch, transition: transitionToMatch)

    presentingViewController = viewController
    self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)

    self.match = match
    self.match.delegate = self

    self.lookupPlayers()
}


func matchmakerViewController(viewController: GKMatchmakerViewController!, didReceiveAcceptFromHostedPlayer playerID: String!) {

}


func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindPlayers playerIDs: [AnyObject]!) {

}


func matchmakerViewController(viewController: GKMatchmakerViewController!, didFailWithError error: NSError!) {

    presentingViewController = viewController
    self.presentingViewController.dismissViewControllerAnimated(true, completion: nil);
    println("Error finding match: \(error.localizedDescription)");

}


func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindHostedPlayers players: [AnyObject]!) {

}



func matchmakerViewControllerWasCancelled(viewController: GKMatchmakerViewController!) {

    println("go back to main menu")

    presentingViewController = viewController
    self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)



}


func findMatchPlease() {


    match = nil
    let matchRequest = GKMatchRequest()
    matchRequest.minPlayers = 2
    matchRequest.maxPlayers = 2


    let mmvc = GKMatchmakerViewController(matchRequest: matchRequest)
    mmvc.matchmakerDelegate = self
    let viewController = self.scene?.view?.window?.rootViewController
    viewController?.presentViewController(mmvc, animated: true, completion: nil)
}



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

    for touch in (touches as! Set<UITouch>) {

        var touch: UITouch = touches.first as! UITouch
        var location = touch.locationInNode(self)
        var node = self.nodeAtPoint(location)

        if node.name == "find" {



            findMatchPlease()




        }


    }


}

您可能想查看 Apple 的 Game Center Programming Guide,尤其是实时匹配章节。基本上一旦你的比赛开始,你就可以开始在玩家之间交换数据,如果你不想移动一些玩家对象(节点)以便其他玩家看到移动你可能想要在玩家移动它的对象/后实现类似的代码节点:

func sendPosition() {
    var msg = CGPointMake(0, 0) // set the position to send here
    let packet = NSData(bytes: &msg, length: sizeof(CGPoint))

    // Swift pre-2.0, no error handling
    var error: NSError?
    if !match.sendDataToAllPlayers(packet, withDataMode: .Unreliable error: &error) {
        // some error happened
    }

    // Swift 2.0:
    do {
        try match.sendDataToAllPlayers(packet, withDataMode: .Unreliable)
    }
    catch {
        // handle errors
    }
}

它将向所有连接到比赛的玩家发送 CGPoint 结构数据,因此在另一端您可能想要实现 GKMatchDelegate 的方法 - match:didReceiveData:fromRemotePlayer:。它可能看起来像这样:

func match(_ match: GKMatch!, didReceiveData data: NSData!, fromRemotePlayer player: GKPlayer!) {
    if let msg = data.bytes as? CGPoint {
        // new position received, assign it to player it was received from
    }
}

您可以通过这种方式发送大部分数据,并在另一端接收并转换回数据。但要注意 Swift 类型可以占用更多字节通过网络发送,我建议创建 C 结构用于发送网络数据,或者如果你想尽可能保持你的代码为 Swift 尝试元组:

var myStruct = exampleStruct (
    SomeInt: 1,
    OtherInt: 2,
    SomeCString: (0x48, 0x45, 0x4C, 0x4C, 0x4F) // HELLO
)
var data = NSData(
    bytes: &myStruct,
    length: sizeof(exampleStruct)
)