dakiki
1
Hi,
I’d like to add GameCenter to my game.
For that, I need an access to the game’s viewcontroller.
I successfully did it on iOS doing that
GKMatchmakerViewController* mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
_mainViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[_mainViewController presentViewController:mmvc animated:YES completion:nil];
But now, I don’t know how to do the same on MacOS.
I have a GKMatchmakerViewController that I need to present.
Any idea on how to do that using cocos2d-x (>= 3.6)?
Thanks
dakiki
2
Ok, finally found some time to try to do this.
For those interested, here’s how to create a valid (empty) NSViewController on MacOS (required for some GameCenter features):
First, you create a custom NSViewController class:
#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC
@interface MyMainViewController : NSViewController
@end
@implementation MyMainViewController
-(void)loadView
{
self.view = [[NSView alloc] init];
}
@end
#endif /* !MAC */
Then you initialize a “_mainViewController” variable of type NSViewController (UIViewController on iOS):
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
_mainViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
#else
_mainViewController = [[MyMainViewController alloc] init];
auto mainWindow = [[NSApplication sharedApplication] mainWindow];
[mainWindow.contentView addSubview:_mainViewController.view];
_mainViewController.view.frame = ((NSView*)mainWindow.contentView).bounds;
#endif
Finally when you need to prevent a viewcontroller, use your “_mainViewController” variable, like for creation a multiplayer games:
GKMatchmakerViewController* mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
[_mainViewController presentViewController:mmvc animated:YES completion:nil];
#else
[_mainViewController presentViewControllerAsModalWindow:mmvc];
#endif
Hope this will help some ppl wanting to create games using GameCenter on both iOS and MacOS!
kiki