Search code examples
ioscocos2d-iphoneorientation

cocos2d v3 re-orient screen during App use


So in cocos2d (I believe I was on v2.1) I did this to lock and set the orientations:

        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    const UIInterfaceOrientation ORIENTATION = delegate.navController.interfaceOrientation;
    delegate.navController.
    delegate.navController.screenOrientation = ORIENTATION == UIInterfaceOrientationMaskPortrait;
    UIViewController *mVC = [[UIViewController alloc] init];
    [delegate.navController presentModalViewController:mVC animated:NO];
    [delegate.navController dismissModalViewControllerAnimated:NO];

With some functions added to the AppDelegate. I cannot seem to get the same results in iOS7 and cocos2d v3.

I have dug through a bit and the proper functions seem to be in place but cannot seem to set up a global variable to set the orientation and return only the one I want at the certain time. Can someone point me down the proper path. I think I'm missing something really small cause the proper code seems to be there already.

Here's the code for my AppDelegate

@implementation AppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setupCocos2dWithOptions:@{
        CCSetupShowDebugStats: @(NO),
    }];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

return YES;

}

-(CCScene *)startScene
{
    return [HomeScreen scene];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

My code never hits the interfaceOrientation functions.

Thoughts??!?


Solution

  • After a couple days fooling around I figured out a solution:

    in AppDelegate I needed this function:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (!self.lockedToOrientation) {
            if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ){
                return UIInterfaceOrientationMaskPortrait;
            }
    
            return UIInterfaceOrientationMaskPortrait;
        }
        else {
            return self.lockedToOrientation;
        }
    }
    

    Where

    @property UIInterfaceOrientationMask lockedToOrientation;
    

    Hope this helps someone!

    Cheers.