GameCenter在另一个场景中显示排行榜

时间:2014-08-03 17:58:05

标签: ios objective-c sprite-kit game-center

我在ViewController的SpriteKit游戏中启用了Game Center功能。一切正常,但我想在触摸按钮后在另一个场景中显示排行榜。我正确地导入了一切。触摸了HighScoreButton'之后,我的项目崩溃了。在Head.m文件中,输出如下:

'NSInvalidArgumentException', reason: '-[UIView presentScene:]: unrecognized selector sent to instance 0x7feec2ff2920'

我最近的代码不起作用:

ViewController.h

@interface ViewController : UIViewController <GKGameCenterControllerDelegate> 
- (void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard;
+ (ViewController*)defaultHelper;
@end

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

// Configure the view.
SKView * skView = (SKView *)self.view;

[self authenticateLocalPlayer];

// Create and configure the scene.
SKScene * scene = [Head sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;

self.canDisplayBannerAds = YES;

// Present the scene.
[skView presentScene:scene];

}

static ViewController *_sharedHelper = nil;

+ (ViewController*)defaultHelper {

// dispatch_once will ensure that the method is only called once (thread-safe)

static dispatch_once_t pred = 0;

dispatch_once(&pred, ^{

    _sharedHelper = [[ViewController alloc] init];

});

return _sharedHelper;

}

-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard{

    GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
    gcViewController.gameCenterDelegate = self;

    if (shouldShowLeaderboard) {
        gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
        gcViewController.leaderboardIdentifier = _leaderboardIdentifier;
    }
    else{
        gcViewController.viewState = GKGameCenterViewControllerStateAchievements;
    }
    [self presentViewController:gcViewController animated:YES completion:nil];
}

-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
    [gameCenterViewController dismissViewControllerAnimated:YES completion:nil];
}

Head.m

if ([Node.name isEqualToString:@"HighScoreButton"]){
    [[ViewController defaultHelper] showLeaderboardAndAchievements:YES];
}

1 个答案:

答案 0 :(得分:0)

我认为问题出在你的视图控制器的单例初始化(这是正确的btw)。

第一次加载时,视图控制器被创建&#34;在幕后&#34; (我不确定究竟在哪里,但我会查找它),并使用skView实例初始化其视图。 (并且因为它不是由您创建的,您的单例实例未初始化并且使用了不同的ViewController实例)

由于您的第一次创建视图控制器时未使用defaultHelper方法,因此您正在创建一个新视图控制器,其视图是UIView的实例,并且它不符合{{ 1}}方法(例如SKViewsetShowFPS)。当然,因为您创建了一个新的视图控制器,所以它不是视图层次结构的一部分。

我建议您检索视图控制器,如下所示:

presentScene