Omer Abbas
Omer Abbas

Reputation: 205

Changing Cocos 2d orientation landscape to portrait

I have two views one of them is in landscape and other in portrait. The default orientation is landscape. When i change the view from landscape to portrait using

[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeRight];

While graphics are rotating perfectly, I'm having issue with the touch screen. Can anyone please help me with this issue?

Upvotes: 1

Views: 9150

Answers (4)

iftekher2283
iftekher2283

Reputation: 41

[startUpOptions setObject:CCScreenOrientationPortrait forKey:CCSetupScreenOrientation];

I just added the above code in appdelegate.m file within the function (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions and it worked

Upvotes: 0

Torsten Barthel
Torsten Barthel

Reputation: 3458

It's straightforward and very simple to achieve correct orientations. You just have to edit your RootViewController implementation: Just modify it's -willChangeToInterfaceOrientation... method to return 'YES' for orientations that you need in your application. For example change your return value for all posibilities to

return ( (interfaceOrientation == UIInterfaceOrientationPortrait) ||    
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) );

That done your app will only rotate if it's in landscape orientation to portrait orientation. And your app (in this case) only supports portrait orientation. To autorotate your app correctly in response to changing from portrait to portraitUpsideDown (and vice versa) just edit the following lines to configure your CCDirector instance with appropiate values:

if( interfaceOrientation == UIInterfaceOrientationPortrait ) {
    [[CCDirector sharedDirector] setDeviceOrientation:     
kCCDeviceOrientationPortraitUpsideDown];
} else if( interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    [[CCDirector sharedDirector] setDeviceOrientation: 
kCCDeviceOrientationPortrait];
}

All that is mentioned in comments that come with your default method implementation from your template project!

And don't forget to edit your app's info.plist to contain appropiate values for the keys -InitialInterfaceOriatation -SupportedInterfaceOrientations

You normally don't need to change anything in your apps delegate-class nor GameConfig.h.

And...there you go!

Upvotes: 1

Omer Abbas
Omer Abbas

Reputation: 205

That was so easy, Solved my problem by just converting the touch points to GL

CGPoint convertedLocation = [[CCDirector sharedDirector]convertToGL:point];

Upvotes: 0

X Slash
X Slash

Reputation: 4131

Are you using the latest cocos2d template? The reason why [[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeRight]; wouldn't work is because by default, it lets UIKit (RootViewController) to handle the rotation, read the comments from line 77 - 84. What your code does is changing CCDirector orientation, not the RootViewController hence your touch location isn't translated properly.

Do you mix Apple standard UIKit with cocos2d? If you don't one very easy solution is just go to GameConfig.h

change these lines

#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR
#define GAME_AUTOROTATION kGameAutorotationUIViewController

to

#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR
#define GAME_AUTOROTATION kGameAutorotationCCDirector

P.S Changing the auto-rotation handler might / might not work depending on how you set up your project.

Upvotes: 3

Related Questions