Game Center Leaderboards with cocos2d Cause Unresponsive App - iphone

OK, so I'm trying to show Apple Game Center Leaderboards called from within my cocos2d game.
I've had some trouble doing so.
I did eventually stumble upon this and I implemented the following in one of my CCScene classes (I slightly modified the original code to prevent a compiler warning).
- (void)showLeaderboardForCategory:(NSString *)category
{
// Create leaderboard view with default Game Center style
leaderboardController = [[GKLeaderboardViewController alloc] init];
// If view controller was successfully created...
if (leaderboardController != nil)
{
// Leaderboard config
leaderboardController.leaderboardDelegate = self; // leaderboardController will send messages to this object
leaderboardController.category = category;
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
// Create an additional UIViewController to attach the GKLeaderboardViewController to
vc = [[UIViewController alloc] init];
// Add the temporary UIViewController to the main view
[[CCDirector sharedDirector].view.window addSubview:vc.view];
// Tell UIViewController to present the leaderboard
[vc presentModalViewController:leaderboardController animated:YES];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[vc dismissViewControllerAnimated:YES completion:nil];
}
And, it works! At least when I call it, it does display the Leaderboard properly.
The only problem is, when I tap "Done" on the Leaderboard and the modal view dismisses, my CCScene no longer responds to tap events.
What do I need to do to regain responsiveness?

Refer sample plain cocos2d project:
-(void)showLeaderboard
{
GKLeaderboardViewController *leaderboardViewController = [[GKLeaderboardViewController alloc] init];
leaderboardViewController.leaderboardDelegate = self;
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] presentModalViewController:leaderboardViewController animated:YES];
[leaderboardViewController release];
}
Delegate Function:
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}

Related

Adding view onto Window of Twitter

Hi i am facing a strange problem. I have a class which is NSObject type, and on same class i want to share image on Facebook and Email and also on Twitter. I successfully done FB and Email, On Email i use [[[UIApplication sharedApplication] keyWindow] addSubview:mailer.view]; to show Mail controller onto Window instread of Presenting Model view controller and same as on Dismiss i use Remove from super view. But when i do same with twitter controller then twitter controller just show me few mili seconds and then hide. I also add new View controller onto Window and then Present its controller onto that View controller but same output. Don't know what going wrong. Please help on that. Thanks in advance. This will be great for me :)
Edited
if (_engine != nil) {
_engine = nil;
[_engine release];
}
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate: self];
if (controller) {
NSLog(#"Sharing on Twitter on loading controller of twitter");
[[UIApplication sharedApplication].keyWindow addSubview:controller.view];
}
else {
NSLog(#"Sharing on Twitter in else condition");
[self sharetoTwitter:screenImg];
}
ShareToTwitterMethod
- (void) sharetoTwitter:(UIImage *)img {
NSString *response = [_engine _uploadImage:img requestType:MGTwitterPublicTimelineRequest responseType:MGTwitterStatus];
NSLog(#"twitter post notification");
}
I used Add subview to load view instead of Presenting View.
Have you tried this
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window addSubView:controller.view];
[window makeKeyAndVisible];//important line dont forget to set this line

take picture method on cameraOverlayView

My problem;
Hide the default camera controls and overlay it with my my own. This is made with the property cameraOverlayView. I also was having problem triggering the takePicture method.
(Question solved in the comments and in the edits. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
Here is what came to be the solution:
I have two UIViewController. The main ViewController and the CustomOverlay (for the camera controls).
I the ViewController I declare the source type and the overlay for may camera control like this:
- (void)viewDidLoad
{
// notification from the CustomOverlay Controller that triggers the eTakePicture method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(eTakePicture:) name:#"eTakePicture" object:nil];
daysBtn.delegate = self;
daysBtn.hidden = YES;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
picker.delegate = self;
overlay = [[CustomOverlay alloc] initWithNibName:#"CustomOverlay" bundle:nil];
// Overlay for the camera controls, note the "= overlay.view", the ".view" was important
// because the overlay is a new UIViewcontroller (with xib) so you have to call the
// view. Most tutorials that I saw were based on UIView so only "= overlay" worked.
picker.cameraOverlayView = overlay.view;
[self presentModalViewController:picker animated:NO];
[super viewDidLoad];
}
Now on the CustomOverlay, which is a UIViewController I have the take picture button and want this button to trigger a method in the main ViewController:
- (IBAction)shoot:(id)control {
[[NSNotificationCenter defaultCenter] postNotificationName:#"eTakePicture" object:self];
}
And back to the main ViewController:
-(void)eTakePicture:(NSNotification *)notification
{
[picker takePicture];
}
All the code above will change a little more once I review it, specially the first block where I have to have a condition to check if cameraSourceType is available.
Hope that helps somebody out there. Any question, just ask.

Implementing a QR reader in a cocos2d engine

So far I am using the cocos2d engine to create a game and I want to implement a QR reader.
So far I've created a CCScene, the CCScene has a menu button and it leads to a onScan Method:
-(void) onScan:(id)sender{
// ADD: present a barcode reader that scans from the camera feed
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
// present and release the controller
[self presentModalViewController: reader
animated: YES];
[reader release];
}
I understand that this works on a standard ViewController class but I am wondering how to best implement this to work with the cocos2d engine on my CCScene
Any help would be appreciated Thanks!
If you come from a typical cocos2d-iphone project which was created using the Xcode template, I think you should have a RootViewController class in your app. The only instance of the RootViewController is created in applicationDidFinishLaunching: in AppDelegate.m.
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
You can somehow save a global reference of your root UIViewController, for example, to implement a class method in RootViewController:
static RootViewController *sharedInstance_ = nil;
#implementaion RootViewController
+ (RootViewController *)sharedInstance {
if (!sharedInstance_) {
sharedInstance_ = [[RootViewController alloc] initWithNibName:nil bundle:nil];
}
return sharedInstance_;
}
and change the initialization in AppDelegate.m to:
viewController = [[RootViewController sharedInstance] retain];
then use it to present your UIViewController:
[[RootViewController sharedInstance] presentModalViewController:reader
animated:YES];
In cocos2d-iphone, the CCDirector IS the UIViewController.
So, just do this:
[[CCDirector sharedDirector] presentModalViewController:reader animated:YES]
UIView* glView = [CCDirector sharedDirector].view;
UIView* window = glView.superview;
[window addSubview:reader.view];

How to navigate the view in iphone programming?

In my appController's ViewDidLoad, I have done some thing as below
- (void)viewDidLoad
{
self.overlayViewController =
[[[OverlayViewController alloc] initWithNibName:#"OverlayViewController" bundle:nil] autorelease];
// as a delegate we will be notified when pictures are taken and when to dismiss the image picker
self.overlayViewController.delegate = self;
self.capturedImages = [NSMutableArray array];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// camera is not on this device, don't show the camera button
NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:self.myToolbar.items.count];
[toolbarItems addObjectsFromArray:self.myToolbar.items];
[toolbarItems removeObjectAtIndex:2];
[self.myToolbar setItems:toolbarItems animated:NO];
}
}
I have two methods as below,
- (IBAction)cameraAction:(id)sender
{
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if (self.imageView.isAnimating)
self.imageView.stopAnimating;
if (self.capturedImages.count > 0)
[self.capturedImages removeAllObjects];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.overlayViewController setupImagePicker:sourceType];
[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
}
}
Now as I click the button, the method will launch the class showing custom view, I want to call this without clicking the button, what should I do ?
I wrote the button coding directly in ViewDidLoad, but not working at all
This code, I took from apple's documentation as example
Help !
If I understand correctly, you are wanting to show a view?
If so you could push using:
[self.navigationcontroller pushviewcontroller:YOURVIEWCONTROLLER animated:YES];
Or you could present it using:
[self presentModalViewControllerpushviewcontroller:YOURVIEWCONTROLLER animated:YES];

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