Maciej Swic
Maciej Swic

Reputation: 11359

cocos2d screen orientation problems

I am using the standard rotation code present in cocos2d 0.99-rc0 to support portrait + two landscape modes. I am showing the menu in portrait mode, and then the screen rotates to landscape for the actual game. Problem is that when i go back to portrait, the whole mainmenu scene is off by half the screen, like someone had moved the anchor point or something.

Any ideas please?

Upvotes: 1

Views: 3492

Answers (1)

Josh Kahane
Josh Kahane

Reputation: 17160

A possible simple solution would be to apply the orientation at the start of the scene then after wards re-apply the positions of your menu items so that its all aligned.

I do the following to change the screen orientation:

Firstly, the first line goes inside the init method I set a timer to start after a quick 0.5 seconds. Putting it in a timer means in my game the scene transition (fade) works smoothly, the screen doesn't rotate/snap round then, but you probably won't need to use this.

[self schedule:@selector(rotate:) interval:0.5];

-(void)rotate:(ccTime) dt{
    [[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
    [self unschedule:@selector(rotate:)];
}

The key line is below, you don't necessarily need the timer:

[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];

Of course you can alter this bit for different orientations:

CCDeviceOrientationLandscapeLeft
CCDeviceOrientationLandscapeRight
CCDeviceOrientationPortrait
CCDeviceOrientationPortraitUpsideDown

Good luck.

Upvotes: 1

Related Questions