2013-10-29 122 views
1

我想在我的代碼中實現GKChallengeListener,並且我使用了Apple的GameCenterManager類。玩家被認證爲優秀並且排行榜,並且挑戰也是。 But i want to notify my application when the remote player completed the challenge. for this i have used GKChallengeListener protocols. but they are not calling up when i am sending or receiving the challenges參考。但我DNT瞭解哪一個類將調用delgate自我 https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKEventListener_Ref/Reference/Reference.html#//apple_ref/occ/intf/GKChallengeListener哪些類用於實現GKLocalPlayerListener協議

在ViewController.h

@interface ViewController : UIViewController <UIActionSheetDelegate, GameCenterManagerDelegate,GKGameCenterControllerDelegate,GKChallengeListener> { 
    GameCenterManager *gameCenterManager; 
    NSInteger currentScore; 
    NSString* currentLeaderBoard; 
    IBOutlet UILabel *currentScoreLabel; 
} 
@property (nonatomic, retain) GameCenterManager *gameCenterManager; 
@property (nonatomic, assign) NSInteger currentScore; 
@property (nonatomic, retain) NSString* currentLeaderBoard; 
@property (nonatomic, retain) UILabel *currentScoreLabel; 
- (IBAction) showLeaderboard; 
- (IBAction) increaseScore; 

@end

在ViewController.m

@implementation ViewController 

@synthesize gameCenterManager; 
@synthesize currentScore; 
@synthesize currentLeaderBoard; 
@synthesize currentScoreLabel; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.currentLeaderBoard = kLeaderboardID; 
    self.currentScore = 0; 
    if ([GameCenterManager isGameCenterAvailable]) { 
     self.gameCenterManager = [[GameCenterManager alloc] init]; 
     [self.gameCenterManager setDelegate:self]; 
     [self.gameCenterManager authenticateLocalUser]; 
    } else { 
     // The current device does not support Game Center. 
    } 
} 

-(void)player:(GKPlayer *)player issuedChallengeWasCompleted:(GKChallenge *)challenge byFriend:(GKPlayer *)friendPlayer{ 
    NSLog(@"issued challenge was completed by friend"); 

} 
-(void)player:(GKPlayer *)player didCompleteChallenge:(GKChallenge *)challenge issuedByFriend:(GKPlayer *)friendPlayer{ 
    NSLog(@"player did complete challenge"); 
} 
-(void)player:(GKPlayer *)player didReceiveChallenge:(GKChallenge *)challenge{ 
    NSLog(@"player did recieve challenge"); 
} 
-(void)player:(GKPlayer *)player wantsToPlayChallenge:(GKChallenge *)challenge{ 
    NSLog(@"player wants to play challenge "); 
} 
+0

請加評論,如果你downvote – iOS

回答

3

這是在貶值iOS 7.0,但你仍然可以使用它作爲替代

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.currentLeaderBoard = kLeaderboardID; 
    self.currentScore = 0; 
    if ([GameCenterManager isGameCenterAvailable]) { 

     self.gameCenterManager = [[GameCenterManager alloc] init]; 
     [self.gameCenterManager setDelegate:self]; 
     [self.gameCenterManager authenticateLocalUser]; 
    } else { 
     // The current device does not support Game Center. 
    } 
GKChallengeEventHandler 
    *gk 
    =[GKChallengeEventHandler challengeEventHandler].delegate=self; 
} 

- (void)localPlayerDidCompleteChallenge:(GKChallenge *)challenge 
{ 
    NSLog(@"localPlayerDidCompleteChallenge"); 
} 
- (void)localPlayerDidReceiveChallenge:(GKChallenge *)challenge 
{ 
    NSLog(@"localPlayerDidReceiveChallenge"); 
} 
- (void)localPlayerDidSelectChallenge:(GKChallenge *)challenge{ 
    NSLog(@"localPlayerDidSelectChallenge"); 
} 
- (void)remotePlayerDidCompleteChallenge:(GKChallenge *)challenge{ 
    NSLog(@"remotePlayerDidCompleteChallenge"); 
} 
- (BOOL)shouldShowBannerForLocallyCompletedChallenge:(GKChallenge *)challenge 
{ 
    return YES; 
} 
- (BOOL)shouldShowBannerForLocallyReceivedChallenge:(GKChallenge *)challenge 
{ 
    return YES; 
} 
- (BOOL)shouldShowBannerForRemotelyCompletedChallenge:(GKChallenge *)challenge 
{ 
    return YES; 
} 
2

今天就爲自己掙扎了一下。

訣竅是將實現GKLocalPlayerListener協議的對象註冊爲localPlayer的偵聽器。呃......那句話似乎已經循環回來了,但這可能有幫助。

/* this happens inside my authenticateLocalPlayer method */ 
if ([GKLocalPlayer localPlayer].authenticated) { 
    [[GKLocalPlayer localPlayer] registerListener:self]; 
    // More stuff here 
    } 

然後在同一個對象中實現協議方法。

編輯:哦!而且你不應該實現GKChallengeListener。你只應該實現GKLocalPlayerListener。 (我打算髮佈一個鏈接,但如果我現在可以找到它,就會發生危險)。

希望有所幫助。