简体   繁体   English

从GameCenter发送和接收数据:Swift

[英]Send and receive data from GameCenter: Swift

I'm creating a game that uses GameCenter and I'm trying to send and retrieve data. 我正在创建一个使用GameCenter的游戏,我正在尝试发送和检索数据。

This is how I'm sending data: (the function 'sendData' is provided by GameCenter with GameKit) 这就是我发送数据的方式:(函数'sendData'由GameCenter提供GameKit)

let nick = GCHelper()
let data = NSData(contentsOfFile: "")
try! nick.match.sendData(toAllPlayers: data as! Data, with: .reliable)

'GCHelper' is a class I'm using that contains many functions for Game Center, a download is further through the questions if you're interested. 'GCHelper'是我正在使用的一个类,它包含许多游戏中心的功能,如果你有兴趣,可以下载更多问题。 I just needed to call it to access the function. 我只需要调用它来访问该功能。

Then to retrieve data I'm attempting to use this: 然后检索数据我试图使用这个:

nick.match(GKMatch, didReceive: Data, fromPlayer: String)

Note : I have not filled in any of the above parameters 注意 :我没有填写上述任何参数

Here is the function I'm using to retrieve the data: 这是我用来检索数据的函数:

public func match(_ theMatch: GKMatch, didReceive data: Data, fromPlayer playerID: String) {
    if match != theMatch {
        return
    }

    delegate?.match(theMatch, didReceiveData: data, fromPlayer: playerID)
}

The function I'm using 'match()' is apart of the GCHelper class. 我正在使用'match()'的函数是GCHelper类的一部分。 GCHelper allows for you to create GameCenter game easier. GCHelper允许您更轻松地创建GameCenter游戏。 Here is a link in case you want to reference it: https://github.com/jackcook/GCHelper 如果您想引用它,可以使用以下链接: https//github.com/jackcook/GCHelper

QUESTION Now that I've showed you all the methods, how would I use the previous method to retrieve data? 问题既然我已经向您展示了所有方法,我将如何使用以前的方法来检索数据? One of its parameters is 'fromPlayer' and asks for the playerId(String), but how would I know what the other players playerID is? 其中一个参数是'fromPlayer'并询问playerId(String),但我怎么知道其他玩家的玩家ID是什么? Better yet, how would I retrieve it? 更好的是,我该如何找回它?

If you don't think this is a good way to handle retrieving data, how could I do it better? 如果您认为这不是处理检索数据的好方法,我怎么能做得更好? Is there another way to do this? 还有另一种方法吗?

Key Facts: The game requires 2 people and data is being exchanged between these 2 people only. 重要事实:游戏需要2个人才能在这2个人之间交换数据。 I need to know how to send and retrieve data amongst the 2. Thanks for the help! 我需要知道如何在2中发送和检索数据。感谢您的帮助! If you have any questions let me know. 如果您有任何问题,请告诉我。

This is the function I'm using to retrieve the data: 这是我用来检索数据的函数:

open func match(_ theMatch: GKMatch, didReceive data: Data, fromPlayer playerID: String) {
}

Now, the data I'm sending is a string. 现在,我发送的数据是一个字符串。 So I create a variable that unarchives the data and sets it to string format. 所以我创建了一个变量来取消归档数据并将其设置为字符串格式。

let myString = NSKeyedUnarchiver.unarchiveObject(with: data) as! String

After this, I need a way to get that string from the view controller or game center file to my game scene. 在此之后,我需要一种方法从视图控制器或游戏中心文件到我的游戏场景中获取该字符串。 In which I simply just stored the data using UserDefaults. 我只是使用UserDefaults存储数据。

let user = UserDefaults.standard
user.set(myString, forKey: "myString")

Then in my SKScene I created a timer, in which it checks every couple of seconds to see if there's a change. 然后在我的SKScene中创建了一个计时器,每隔几秒检查一次计时器是否有变化。 It stores the values so that when a new values arrives it can compare and if it is different it does something with it, for example: update player points. 它存储值,以便当新值到达时它可以进行比较,如果它不同,它会对它做一些事情,例如:更新玩家点数。

Note: This is what I found to work, someone else may have a better way, but it works fine for me. 注意:这是我发现的工作,其他人可能有更好的方法,但它对我来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM