gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:48:44 27 4
gpt4 key购买 nike

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

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




}


}


}

最佳答案

您可能想查看 Game Center Programming Guide来自 Apple,特别是实时比赛章节。基本上,一旦你的比赛开始,你就可以开始在玩家之间交换数据,如果你不想在某个玩家对象(节点)周围移动,这样其他玩家就可以看到你的移动,一旦玩家移动了它的对象,你可能想要实现类似的代码/节点:

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

关于ios - 为什么我不能让设备通过 Game Center Multiplayer 连接在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com