我正在尝试在我的代码中实现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
引用。但是我知道哪个班级会把德尔盖特称为自己
https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKEventListener_Ref/Reference/Reference.html#//apple_ref/occ/intf/GKChallengeListener
在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 :(得分: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;
}
答案 1 :(得分:2)
今天我自己挣扎了一下。
技巧是将实现GKLocalPlayerListener协议的对象注册为localPlayer的侦听器。呃......那句话好像已经回归了,但这可能会有所帮助。
/* this happens inside my authenticateLocalPlayer method */
if ([GKLocalPlayer localPlayer].authenticated) {
[[GKLocalPlayer localPlayer] registerListener:self];
// More stuff here
}
然后在同一个对象中实现协议方法。
编辑:哦!而且你不应该实现GKChallengeListener。您应该只实现GKLocalPlayerListener。 (我打算发布一个链接,但如果我现在可以找到它,那就很危险。)希望有所帮助。