Android Orientation

Android Orientation


This detects rotation on the web, but it doesn’t seem to work on an android device. Is there anyway to do this in Cocos Creator? I need to know when the device rotates in a particular scene.

cc.director.on(cc.Director.EVENT_PROJECTION_CHANGED, function (event) 
}, this);

hi, this event isn’t for orientation changing

If you are in web, you can listen to the orientationchange event. or you can setup a callback with cc.view.setResizeCallback(...), and detect orientation in it.

If you are in native, I’m afraid there is no such callback yet, but you can freeze in one particular orientation

Thanks for the response.

The event may not be for orientation change, but it works great as a substitute for web development.

Unfortunately, I am developing for native and I figured there wasn’t support for orientation change yet.

I’ll have to try and figure something out because my game works great in all orientations, à la Candy Crush.

I think you can register orientation change event in native (the method diverse for iOS and Android), then dispatch the event to JS scope using the EventDispatcher (cc.eventManager in JS) or use ScriptingCore::evalString

I couldn’t seem to get cc.eventManager to do what I needed it to do, but evalString did the job for me, thanks!

One last question, after the orientation changes, my scene’s visible size seems to stay the same, so half of my scene is cut off (scene won’t show fullscreen), and I can’t manage to figure it out. I thought cc.view would solve my problems, but everything I try keeps the visible size the same. Any ideas?

Seems it’s more tricker than we thought, you may try to do cc.view.setFrameSize then cc.view.setDesignResolutionSize

It comes close to working, but it won’t fill the whole screen. (see picture below)

I have tried each resolution policy, and none of them will fill the screen entirely. Am I missing something? Or do you have anymore ideas?

So what’s your current code ?

Here is my code, where windowSize = cc.director.getVisibleSize()

cc.view.setFrameSize(this.windowSize.width, this.windowSize.height);
cc.view.setDesignResolutionSize(this.windowSize.width, this.windowSize.height, cc.ResolutionPolicy.SHOW_ALL);

The problem could be the incorrect windowSize, I’m not sure how to retrieve the correct frame size, @owen please take a look

I figured it out!!! All of your help was great! It turns out if you call getVisibleSize() on rotation or any other incorrect time, then it can lose the correct visible size of the android device. Thanks again!

Glad it works

1 Like

I just wanted to update the post because I forgot to provide one of my changes for the correct answer. Use this method to get the correct size


instead of this


Bump a 2 years old thread…
Does creator have orientation callback for native now?

Does creator have orientation callback for native now?

Does creator have orientation callback for native now?