Well this has been… difficult to find.

In Cocos2D-iPhone to enable the horizontal orientation for your game you need to do the following in the AppDelegate of your project:

[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];

The process is rather similar in Cocos2D-x, but we’ll get to that in a moment.

The recommended way to set the orientation in an Android game is to use theĀ android:screenOrientation="landscape" property in AndroidManifest.xml.

If you open the AppDelegate.cpp file you will find there two methods that are called when the game is launched:

  1. AppDelegate::initInstance() – this method was supposed to be called by all the platforms
  2. AppDelegate::applicationDidFinishLaunching() – this method was supposed to be called by iPhone only

Now, you see that I said “was supposed”. For some reason the applicationDidFinishLaunching() method is called by the other platforms as well.

The initInstance() method is supposed to, of course, initialize the instance for each of the platforms based on preprocessor directives. When you get to the iOS section you get:

        // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here.
#endif  // CC_PLATFORM_IOS

And the Android section:

	// OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp
	// the default setting is to create a fullscreen view
	// if you want to use auto-scale, please enable view->create(320,480) in main.cpp

The rest of the platforms usually initialize and set their preferred orientation and window size here. iOS on the other hand does that in the applicationDidFinishLaunching() method:


Now, the problem with this is that this code will get executed by Android as well, and if you set the orientation here to landscape, that means that it will conflict with the already set orientation from the Android manifest file.

To get around this, you need to make sure this code only gets executed when building for iOS. We will of course use the handy preprocessor directives to isolate this piece of code:


And that’s it! The game should now run in landscape in both Android and iPhone. You may want to follow the instructions for the Android build related to the main.cpp file – your game will look funky otherwise.

Share this article

Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *