2014-04-07 35 views
0

我試圖在用戶按下完成時關閉Game Center,爲什麼這不起作用?我已經看過蘋果文檔,並用Google的方式Google搜索,發生了什麼問題?遊戲中心沒有解僱?

GCHelper.h

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

@protocol GameCenterManagerDelegate 

@end 

@interface GCHelper : NSObject <GKGameCenterControllerDelegate, GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate> { 

BOOL gameCenterAvailable; 
BOOL userAuthenticated; 

UIViewController *presentingViewController; 
id <GameCenterManagerDelegate> delegate; 

} 
@property (nonatomic, strong) id <GameCenterManagerDelegate> delegate; 
@property (assign, readonly) BOOL gameCenterAvailable; 
@property (retain) UIViewController *presentingViewController; 

+ (GCHelper *) showGameCenter; 
+ (GCHelper *)sharedInstance; 
- (void)authenticateLocalUser; 

@end 

GCHelper.m

#import "GCHelper.h" 

@implementation GCHelper 

@synthesize gameCenterAvailable; 
@synthesize presentingViewController; 
@synthesize delegate; 


#pragma mark Initialization 

static GCHelper *sharedHelper = nil; 
static GCHelper *showGameCenter = nil; 

+ (GCHelper *) sharedInstance { 
if (!sharedHelper) { 
    sharedHelper = [[GCHelper 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)authenticationChanged { 

if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) { 
    NSLog(@"Authentication changed: player authenticated."); 
    userAuthenticated = TRUE; 
} else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) { 
    NSLog(@"Authentication changed: player not authenticated"); 
    userAuthenticated = FALSE; 
} 

} 

#pragma mark User functions 

- (void)authenticateLocalUser { 

if (!gameCenterAvailable) return; 

NSLog(@"Authenticating local user..."); 
if ([GKLocalPlayer localPlayer].authenticated == NO) { 
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil]; 
} else { 
    NSLog(@"Already authenticated!"); 
} 
} 


/* 
- (void) showBanner 

{ 

NSString* title = [self generateTitle]; 

NSString* message = [self generateBannerMessage]; 

[GKNotificationBanner showBannerWithTitle: title message: message 

         completionHandler:^{ 

          [self advanceToNextInterfaceScreen] 

         }]; 

} 
*/ 

+ (GCHelper *)showGameCenter 
{ 
@synchronized (self) 
{ 
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init]; 

if (gameCenterController != NULL) 

{ 

    [[CCDirector sharedDirector]presentViewController:gameCenterController animated:YES completion:nil]; 

} 
} 
return showGameCenter; 
} 

// When player dismisses game center. 
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController 

{ 

gameCenterViewController.gameCenterDelegate = self; 
[gameCenterViewController dismissModalViewControllerAnimated:YES]; 

} 


@end 

回答

2

這不是一個需要被解僱,你在功能-(void)gameCenterViewControllerDidFinish接收參數的導演,但GKGameCenterViewController 。你應該這樣做:

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

編輯:你也有你的控制器的代表在類的.h文件設置爲當前場景和實施<GKGameCenterControllerDelegate>

//In your .h file 
@interface foo : CCScene <GKGameCenterControllerDelegate>{ 
} 

//In you .m file 
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init]; 

if (gameCenterController != NULL) 
{ 
    gameCenterController.gameCenterDelegate = self; 
    [[CCDirector sharedDirector]presentViewController:gameCenterController animated:YES completion:nil]; 
} 
+0

感謝你的答案,但它沒有奏效。 – Jason

+1

你必須將你的視圖控制器的'delegate'屬性設置爲你當前的場景。查看我更新後的完整代碼的答案! – RaphBlanchet

+0

這是我在執行此操作時遇到的錯誤。 '不兼容的指針類型'從'Class'分配給'id ' – Jason