我正在尝试将游戏结束时的分数添加到游戏中心,但它不会给游戏中心增加任何内容。我整天都在寻找我犯的“错误”,但我什么都没看到?
以下是我们的一些代码。
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
@class GKLeaderboard, GKAchievement, GKPlayer;
int counter;
@protocol TopscoreDelegate <NSObject>
@optional
- (void) processGameCenterAuth: (NSError*) error;
- (void) scoreReported: (NSError*) error;
- (void) reloadScoresComplete: (GKLeaderboard*) leaderBoard error: (NSError*) error;
@end
@interface Topscore : UIViewController <NSObject> {
AVAudioPlayer *audioPlayer;
IBOutlet UILabel *count;
Topscore* Topscore;
}
- (IBAction)tweetTapped:(id)sender;
- (IBAction) subScore;
- (IBAction) showLeader;
- (void) reportScore;
@property (nonatomic, assign) id <TopscoreDelegate> delegate;
+ (BOOL) isGameCenterAvailable;
- (void) authenticateLocalUser;
@property (nonatomic, retain) Topscore *Topscore;
@property (nonatomic, retain) NSString* currentLeaderBoard;
@property (nonatomic, retain) UILabel *count;
@property(nonatomic, readonly, retain) NSDate *date;
- (void) reportScore: (int64_t) score forCategory: (NSString*) category;
- (void) reloadHighScoresForCategory: (NSString*) category;
@end
.m文件看起来像;
我在xib文件中有一个按钮,它连接到IBAction Subscore
。我没有收到错误,只收到一些警告。
PerformSelector may cause a leak because its selector is unknown
此警告在线;
[委托performSelector:选择器withObject:arg withObject:err];
@interface Topscore ()
@property (strong, nonatomic) NSString *imageString;
@property (strong, nonatomic) NSString *urlString;
- (void)clearLabels;
@end
@implementation Topscore
@synthesize count, date, urlString = _urlString, imageString = _imageString, delegate, Topscore, currentLeaderBoard;
- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
assert([NSThread isMainThread]);
if([delegate respondsToSelector: selector])
{
if(arg != NULL)
{
[delegate performSelector: selector withObject: arg withObject: err];
}
else
{
[delegate performSelector: selector withObject: err];
}
}
else
{
NSLog(@"Missed Method");
}
}
- (void) callDelegateOnMainThread: (SEL) selector withArg: (id) arg error: (NSError*) err
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self callDelegate: selector withArg: arg error: err];
});
}
//..
+ (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);
}
- (void) authenticateLocalUser
{
if([GKLocalPlayer localPlayer].authenticated == NO)
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
{
[self callDelegateOnMainThread: @selector(processGameCenterAuth:) withArg: NULL error: error];
}];
}
}
- (void) reloadHighScoresForCategory: (NSString*) category
{
GKLeaderboard* leaderBoard= [[GKLeaderboard alloc] init];
leaderBoard.category= category;
leaderBoard.timeScope= GKLeaderboardTimeScopeAllTime;
leaderBoard.range= NSMakeRange(1, 1);
[leaderBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error)
{
[self callDelegateOnMainThread: @selector(reloadScoresComplete:error:) withArg: leaderBoard error: error];
}];
}
//..
- (void) showAlertWithTitle: (NSString*) title message: (NSString*) message
{
UIAlertView* alert= [[UIAlertView alloc] initWithTitle: title message: message
delegate: NULL cancelButtonTitle: @"OK" otherButtonTitles: NULL];
[alert show];
}
- (void) scoreReported: (NSError*) error;
{
if(error == NULL)
{
[self.Topscore reloadHighScoresForCategory: self.currentLeaderBoard];
[self showAlertWithTitle: @"High Score Reported!"
message: [NSString stringWithFormat: @"", [error localizedDescription]]];
}
else
{
[self showAlertWithTitle: @"Score Report Failed!"
message: [NSString stringWithFormat: @"Reason: %@", [error localizedDescription]]];
}
}
- (IBAction) subScore {
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"01"];
scoreReporter.value = counter;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
NSLog(@"Failed: %i", counter);
} else {
NSLog(@"Succes: %i", counter);
[self callDelegateOnMainThread: @selector(scoreReported:) withArg: NULL error: error];
}
}];
}
如果你需要更多代码,请问我!
PS:领导者出现了但是点击按钮后发布/添加没有添加的分数(IBAction)
答案 0 :(得分:3)
关于Game Center Scores没有出现,我回答了类似的问题。
您至少需要从不同的帐户中发布一个至少两个分数:Solution
同样应该是要求GC的分数,因为如果董事会中只有1个人,它将不会返回任何分数。