Landscape view issues with cocos2d v1.1 and iOS 6

In this post I want to discuss a workaround we have implemented to address display issues we were seeing on iOS 6.

Our app is setup to operate in landscape view.  When iOS 6 came out we found that under the new OS, in both the simulator and on the device, the app would only show in portrait. Our app is still under development so I did not worry about addressing this issue until recently.

The first thing I did was to change how the view controller is setup in AppDelegate.mm.  When you create a new project using the cocos2d v1.1 template, the viewController.view is added as subview of your window.

// make the View Controller a child of the main window
[self.window addSubview: self.viewController.view];

Rather then adding this as a subview you should instead set your windows rootViewController to be your viewController.

// make the View Controller a child of the main window
self.window.rootViewController = self.viewController;

This will fix the orientation issue. But this presents another problem.  Now when the first scene is loaded the background is not positioned correctly in the window.  Something about the coordinate system is wrong.  If you transitioned to a new scene everything will be fine.  You can even return back to the original scene and it will display correctly.  So the workaround I am using, until I find a better solution, is to briefly transition to a blank scene when starting the game. The blank scene simply loads and then immediately transitions to your normal first scene. In my case this is our home menu.  Below is the simple blank scene I set up for this.

#import "BlankLayer.h"
#import "MenuLayer.h"

@implementation BlankLayer

+(CCScene *) scene {
	CCScene *scene = [CCScene node]; 
	BlankLayer  *layer = [BlankLayer node];	
	[scene addChild: layer];  
	return scene;  
}

-(id)init {
    self = [super init];
    if (self) {
        [self performSelector:@selector(showMenu) withObject:self afterDelay:0.1f];
    }
    return self;
}

-(void)showMenu {
    [[CCDirector sharedDirector] replaceScene:[MenuLayer scene]];

}

@end

Then in AppDelegate simply have the director run with the blank scene rather then your normal first scene.

[[CCDirector sharedDirector] runWithScene: [BlankLayer scene]];
 I would like to have a better solution that avoids the need to transition to a blank scene. But, for now at least, this is an acceptable workaround that will allow us to continue with development.

 

Posted in Coding and tagged , , , .

One Comment

Comments are closed.