游戏中心分数提交

时间:2014-05-03 14:02:10

标签: ios sprite-kit game-center

在GCHelper.h中

#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@interface abGCHelper : NSObject {

BOOL gameCenterAvailable;
BOOL userAuthenticated;

}

@property (assign, readonly) BOOL gameCenterAvailable;
-(void)reportScore:(int64_t)score forLeaderboardID:(NSString*)identifier;

 @end

在GCHelper.m

#import "abGCHelper.h"

@implementation abGCHelper

@synthesize gameCenterAvailable;

 static abGCHelper *_sharedHelper = nil;

 -(abGCHelper*)defaultHelper {

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

    static dispatch_once_t pred = 0;
    dispatch_once(&pred, ^{
        _sharedHelper = [[abGCHelper alloc] init];
    });
    return _sharedHelper;
}

- (BOOL)isGameCenterAvailable {

// check for presence of GKLocalPlayer API
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));

// check if the device is running iOS 4.1 or later
NSString *reqSysVer = @"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer
                                       options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);

}

 - (id)init {

if ((self = [super init])) {

    gameCenterAvailable = [self isGameCenterAvailable];

    if (gameCenterAvailable) {

        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

        [nc addObserver:self selector:@selector(authenticationChanged)     name:GKPlayerAuthenticationDidChangeNotificationName object:nil];

          }

     }

    return self;

}


 - (void)authenticateLocalUserOnViewController:(UIViewController*)viewController
                       setCallbackObject:(id)obj
                       withPauseSelector:(SEL)selector
{

if (!gameCenterAvailable) return;

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

NSLog(@"Authenticating local user...");

if (localPlayer.authenticated == NO) {

    [localPlayer setAuthenticateHandler:^(UIViewController* authViewController, NSError *error)     {

        if (authViewController != nil) {

            if (obj) {

                [obj performSelector:selector withObject:nil afterDelay:0];

            }

            [viewController presentViewController:authViewController animated:YES completion:^ {

            }];

        } else if (error != nil) {

            // process error
        }

    }];

}

    else {

    NSLog(@"Already authenticated!");
    }

 }

-(void)authenticationChanged {

 if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {

    NSLog(@"Authentication changed: player authenticated.");

    userAuthenticated = TRUE;

    // Load the leaderboard info
    // Load the achievements

} else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {

    NSLog(@"Authentication changed: player not authenticated.");

    userAuthenticated = FALSE;

   }

}

-(void)reportScore:(int64_t)score forLeaderboardID:(NSString*)identifier
{

   GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
    scoreReporter.value = score;
     scoreReporter.context = 0;

     [GKScore reportScores:@[scoreReporter] withCompletionHandler:^(NSError *error) {
    if (error == nil) {
        NSLog(@"Score reported successfully!");
    } else {
        NSLog(@"Unable to report score!");
    }
}];
}

我参考了在线教程来做到这一点。现在,问题是:

  1. 如何提交MyScene.m中定义的highScore?

  2. 我必须在哪里放置我的排行榜ID?

  3. 请帮忙 感谢...

0 个答案:

没有答案