0

I've been trying to implement Game Center in my Sprite Kit game, but every time I try to present the leaderboard...

-(void)showLeaderboard:(UIViewController*)gcvc {
    GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];
    if (leaderboardController != NULL)
    {
        leaderboardController.leaderboardIdentifier = @"Game_Leaderboard";
        leaderboardController.viewState = GKGameCenterViewControllerStateLeaderboards;
        leaderboardController.gameCenterDelegate = self;
        UIViewController *vc = self.view.window.rootViewController;
        [vc presentViewController: leaderboardController animated: YES completion:nil];
    }
}

The game crashes on startup with the error:

[UIView setShowsDrawCount:]: unrecognized selector sent to instance 0x178169e40 (lldb)

I've looked up related questions and all of them suggest changing the View's class in IB to SKView class, but no luck. The exception breakpoint stops at the line:

SKView *spriteView = (SKView *)self.view;
spriteView.showsDrawCount = YES;

And the view is clearly of type SKView, but it still says that spriteView is of type UIView. I'm not sure if it should even matter since the game's main view controller is a subclass of UIViewController, which is what is needed to present the Game Center Leaderboard so I have no idea how to fix this error.

evanlws
  • 93
  • 1
  • 12
  • Your issue would seem unrelated to the leaderboard. Where is line `(SKView *)self.view` and why do you say it is an `SKView` (how have you proved it)? – Wain May 29 '14 at 07:43
  • What is `self`? Are you certain that `self.view` is an `SKView`? If it isn't you can't just cast it this way and make it an `SKView`. That isn't how casting works. – Fogmeister May 29 '14 at 07:45
  • it is no skview and you are wrong... the runtime doesn't lie :) – Daij-Djan May 29 '14 at 08:06
  • The `(SKView *)self.view` is in my GameViewController's viewDidLoad method. It is used to create the View inside the View Controller from [Apple's Documentation](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/GettingStarted/GettingStarted.html#//apple_ref/doc/uid/TP40013043-CH2-SW1) In interface builder I've assigned the view inside the game's view controller to an SKView Class – evanlws May 29 '14 at 08:08
  • Do you have iAd enable? On SO are plenty of these issues because iAD apparently replaces the view controller's view (the SKView) with its AdBannerView. – CodeSmile May 29 '14 at 10:10
  • No I was actually debating implementing them right after I got the Game Center leaderboard working! I did consider alternatives so I will most likely use them – evanlws May 29 '14 at 16:47

2 Answers2

0

Well after a lot of tweaking, apparently there were a couple issues with the code. UIViewController *vc = self.view.window.rootViewController; wasn't needed since I was already passing the view controller as a parameter, and instead of following Apples code for creating the SKView, I simply connected an IBOutlet and got rid of SKView *spriteView = (SKView *)self.view; I would have never even thought to change the viewDidLoad method so thanks guys!

evanlws
  • 93
  • 1
  • 12
0

For what it's worth, I usually obtain the SKView from the main UIViewController like so:

/// Returns the Director's own view, but as a SKView *.
@property (weak, readonly) SKView * skview;

// ...

- (SKView *) skview {
  return (SKView *)self.view;
}
CloakedEddy
  • 1,965
  • 15
  • 27