您好我试图调用一个函数来显示SKScene的GameCenter LeaderBoard,但我没有成功。以下是我从SKScene调用该函数的方法。
class Menu: SKScene {
func score(sender:UIButton!) {
GamecenterUtils.sharedGamecenterUtils.showLeaderboardOnViewController(GamecenterUtils(),leaderboardID:"myleaderID")
}
}
我收到编译错误' GamecenterUtils'不能转换为UIViewController。 但我这样做是因为GamecenterUtils是我的CGGameCenterControllDelegate。 这是GamecenterUtils类,而不是处理Game Center的所有内容:
private let _sharedGamecenterUtils = GamecenterUtils();
class GamecenterUtils : NSObject, GKGameCenterControllerDelegate {
class var sharedGamecenterUtils : GamecenterUtils{
return _sharedGamecenterUtils;
}
override init(){
}
func authenticateLocalUserOnViewController(viewController:UIViewController){
var localPlayer:GKLocalPlayer = GKLocalPlayer.localPlayer();
if (localPlayer.authenticated == false) {
localPlayer.authenticateHandler = {(authViewController, error) -> Void in
if (authViewController != nil) {
viewController.presentViewController(authViewController,animated:false,nil);
}
}
}
else {
println("Already authenticated");
}
}
func reportScore(score:Int,leaderboardID:NSString) {
var scoreReporter:GKScore = GKScore(leaderboardIdentifier:"catchthategg01");
scoreReporter.value = Int64(score);
scoreReporter.context = 0;
GKScore.reportScores([scoreReporter],{(error) -> Void in
if let reportError = error {
println("Unable to report score!\nError:\(error)");
}
else {
println("Score reported successfully!");
}
});
}
func showLeaderboardOnViewController(viewController:UIViewController?, leaderboardID:NSString){
if let containerController = viewController {
var gamecenterController:GKGameCenterViewController = GKGameCenterViewController();
gamecenterController.gameCenterDelegate = self;
gamecenterController.viewState = GKGameCenterViewControllerState.Leaderboards;
gamecenterController.leaderboardIdentifier = "catchthategg01";
viewController?.presentViewController(gamecenterController,animated:false,nil);
}
}
func gameCenterViewControllerDidFinish(_gameCenterViewController: GKGameCenterViewController!){
_gameCenterViewController.dismissViewControllerAnimated(false,nil);
}
}
感谢您的帮助。