为什么我无法通过Game Center Multiplayer将设备连接在一起?

时间:2015-07-27 03:33:08

标签: ios xcode swift game-center multiplayer

我正在制作一款使用GameCenter多人游戏的游戏,但我现在正试图弄清楚当我在我的模拟器上移动它时,我将如何让节点在我的设备上移动,反之亦然。我让游戏中心进行身份验证,找到匹配,寻找玩家,找到玩家但是当我到达我的游戏玩法时,两个设备之间没有多人游戏活动。有人可以帮我弄这个吗?对不起,所有的代码。谢谢!

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()




        }


    }


}

1 个答案:

答案 0 :(得分:1)

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

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)
)