Running game in both Landscape AND Portrait mode

It is fine to run the game in EITHER Landscape OR Portrait mode but if you want to support BOTH modes then you endup in trouble.

The system auto-rotates the screen properly, even the nodes are in expected positions.
The problem is with the scene size after the changed orientation. It still maintains the size of the original orientation.
Changing the setContentSize() has effect on placing the node to correct positions but that could be clipped away if out of new view bounderies.
Futhermore, the buttons are positioned as expected but they react on touches in different locations!

It is better to see it in pictures.

Default/start orientation: correct behavior:
All nodes are properly placed and touches work as expected.

Changed orientation: BAD behavior:

  1. buttons are in incorect positions (should be in top/left and bottom/right corners)
  2. scene contentSize is incorect
  3. Button touches react in incorrect positions (other then where the button node is placed)

Again, changing this->setContentSize() does not solve the problem.
Yet, console shows proper surface size changes. This is not however reflected in scene corrent values.

2015-07-20 15:51:21.788 MGWUNB iOS[96680:762126] cocos2d: surface size: 640x960
2015-07-20 15:51:22.795 MGWUNB iOS[96680:762126] cocos2d: surface size: 960x640

This is what is shown on the screen:

Size size = this->getContentSize();
Size winSize = Director::getInstance()->getWinSize();
Size visibleSize = Director::getInstance()->getVisibleSize();

It seems that the native UIVIew is not resized properly. Same behavior is on iOS and Android.
Please help!
–Josef

That looks pretty normal to me. I don’t see any other way than to move the nodes in the overridden setContentSize().

Yes, this is what I’m doing. But they are not visible in the black area.
They are clipped away.
The native UIVIew which actually shows the cocos2d content has wrong size…

This is what the scene looks like if I set the ContentSize manualy.

  1. The nodes in yellow area are placed as expected.
  2. The nodes in black area are not visible (clipped away)
  3. Button touches react in incorrect positions, see the notes in the picture.

I think the size-related function won’t be effective after the scene is activated.

One more hint, this behaves the same under both iOS and Android.

Any help on how to set the native view size properly?

I’ve tried it with cocos2d-x v.3.7 with the same result…

Hello, I have the same problem. Is there some solution fot that issue?

Try this, I use it on Android and it somehow works:

auto director = cocos2d::Director::getInstance();
auto glview = director->getOpenGLView();

cocos2d::GL::invalidateStateCache();
cocos2d::GLProgramCache::getInstance()->reloadDefaultGLPrograms();
cocos2d::DrawPrimitives::init();

cocos2d::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
director->setGLDefaultValues();

glview->setFrameSize(this->width, this->height);
glview->setDesignResolutionSize(this->width, this->height, ResolutionPolicy::SHOW_ALL);
director->drawScene();

Or maybe try just this:

auto director = cocos2d::Director::getInstance();
auto glview = director->getOpenGLView();

glview->setFrameSize(this->width, this->height);
glview->setDesignResolutionSize(this->width, this->height, ResolutionPolicy::SHOW_ALL);
director->drawScene();

… and remember reset frame for EAGLView

CCEAGLView eaglview = (CCEAGLView) glview->getEAGLView();

    if (eaglview)
    {
        UIInterfaceOrientation uio = [UIApplication sharedApplication].statusBarOrientation;
        bool islandscape = UIInterfaceOrientationIsLandscape(uio);
        
        CGRect rect = [[UIScreen mainScreen] bounds];
        if (islandscape && (rect.size.width < rect.size.height))
        {
            float temp = rect.size.width;
            rect.size.width = rect.size.height;
            rect.size.height = temp;
        }
        [eaglview setFrame:rect];

Hello,

Is it working for you guys?
Are you able to Rotate device Landscape to Portrait?

I tried. Am getting as per the above screenshot.

Plz help me with the problem.

Thanks in advance
Gurudath

Hello, I have the same problem. Is there some solution ? cocos2d-x team ? PLZ!

Look at this post and my answer:

https://discuss.cocos2d-x.org/t/how-to-update-winsize-visiblesize-visibleorigin-of-director/42646/2?u=dimon4eg