本文介绍了斯威夫特 - 游戏中心无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Swift游戏中实现Game Center。我有一个菜单视图控制器,用户可以按下SCORES按钮,它应该将它们带到Game Center视图控制器。

I'm trying to implement Game Center in my Swift game. I have a menu view controller, where the user can press a "SCORES" button, which should take them to the Game Center view controller.

这是运行的代码在菜单vc中,当按下按钮时:

This is the code that runs in the menu vc, when the button is pressed:

var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self

gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
gcViewController.leaderboardIdentifier = "VHS"

self.presentViewController(gcViewController, animated: true, completion: nil)

我在Game Center vc中有代码,但我认为它没有机会运行。应用程序在此代码后停止执行(没有断点或错误,只是不允许我点击任何内容)并显示一条弹出消息,内容如下:

I have code in the Game Center vc, but I don't think it gets a chance to run. The app stops execution after this code (no breakpoints or errors, just won't let me tap anything) and displays a pop up message that reads:

Game Center Unavailable
Player is not signed in

唯一的其他回复我得到的是Xcode,其中以下行打印到日志中:

The only other response I get is in Xcode, where the following line is printed to the log:

2014-08-29 14:10:33.157 Valley[2291:304785] 17545849:_UIScreenEdgePanRecognizerEdgeSettings.edgeRegionSize=13.000000

我不知道这意味着什么或为什么游戏中心不工作。任何人都可以帮忙??

I have no idea what this means or why Game Center is not working. Can anybody help??

推荐答案

假设您已在应用中启用了Game Center,并在iTunes Connect中添加了排行榜,那么您需要在显示GC之前验证您的播放器。此外,请确保您已在iTunes Connect中创建了一个测试用户,可在出现提示时用于登录Game Center。

Assuming that you've enabled Game Center in your app and also added a leaderboard in iTunes Connect then you need to authenticate your player before you can show GC. Also, be sure that you've created a test user in iTunes Connect that you can use to log in to Game Center when the prompt appears.

您的 MenuViewController 应该在viewDidLoad中验证本地播放器,如下所示:

Your MenuViewController should authenticate the local player in viewDidLoad like so:

class MenuViewController: UIViewController,
            GKGameCenterControllerDelegate
{
    var leaderboardIdentifier: String? = nil
    var gameCenterEnabled: Bool = false

    override func viewDidLoad()
    {
        super.viewDidLoad()

        //Your code that sets up your scene or other set up code

        //HERE IS WHERE YOU AUTHENTICATE
        authenticateLocalPlayer()
    }

    func authenticateLocalPlayer()
    {
        var localPlayer = getLocalPlayer() // see GKLocalPlayerHack.h
        localPlayer.authenticateHandler =
            { (viewController : UIViewController!, error : NSError!) -> Void in
                if viewController != nil
                {
                    self.presentViewController(viewController, animated:true, completion: nil)
                }
                else
                {
                    if localPlayer.authenticated
                    {
                        self.gameCenterEnabled = true
                        localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler
                        { (leaderboardIdentifier, error) -> Void in
                            if error != nil
                            {
                                print("error")
                            }
                            else
                            {
                                self.leaderboardIdentifier = leaderboardIdentifier
                                print("\(self.leaderboardIdentifier)") //in your example "VHS" should be returned
                            }
                        }
                    }
                    else
                    {
                        print("not able to authenticate fail")
                        self.gameCenterEnabled = false

                        if error
                        {
                            print("\(error.description)")
                        }
                        else
                        {
                            print(    "error is nil")
                        }
                    }
                }
        }
    }


    func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
    {
        gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}

在您成功通过认证后,您应该能够提供游戏中心。

After you've successfully authenticated then you should be able to present Game Center.

注意line:
var localPlayer = getLocalPlayer()//请参阅GKLocalPlayerHack.h

为了让它工作,你需要做一些小工具让GKLocalPlayer在Swift中正确实例化。

To get that to work you need to do a little hack to get GKLocalPlayer to instantiate correctly in Swift.

在Objective-C中创建一个新类并将文件命名为GKLocalPlayerHack.h / m

Create a new class in Objective-C and name the file GKLocalPlayerHack.h/m

在标题中放置:

//  GKLocalPlayerHack.h
// Issue with GameKit and Swift
// https://stackoverflow.com/questions/24045244/game-center-not-authenticating-using-swift

#import <GameKit/GameKit.h>

@interface GKLocalPlayerHack : NSObject

GKLocalPlayer *getLocalPlayer(void);

@end

在执行文件中:

// GKLocalPlayerHack.m
// Issue with GameKit and Swift
// https://stackoverflow.com/questions/24045244/game-center-not-authenticating-using-swift

#import "GKLocalPlayerHack.h"

@implementation GKLocalPlayerHack

GKLocalPlayer *getLocalPlayer(void)
{
    return [GKLocalPlayer localPlayer];
}

@end

请务必添加:

#import "GKLocalPlayerHack.h"

到您的桥接标题。
感谢@marmph在这个问题上的回答:

这篇关于斯威夫特 - 游戏中心无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:38