【问题标题】:How to show leaderboard in iOS 7如何在 iOS 7 中显示排行榜
【发布时间】:2023-03-17 15:13:01
【问题描述】:

我正在使用 sprite-kit 并针对 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];
}

我想在这个类中的排行榜按钮。

-(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 *'

【问题讨论】:

  • 有什么问题?
  • 我不知道。我在我的排行榜按钮中写的内容是我的行动。我知道这个问题很简单,这是我第一次体验游戏中心。如果你回答我,我会很高兴:)
  • 你有什么问题??
  • 如何使用我的按钮进入 Game Center 排行榜?

标签: ios ios7 sprite-kit game-center


【解决方案1】:

试试这个,它很容易实现并且效果很好:

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];

【讨论】:

  • 我不能使用它,我正在使用 sprite-kit。我在一个视图控制器中有 3 个场景。我试过这个:[[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:self]; Xcode 为自己说“不兼容的指针类型将 'GameMenuScene *' 发送到 'UIViewController *' 类型的参数”
  • 设置一个 NSNotification。在显示 SKScene 的 viewController 中,创建一个打开排行榜的方法。然后在 SKScene 中创建一个调用 NSNotification 的方法(参见上面的代码)
  • NSNOTIFICATION 对性能来说是个坏主意。顺便说一句谢谢我已经修好了:)
【解决方案2】:
if ([node.name isEqualToString:@"gameCenter"]) {
    UIViewController *vc = self.view.window.rootViewController;
    [[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:vc];
}

但实际上你应该从你的 ViewController 中展示另一个 ViewController,而不是来自 SKScene

【讨论】:

  • gameKitHelperInstance 是什么?
  • 这是GameKitHelper类的一个实例
  • 我不知道我的实例名称是什么,它不起作用。我会更新我的 gamekithelper.m 请帮助我
  • 我更新了有关我的 gameKitHelper 的更多详细信息。如果你谈论 sharedGameKit 助手它不起作用 Xcode 找不到这个。
  • 类和对象是iOS开发的一个非常基础的东西,绝对超出了这个问题的范围。您应该阅读更多相关信息。你可以从这里开始:developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…
【解决方案3】:

安德烈是对的。 gameKitHelper 表示 [gameKitHelper sharedGameKitHelper]。像这个例子

if ([node.name isEqualToString:@"gameCenter"]) {
        UIViewController *vc = self.view.window.rootViewController;
        [[GameKitHelper sharedGameKitHelper] showGKGameCenterViewController:vc];
    }

继续编码:)

【讨论】:

  • 哇,它正在工作,谢谢:)。安德烈回答但我不明白
猜你喜欢
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
相关资源
最近更新 更多