With the upcoming release of iOS 4.1 and GameCenter next week, I decided to try and add the new functionality to StackEm. Most of the implementation went without a hitch but there were some places I ran into issues and I thought I would share my experiences so others don’t run into the same issues.
One of the first things you will run into is how to add a ViewController to cocos2d. I scoured the cocos2d forums and found that other people had run into the issue but the solution wasn’t clear to me. Then in an ‘Aha!’ moment, it all became clear. You need to create a ViewController (a nil one is fine) and add it’s view property to the cocos2d glview.
Here’s what I mean, the following code will show the matchmaker:
UIViewController *tempVC=[[UIViewController alloc] init] ;
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:mmvc animated:YES];
Now you need to create the delegate for GKMatchmakerViewController and hide the modal when they click close:
- (void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController
You’ll want to release that UIViewController in your onExit function:
Edit: Thanks to Matt for finding how to fix the memory leak and multi-touch after dismissing game center.