无法将 GKTurnBasedEventListener 设置为我的 ViewController 的委托?
GKTurnBasedEventListener could not be set to delegate of my ViewController?
在 objC 中 Rawendrich 为 GKTurnBasedEventListener
编写的语法,当时是 GKTurnBasedEventHandler
,现在由 Apple 更改如下。
if (!gameCenterAvailable) return;
void (^setGKEventHandlerDelegate)(NSError *) = ^ (NSError *error)
{
GKTurnBasedEventHandler *ev =
[GKTurnBasedEventHandler sharedTurnBasedEventHandler];
ev.delegate = self;
};
NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer]
authenticateWithCompletionHandler:
setGKEventHandlerDelegate];
} else {
NSLog(@"Already authenticated!");
setGKEventHandlerDelegate(nil);
}
现在将其转换为swift,并用写下GKTurnBasedEventListener
而不是GKTurnBasedEventHandler
的组合,得到以下方式。
// Converted with Swiftify v1.0.6381 - https://objectivec2swift.com/
if !gameCenterAvailable {
return
}
var setGKEventHandlerDelegate: ((_: Error) -> Void)? = {(_ error: Error?) -> Void in
var ev = GKTurnBasedEventHandler.shared()
ev.delegate = self
}
print("Authenticating local user...")
if GKLocalPlayer.localPlayer().authenticated == false {
GKLocalPlayer.localPlayer().authenticate(withCompletionHandler: setGKEventHandlerDelegate)
}
else {
print("Already authenticated!")
setGKEventHandlerDelegate(nil)
}
不幸的是,这不是为我的 ViewController
.
设置 GKTurnBasedEventListener
委托的正确语法
请问你们谁能帮我解决这个问题,因为没有这个我就无法阅读事件侦听器的默认函数。
干杯!
终于在艰难的 10 小时后,我从 Here. Although this syntax is in objC, but there's no problem of converting it to swift from Swiftify 中找出了这个问题。
虽然比实际时间晚了一点,但我现在可以理解 GKTunBasedEventListener
的设置委托与我们为 UITableViewControllerDelegate
所做的不同。
这里必须先对本地播放器进行认证,然后再将本地播放器的监听器注册到ViewController
的delegate
GKLocalPlayerListener。
我在 Apple's Documentation 上发现的另一件事:
不要直接实现GKChallengeListener
、GKInviteEventListener
、GKSavedGameListener
和GKTurnBasedEventListener
;改为实施 GKLocalPlayerListener
。您可以使用 GKLocalPlayerListener
.
监听和处理多个事件
那么接下来我就按照下面的方式实现了。
import GameKit
class ViewController: UIViewController, GKTurnBasedMatchmakerViewControllerDelegate,
GKLocalPlayerListener {
.....
func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {
print("#1")
print(player)
print("#2")
print(match)
print("#3")
print(didBecomeActive)
if match.status == GKTurnBasedMatchStatus.open
{
if GKLocalPlayer.localPlayer() == match.currentParticipant
{
if didBecomeActive
{
// Active now
}
else
{
// Active already
}
}
else
{
// It's someone's turn
if match.matchData != myMatch?.matchData
{
// Match Data being Updated by Someone
print(player.alias ?? "No Name:")
}
}
}
thirdTopLabel.text = match.matchID! + "\n" + didBecomeActive.description
}
....
现在在 ViewDidLoad()
函数中放入以下代码。
// In the ViewDidLoad function
if(!GKLocalPlayer.localPlayer().isAuthenticated)
{
authenticatePlayer { (auth) in
weak var weakSelf = self
weak var weakPlayer = GKLocalPlayer.localPlayer()
if(auth){
weakPlayer?.register(weakSelf!)
self.suthentication = true;
}
else{
print("failed in authentication")
self.suthentication = false;
}
}
}
else
{
// Already Authenticated
GKLocalPlayer.localPlayer().register(self)
localPlayer = GKLocalPlayer.localPlayer()
}
最后你的身份验证功能应该是这样的。
// authenticate local player :: Just Authentication
func authenticatePlayer(completionHandler: @escaping (_ resultedPlaces: Bool) -> Void) {
localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler =
{ (viewController , error ) -> Void in
if viewController != nil
{
self.present(viewController!, animated:true, completion: nil)
}
else
{
if self.localPlayer.isAuthenticated
{
completionHandler(true);
}
else
{
completionHandler(false);
print("not able to authenticate fail")
self.gameCenterEnabled = false
if (error != nil)
{
print("\(error.debugDescription)")
}
else
{
print( "error is nil")
}
}
}
}
}
注意:GKLocalPlayerListener
无法在模拟器上运行。
仅供参考,如果您想了解如何在回合制 GameKit 比赛中使用 GKLocalPlayerListener 的工作示例,欢迎您查看此 example project for a turn based game。我希望它有助于在上下文中查看所有部分。
在 objC 中 Rawendrich 为 GKTurnBasedEventListener
编写的语法,当时是 GKTurnBasedEventHandler
,现在由 Apple 更改如下。
if (!gameCenterAvailable) return;
void (^setGKEventHandlerDelegate)(NSError *) = ^ (NSError *error)
{
GKTurnBasedEventHandler *ev =
[GKTurnBasedEventHandler sharedTurnBasedEventHandler];
ev.delegate = self;
};
NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer]
authenticateWithCompletionHandler:
setGKEventHandlerDelegate];
} else {
NSLog(@"Already authenticated!");
setGKEventHandlerDelegate(nil);
}
现在将其转换为swift,并用写下GKTurnBasedEventListener
而不是GKTurnBasedEventHandler
的组合,得到以下方式。
// Converted with Swiftify v1.0.6381 - https://objectivec2swift.com/
if !gameCenterAvailable {
return
}
var setGKEventHandlerDelegate: ((_: Error) -> Void)? = {(_ error: Error?) -> Void in
var ev = GKTurnBasedEventHandler.shared()
ev.delegate = self
}
print("Authenticating local user...")
if GKLocalPlayer.localPlayer().authenticated == false {
GKLocalPlayer.localPlayer().authenticate(withCompletionHandler: setGKEventHandlerDelegate)
}
else {
print("Already authenticated!")
setGKEventHandlerDelegate(nil)
}
不幸的是,这不是为我的 ViewController
.
GKTurnBasedEventListener
委托的正确语法
请问你们谁能帮我解决这个问题,因为没有这个我就无法阅读事件侦听器的默认函数。
干杯!
终于在艰难的 10 小时后,我从 Here. Although this syntax is in objC, but there's no problem of converting it to swift from Swiftify 中找出了这个问题。
虽然比实际时间晚了一点,但我现在可以理解 GKTunBasedEventListener
的设置委托与我们为 UITableViewControllerDelegate
所做的不同。
这里必须先对本地播放器进行认证,然后再将本地播放器的监听器注册到ViewController
的delegate
GKLocalPlayerListener。
我在 Apple's Documentation 上发现的另一件事:
不要直接实现GKChallengeListener
、GKInviteEventListener
、GKSavedGameListener
和GKTurnBasedEventListener
;改为实施 GKLocalPlayerListener
。您可以使用 GKLocalPlayerListener
.
那么接下来我就按照下面的方式实现了。
import GameKit
class ViewController: UIViewController, GKTurnBasedMatchmakerViewControllerDelegate,
GKLocalPlayerListener {
.....
func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {
print("#1")
print(player)
print("#2")
print(match)
print("#3")
print(didBecomeActive)
if match.status == GKTurnBasedMatchStatus.open
{
if GKLocalPlayer.localPlayer() == match.currentParticipant
{
if didBecomeActive
{
// Active now
}
else
{
// Active already
}
}
else
{
// It's someone's turn
if match.matchData != myMatch?.matchData
{
// Match Data being Updated by Someone
print(player.alias ?? "No Name:")
}
}
}
thirdTopLabel.text = match.matchID! + "\n" + didBecomeActive.description
}
....
现在在 ViewDidLoad()
函数中放入以下代码。
// In the ViewDidLoad function
if(!GKLocalPlayer.localPlayer().isAuthenticated)
{
authenticatePlayer { (auth) in
weak var weakSelf = self
weak var weakPlayer = GKLocalPlayer.localPlayer()
if(auth){
weakPlayer?.register(weakSelf!)
self.suthentication = true;
}
else{
print("failed in authentication")
self.suthentication = false;
}
}
}
else
{
// Already Authenticated
GKLocalPlayer.localPlayer().register(self)
localPlayer = GKLocalPlayer.localPlayer()
}
最后你的身份验证功能应该是这样的。
// authenticate local player :: Just Authentication
func authenticatePlayer(completionHandler: @escaping (_ resultedPlaces: Bool) -> Void) {
localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler =
{ (viewController , error ) -> Void in
if viewController != nil
{
self.present(viewController!, animated:true, completion: nil)
}
else
{
if self.localPlayer.isAuthenticated
{
completionHandler(true);
}
else
{
completionHandler(false);
print("not able to authenticate fail")
self.gameCenterEnabled = false
if (error != nil)
{
print("\(error.debugDescription)")
}
else
{
print( "error is nil")
}
}
}
}
}
注意:GKLocalPlayerListener
无法在模拟器上运行。
仅供参考,如果您想了解如何在回合制 GameKit 比赛中使用 GKLocalPlayerListener 的工作示例,欢迎您查看此 example project for a turn based game。我希望它有助于在上下文中查看所有部分。