简体   繁体   中英

SKScene Leaderboards

I have a game organised in the following way:

  1. A menu (ViewController) - made in Storyboard - in which I authenticate the player to gamecenter and provide a button to go to the Leaderboards (this works perfectly well)

  2. A viewcontroller in which the SKScene is made which starts the game (GameScene)

  3. An SKscene called GameOverScene which appears when the game is lost.

I want to have a button on the GameOverScene to take me to the leaderboards

This is the code I used in the GameOverScene to show the leaderboards:

-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard{
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];

gcViewController.gameCenterDelegate = self;

if (shouldShowLeaderboard) {
    gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
    gcViewController.leaderboardIdentifier = @"wurst.highscore";
}
else{
    gcViewController.viewState = GKGameCenterViewControllerStateAchievements;
}
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController:gcViewController animated:YES completion:nil];

}

I run this piece of code when the appropriate button is touched and nothing happens: I get a log error or warning saying:

" Warning: Attempt to present on whose view is not in the window hierarchy!"

Why is it trying to present it on MenuViewController and not on GameViewController (which is the viewcontroller on which the SKScenes take place?

I would really appreciate the help

In your case there's a difference between your view's view controller and the current window's. I think it's a better idea to implement this with a delegate method from your scene to your view controller. Let the view controller's code present the leaderboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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