如何在iOS 7中显示排行榜

时间:2014-09-17 18:15:11

标签: ios ios7 sprite-kit game-center

我正在使用精灵套件和目标iOS 7.我想在我的MenuScene中显示带按钮的排行榜。

我的助手方法代码。

#import "GameKitHelper.h"

NSString *const PresentAuthenticationViewController =
@"present_authentication_view_controller";

@interface GameKitHelper()<GKGameCenterControllerDelegate>
@end

@implementation GameKitHelper {
    BOOL _enableGameCenter;
}

+ (instancetype)sharedGameKitHelper
{
    static GameKitHelper *sharedGameKitHelper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedGameKitHelper = [[GameKitHelper alloc] init];
    });
    return sharedGameKitHelper;
}

- (id)init
{
    self = [super init];
    if (self) {
        _enableGameCenter = YES;
    }
    return self;
}


- (void)showGKGameCenterViewController:
(UIViewController *)viewController
{
    if (!_enableGameCenter) {
        NSLog(@"Local play is not authenticated");
    }
    GKGameCenterViewController *gameCenterViewController =
    [[GKGameCenterViewController alloc] init];
    gameCenterViewController.gameCenterDelegate = self;
    gameCenterViewController.viewState =
    GKGameCenterViewControllerStateAchievements;
    [viewController presentViewController:gameCenterViewController
                                 animated:YES
                               completion:nil];
}

我想在这堂课中使用Leaderboard Button。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if ([node.name isEqualToString:@"start"]) {
        MyScene *myScene = [MyScene sceneWithSize:self.size];
        [self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
    }
    if ([node.name isEqualToString:@"gameCenter"]) {
        //HERE MY LEADERBOARD BUTTON ACTION
        //I don't know what i write here...
   {
}

我尝试了很多方法,但这些方法在iOS 6中工作,我的目标是iOS 7。 我试过这个: [[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:self]; Xcode为自己说 Incompatible pointer types sending 'GameMenuScene *' to parameter of type 'UIViewController *'

3 个答案:

答案 0 :(得分:0)

if ([node.name isEqualToString:@"gameCenter"]) {
    UIViewController *vc = self.view.window.rootViewController;
    [[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:vc];
}

但实际上你应该从ViewController中呈现另一个ViewController,而不是来自SKScene

答案 1 :(得分:0)

试试这个,它很容易实现并且效果很好:

https://github.com/nihalahmed/GameCenterManager

在viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(openLeaderboard) 
    name:@"TestNotification"
    object:nil];

VC内部:

   - (void) openLeaderboard
{

    // Open Leaderboards here


}

在你的场景中:

[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];

答案 2 :(得分:0)

安德烈是对的。 gameKitHelper表示[gameKitHelper sharedGameKitHelper]。像这个例子

if ([node.name isEqualToString:@"gameCenter"]) {
        UIViewController *vc = self.view.window.rootViewController;
        [[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:vc];
    }

继续编码:)