cocos2d Scene goes black after GameCenter leaderboard dismissal - iphone

Im having a strange problem that only occurs on the iPad version of my game. When I bring up the GameCenter leaderboard, it appears like normal. No issues there. But when you tap the close button the leader board disappears and the cocos2d scene goes black. No errors, nothing. Nothing I do can bring the screen back. Ive tried resuming the director and even tried some solutions on a similar thread.
Here is my code in the cocos2d scene. GCController is just a subclass of the RootViewController, nothing special:
tempVC = [[GCController alloc] init];
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil)
{
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardController.category = [NSString stringWithFormat:#"%#%#", [self cleanString:selectedSong], #"TotalHD"];
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:leaderboardController animated:YES];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[tempVC dismissModalViewControllerAnimated:YES];
[tempVC.view.superview removeFromSuperview];
[tempVC release];
}
Anyone have any ideas? If I can provide any more information let me know. Thanks.

You are not providing enough code. I see no fault in the code you are showing at the moment. Your most likely cause of a black screen (depending on how you do things) is that timers have been interrupted and you may need to reset them with
- (void) onEnter;
or
- (void) onEnterTransitionDidFinish;
If that isn't it then post more code and I will check back. We need to see when the view is presented and what it should return to.

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[tempVC dismissModalViewControllerAnimated:YES];
**[tempVC.view removeFromSuperview];**
}

Related

Dismissing camera view crashes app

When I try to dismiss my UIImagePickerController, it crashes the app. the error is: "Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'"
I have the preferred interface orientation set in my view controller.
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
- (BOOL) shouldAutorotate {
return YES;
}
Here is the method I'm calling to bring up the camera, this works fine for adding the camera, but like I said, crashes when I try to remove the camera.
-(IBAction)addCamera:(id)sender
{
self.cameraController = [[UIImagePickerController alloc] init];
self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraController.cameraViewTransform = CGAffineTransformScale(self.cameraController.cameraViewTransform,
1.13f,
1.13f);
self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;
self.wantsFullScreenLayout = YES;
ar_overlayView = [[UIView alloc] initWithFrame:CGRectZero];
self.view = ar_overlayView;
[self.cameraController setCameraOverlayView:ar_overlayView];
[self presentViewController:cameraController animated:NO completion:nil];
[ar_overlayView setFrame:self.cameraController.view.bounds];
}
-(IBAction)back:(id)sender
{
[ar_overlayView removeFromSuperview];
[cameraController dismissViewControllerAnimated:NO completion:nil];
}
Alright, found the solution, it was really simple, I just changed my back method to:
[self dismissModalViewControllerAnimated:YES];
Now my camera goes away and I can see my original view when I press the back button.
I still haven't figured out what was causing the original problem as I've gone through the info.plist and the methods for supported orientations, but this accomplishes what I wanted.
I'm still curious as to what was causing the error though if anyone has any ideas.
You can try remove this method:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
It will fix it.but sometimes it will lead to reduce stateBar height.
You cannot remove a UIViewController's main view from its superview.
Instead of this:
self.view = ar_overlayView;
Try this:
[self.view addSubview:ar_overlayView];
Then you will be able to remove it from the superview correctly.
You should be using the didFinishPickingMedieWithInfo method similar to below and use [self dismissModalViewControllerAnimated:YES];
-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
NSLog(#"Camera");
}
else {
NSLog(#"Album");
}
[self dismissModalViewControllerAnimated:YES];
}
I don't think you need to add ar_overlayView to the current view yourself if it's a camera overlay.
Here's what your code is doing now:
Add ar_overlayView to the current view
Add ar_overlayView as the camera's overlay view
Show the camera view (modal)
At this point, ar_overlayView is being displayed twice. When you send it the removeFromSuperview message on dismissing the camera view, it might be getting confused since it's in two view hierarchies at the same time.
Skipping the self.view = ar_overlayView; or [self.view addSubview:ar_overlayView]; lines should fix the problem.
Also, dismissViewControllerAnimated:completion: should be sent to the same object that presentViewController:animated:completion was called on (self in this case):
-(IBAction)addCamera:(id)sender
{
// -- snip -- //
ar_overlayView = [[UIView alloc] initWithFrame:self.cameraController.view.bounds];
[self.cameraController setCameraOverlayView:ar_overlayView];
[self presentViewController:self.cameraController animated:NO completion:nil];
}
-(IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:NO completion:nil];
}

Game center "Done" button doesn't work

I am showing a Game Center Leaderboard but after it appears I can't close it—the "Done" button doesn't work.
Here is my code for RootViewController.m:
-(void) showLeaderboard {
GKLeaderboardViewController *leaderboardVC [GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardVC != nil) {
[leaderboardVC setDelegate: self];
[leaderboardVC setCategory: kLeaderboardID];
[leaderboardVC setTimeScope: GKLeaderboardTimeScopeWeek];
[self presentModalViewControllerAnimated: YES];
}
}
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
NSLog(#"Close leaderboard");
[self dismissModalViewControllerAnimated: YES];
}
What's also strange is that I never see "Close leaderboard" in the log.
Did you add GKLeaderboardViewControllerDelegate to the list of protocols that this class implements? (leaderboardViewControllerDidFinish is the only method in this protocol). Your code looks perfect so this is the only thing I can think of.

Presenting Game Center problems

I am trying to add High Scores in my game, and I don't know why i can't present the Leader boards. I can connect the user and send data. But can't see it.
My Game its in Cocos2d. But my MENU isn't when the user click on Play CCDirector Start
My main windows structure its:
-Navigation Contoller
- Menu -Ui View controller
- Navigation Item
I try to do it like a navigation and push it and crash. Also add it as subview. And crash. My leader boards code
- (IBAction)showLeader:(id)sender {
if ([self isGameCenterAvailable]) {
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
//[self presentModalViewController:leaderboardController animated:YES];
[self.navigationController pushViewController:leaderboardController animated:YES];
}
}
}
-(BOOL)isGameCenterAvailable {
// Check for presence of GKLocalPlayer API.
Class gcClass = (NSClassFromString(#"GKLocalPlayer"));
// The device must be running running iOS 4.1 or later.
NSString *reqSysVer = #"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
What could i do?
And another question. Could i put my own effect to present the leader bords scores? Or apple will reject it?
I am trying to do a small framework. So i don't need to codify allot in the Menu Game etc..
Thanks :D
I found how to do this.
menu2Game = [[Menu2Game alloc] initWithNibName:#"Menu2Game" bundle:nil];
menu2Game.wantsFullScreenLayout = YES;
[self.view addSubview:menu2Game.view];
[menu2Game presentModalViewController:leaderboardController animated:YES];
menu2Game its a clean View Controller. Maybe I can create a view controller temporally here. But I need to save it somewhere so i could dismiss it.
It work. But i am trying to do it in a landscape

Dismissing GameKit modal view

I am trying to integrate Apple's game center into my application. I can successfully post scores to the leader board, and show the leader board, but the problem comes when I try to dismiss the leader board modal view. I've followed apple's code direction from the Game Kit Programming Guide ([url]http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html[/url]).
My code is as follows for Game Center:
-(BOOL)isGameCenterAvailable{
// Check for presence of GKLocalPlayer class.
BOOL localPlayerClassAvailable = (NSClassFromString(#"GKLocalPlayer")) != nil;
// The device must be running iOS 4.1 or later.
NSString *reqSysVer = #"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (localPlayerClassAvailable && osVersionSupported);
}
- (void) authenticateLocalPlayer
{
if([self isGameCenterAvailable]){
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
// Perform additional tasks for the authenticated player.
}
}];
}
}
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
// handle the reporting error
}
}];
}
- (void) showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController: leaderboardController animated: YES];
}
//[leaderboardController release];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
if([self modalViewController] != nil){
[self dismissModalViewControllerAnimated:YES];
}
}
-(IBAction)show{
[self showLeaderboard];
}
-(IBAction)submit{
[self reportScore:9 forCategory:kLeaderboardID];
}
Xcode tells me the problem line is [self dismissModalViewControllerAnimated:YES]; it says I'm getting bad access, which I know means I'm trying to access a bad pointer, but I don't see why anything wouldn't be invalid. Self reports that it has a modalviewcontroller. I've tried all sorts of variants, and I'm completely baffled as to why it is giving me errors.
Any help or suggestions would be greatly appreciated.
Thanks in advance!
I just had a very similar issue on my App. I discovered that it was not related to the ModalViewController itself but the view controller displaying it.
If you profile the app using the zombies option in the Profiler, you will be able to see that something is being released that should not be (Most likely a UIImage or UIView). You should be able to track down the function where the zombied object was allocated to find the real object causing the trouble.
I am supposing that the reason the error shows when the ModalViewController is dissmissed is that various view elements are called to redraw or refresh after the dialog goes away and then something gets accessed that was released when it shouldn't have been.
Hope this helps.

lost control in LeaderBoard - GameCenter

I want to add Game center for my project but now I get some stuck and can't find any solution for my problem by Google :(
When I call method showleaderboard in my project, leaderboard appear and success to load my score in gamecenter but it don't receive touch (this screen look like freeze).
This is my code:
-(void) ShowLeaderBoardCategory:(NSString *)my_category
{
GKLeaderboardViewController * leaderboardController = [[GKLeaderboardViewController alloc] init];
if(leaderboardController != nil)
{
leaderboardController.category = my_category;
leaderboardController.leaderboardDelegate = mySubView;
[mySubView presentModalViewController: leaderboardController animated: YES];
[glView addSubview:mySubView.view];
}
[leaderboardController release];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[mySubView dismissModalViewControllerAnimated:YES];
[mySubView release];
[viewController.view removeFromSuperview];
[viewController release];
}
mySubView is interface I define:
#interface MyUIView : UIViewController<GKLeaderboardViewControllerDelegate>
......
and used it:
MyUIView *mySubView;
Please, tell me what wrong in my code? :((
What reason can there be to stop receiving cocos2d events?
Thanks for reading and hope your hint.
[viewController.view.superview removeFromSuperview];
worked for me.
The problem is in your removal method: where viewController refers to the leaderboard:
[mySubView dismissModalViewControllerAnimated:YES]; //this removes the modally presnted leaderboard.
[mySubView removeFromSuperview]; //this should show up the glView