Michael
Michael

Reputation: 6899

Cocos2d - Setting Device/Screen Orientation

I am new to the cocos2d API and have noticed that there are a few ways to set the screens orientation within the templates. I have not been able to figure out the correct way to set my orientation to LandscapeRight and keep it that way throughout the entire game. How do I change my orientation so that it maintains LandscapeRight? Any help is appreciated. Thank you!

Upvotes: 1

Views: 6841

Answers (6)

mohammad alabid
mohammad alabid

Reputation: 444

if you added shouldAutorotateToInterfaceOrientation and not solved your problem

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

THEN Try to add this line to appDelegate.m

[window_ setRootViewController:navController_];

Good Luck

Upvotes: 0

Hao Chen
Hao Chen

Reputation: 1

The correct answer - took me a while to find - is in the info.plist, change the supported interface orientations values, item 0 and item 1 have 4 possible values, Portrait (top home button) etc.

Upvotes: -2

WiseOldDuck
WiseOldDuck

Reputation: 3226

The answer here has changed with cocos2d 2.0, as CCDirector is now a ViewController on iOS:

CCDirector no longer supports device orientation. All autorotation and device orientation is handled by RootViewController now. Fortunately, [[UIDevice currentDevice] orientation] can be used in place of [[CCDirector sharedDirector] deviceOrientation]. The enums are the same, except that they begin with UI instead of CC.

Forcing a specific orientation is a simple matter of returning YES only to the desired orientation in the RootViewController method shouldAutorotateToInterfaceOrientation.

Choosing between Cocos2D v1.x & 2.x and Tips for updating to Cocos2D 2.0 at learn-cocos2d.com

Upvotes: 8

Anish
Anish

Reputation: 2917

In the RootViewController.m,search for the line

return ( UIInterfaceOrientationIsPortrait(interfaceOrientation ));

change it to

return ( UIInterfaceOrientationIsLandscape(interfaceOrientation ));

Upvotes: 1

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 13999

Modify GameConfig.h from the cocos2d template.

#define GAME_AUTOROTATION kGameAutorotationNone
/* original code is kGameAutorotationUIViewController. */

And modify AppDelegate.m as well.

#if GAME_AUTOROTATION == kGameAutorotationUIViewController
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeRight];
    /* original code is "Left". */
#endif

Upvotes: 5

xuanweng
xuanweng

Reputation: 1939

Use this line:

[[CCDirector sharedDirector] setDeviceOrientation:kkCCDeviceOrientationLandscapeRight];

Upvotes: 3

Related Questions