简体   繁体   English

UIView不会转换为SKView

[英]UIView won't cast to SKView

I've been trying to implement Game Center in my Sprite Kit game, but every time I try to present the leaderboard... 我一直试图在我的Sprite Kit游戏中实现Game Center,但是每次我尝试展示排行榜时...

-(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) [UIView setShowsDrawCount:]:无法识别的选择器已发送到实例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. 我查找了相关的问题,所有这些问题都建议将IB中的View类更改为SKView类,但是没有运气。 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. 并且该视图显然是SKView类型的,但是它仍然说spriteView是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. 我不确定这是否重要,因为游戏的主视图控制器是UIViewController的子类,而这是呈现Game Center排行榜所需要的,因此我不知道如何解决此错误。

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; 因为我已经将视图控制器作为参数传递了,所以不需要它,而无需遵循Apple的代码来创建SKView,而是直接连接了IBOutlet并摆脱了SKView *spriteView = (SKView *)self.view; I would have never even thought to change the viewDidLoad method so thanks guys! 我什至从未想过要更改viewDidLoad方法,所以谢谢大家!

For what it's worth, I usually obtain the SKView from the main UIViewController like so: 对于它的价值,我通常像这样从主UIViewController获得SKView:

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

// ...

- (SKView *) skview {
  return (SKView *)self.view;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM