如何在iOS 7中显示排行榜
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 7中显示排行榜相关的知识,希望对你有一定的参考价值。
我正在使用精灵套件和目标ios 7.我想在我的MenuScene中显示带按钮的排行榜。
我的助手方法代码。
#import "GameKitHelper.h"
NSString *const PresentAuthenticationViewController =
@"present_authentication_view_controller";
@interface GameKitHelper()<GKGameCenterControllerDelegate>
@end
@implementation GameKitHelper {
BOOL _enableGameCenter;
}
+ (instancetype)sharedGameKitHelper
{
static GameKitHelper *sharedGameKitHelper;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedGameKitHelper = [[GameKitHelper alloc] init];
});
return sharedGameKitHelper;
}
- (id)init
{
self = [super init];
if (self) {
_enableGameCenter = YES;
}
return self;
}
- (void)showGKGameCenterViewController:
(UIViewController *)viewController
{
if (!_enableGameCenter) {
NSLog(@"Local play is not authenticated");
}
GKGameCenterViewController *gameCenterViewController =
[[GKGameCenterViewController alloc] init];
gameCenterViewController.gameCenterDelegate = self;
gameCenterViewController.viewState =
GKGameCenterViewControllerStateAchievements;
[viewController presentViewController:gameCenterViewController
animated:YES
completion:nil];
}
我想在这堂课中使用Leaderboard Button。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"start"]) {
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}
if ([node.name isEqualToString:@"gameCenter"]) {
//HERE MY LEADERBOARD BUTTON ACTION
//I don't know what i write here...
{
}
我尝试了很多方法,但这些方法在iOS 6中工作,我针对iOS 7.我试过这个:[[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:self];
Xcode说自己Incompatible pointer types sending 'GameMenuScene *' to parameter of type 'UIViewController *'
答案
安德烈是对的。 gameKitHelper表示[gameKitHelper sharedGameKitHelper]。像这个例子
if ([node.name isEqualToString:@"gameCenter"]) {
UIViewController *vc = self.view.window.rootViewController;
[[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:vc];
}
继续编码:)
另一答案
if ([node.name isEqualToString:@"gameCenter"]) {
UIViewController *vc = self.view.window.rootViewController;
[[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:vc];
}
但实际上你应该从ViewController呈现另一个ViewController,而不是来自SKScene
另一答案
试试这个,它易于实现并且运行良好:
https://github.com/nihalahmed/GameCenterManager
在viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(openLeaderboard)
name:@"TestNotification"
object:nil];
内部VC:
- (void) openLeaderboard
{
// Open Leaderboards here
}
在你的场景中:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
以上是关于如何在iOS 7中显示排行榜的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 7 / iOS 9:在 SKNode 中使用 OpenGL 片段着色器未编译
显示排行榜崩溃应用程序,因为不支持 showViewController