问题
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