show GKGameCenterViewController in SKScene

六月ゝ 毕业季﹏ 提交于 2019-12-24 14:15:32

问题


I struggling with the following Problem:

I've made a game with SpriteKit. I implemented the GameCenter to my game. It works. Player logs in automatically and the highscore will be added to the default Leaderboard. But for example in the "EndScreen" I want to show the GameCenterLeaderboard.

Appledocumentation tells me that I should use the following code:

- (void) showGameCenter

{

GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];

if (gameCenterController != nil)

{

gameCenterController.gameCenterDelegate = self;

[self presentViewController: gameCenterController animated: YES completion:nil];

}

}

But presentViewController does not work. Is there any way to switch from a SKScene to my standard ViewController. Or how can I show up the GameCenterleaderboard with a button touched?

To be honest I'm pretty new to programming so this issue here might not a big problem to solve for you guys. Big big thanks for the help.


回答1:


yes there is a way, you can call this code directly from -(void)showGameCenter:

  UIViewController *vc = self.view.window.rootViewController;
  [vc presentViewController: gameCenterController animated:YES completion:Nil];



回答2:


You can use notification to tell ViewController to show a leaderboard.

ViewController.m:

@implementation GameSceneViewController

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showGameCenter)
                                                 name:@"ShowLeaderboard"
                                               object:nil];
}

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

.....

SKScene.m:

- (void)showLeaderboard {    
        [[NSNotificationCenter defaultCenter] 
             postNotificationName:@"ShowLeaderboard"
                           object:nil
                         userInfo:nil];
}


来源:https://stackoverflow.com/questions/23077624/show-gkgamecenterviewcontroller-in-skscene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!