trying to check device orientation...

function deviceOrientation() &nbsp;&nbsp; &nbsp;if then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; --Check to see the device is in LANDSCAPE position \<---- &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else --pause game and tell user to turn their device sideways/landscape \<---- end --create runtime listener Runtime:addEventListener ( "orientation", deviceOrientation ) &nbsp;

The above is my code. Im trying to create a function that checks to make sure the device is Landscape. If it the device is turned to the Portrait position, I want my game to pause (stop) and issue a popup window that tells the user to turn the device “sideways” or whatever…

Anyway, that’s what i am trying to do, but haven’t been able to get it to work. By chance, would anyone have a solution for this?  :huh: 

Hi there,

Out of curiosity, have you considered just making your app run as landscape-only?  I think your users will prefer that versus a popup.

You can make your app landscape-only via your build.settings file.  Check out the info here: http://docs.coronalabs.com/guide/distribution/buildSettings/index.html#orientation.

  • Andrew

Yes it is landscape in config.lua file. That’s already been taken care of. The problem is when a user turns their device to portrait mode for whatever reason…A user may tip their device portrait just to see what happens, people are weird like that…Anyway, the point is my game can NOT be played in portrait so I’m trying to safe guard against any problems a user may encounter by making sure the device checks to make sure it is ALWAYS in landscape position.

If anyone has any good solutions to this, please post. Thank you! :slight_smile:

It would be in the build.settings file, not the config.lua file, that you set your app’s orientation.  Have you set it correctly?

When you say your game “can NOT be played in portrait”, what do you mean exactly?  Do you mean that if the user rotates to portrait, the game literally won’t function properly (i.e., the display will look weird to the user, or it’ll crash)?  Or do you mean that playing in portrait would give the player some sort of unfair advantage or different gameplay experience that you want to avoid?

You said you want to “safe guard against any problems a user may encounter” by rotating to portrait.  Why would they encounter any problems at all?  If your app is set to be landscape-only, then if the user rotates to portrait, nothing will happen.  Think of Angry Birds or any other landscape-only game.

Of course, maybe your game is very unusual in how it uses, say, the gyroscope or accelerometer, in which case maybe I could understand that, even if the app is set to landscape-only, rotating to portrait would somehow be a bad thing.  Is that the issue?

  • Andrew

I meant the game wont function properly. There are some visual issues I wish to avoid. I would rather not go into specifics because I dont want to deviate from my question’s focus. Basically I just want my game to suspend until the device senses it is back to landscape position, and while in portrait mode to show a msg (“TAP SCREEN TO CONTINUE” or something like that) if the user puts their device in portrait mode. Sounds simple, but if it was I would be posting here lol…Thanks to anyone who can post a full or even somewhat solution of any kind.

i always get config and buildsettings mixed up when i think about them…Sorry for any confusion.

No worries.

The reason for all my questions is because it sounds to me like you’re trying to address a surface problem (your game doesn’t look right if user rotates the device) with a misguided approach (a popup) that’s probably going to annoy your users.  If your game actually has “visual issues”, as you called them, when the user rotates the device, then I think you have a much deeper problem in terms of how your game is set up and how you’re handling orientation changes, and I think you should focus on fixing that problem.  I realize you mentioned that you didn’t want to go into specifics, but it might be worth at least describing in a bit more detail what you’re facing.  Maybe the visual issues you’re having actually have an easy root cause fix, in which case you can avoid the ugly popup altogether.

  • Andrew

Yeah I can’t quite understand the issue here - if it’s set to Landscape only (properly) you don’t need to worry about what happens when the user rotates the device. Think Angry Birds - does that do anything if you rotate the device?

I dont know I have never played angry birds game. I thought you needed to specify in code your orientation? I know it is in the buildsettings but I thought that it was in there for simulating/development purposes. For the user to benefit from a device making orientation checks, I thought you had to specify that in code?

I don’t have a way to test this

function deviceOrientation ( event ) -- LINE BELOW CHECKS IF DEVICE IS ROTATED AWAY FROM LANDSCAPE POSITION &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.type ==&nbsp; "portrait" or "portraitUpsideDown" or "faceDown" then "applicationSuspend" else end end Runtime:addEventListener ( "orientation", deviceOrientation ) --LISTENER &nbsp;

But, think this will work? :ph34r:

No, the orientation you specify in build.settings isn’t just for simulating/developing purposes, it actually determines what orientations are shown on the user’s device.  For most applications, you can just set these settings in build.settings and then never have to do anything in your code to get your orientation working correctly.

Can you copy/paste your build.settings file into this thread?  We can take a look and make sure you’ve set it up correctly to handle landscape-only orientation.

  • Andrew

Hmm…okay well here is my buildsettings file. What’dya think?

-- MY BUILD SETTINGS FOR "NAME OF GAME HERE" settings = { &nbsp;&nbsp; &nbsp;orientation = &nbsp;&nbsp; &nbsp;{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;default = "landscapeRight", &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;supported = &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;"landscapeRight" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}, &nbsp;&nbsp; &nbsp;}, &nbsp;&nbsp; &nbsp;android = &nbsp;&nbsp; &nbsp;{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;versionCode = "1" &nbsp;&nbsp; &nbsp;}, &nbsp;&nbsp; &nbsp;androidPermissions = &nbsp;&nbsp; &nbsp;{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;"android.permission.WRITE\_EXTERNAL\_STORAGE" &nbsp;&nbsp; &nbsp;}, &nbsp;&nbsp; &nbsp;iphone = &nbsp;&nbsp; &nbsp;{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;plist = &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;UIPrerenderedIcon = true, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;UIApplicationExitsOnSuspend = false, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;UIStatusBarHidden = true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CFBundleIconFile = "images/iconMM.png", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CFBundleIconFiles = { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "iconMM.png" &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}, &nbsp;&nbsp; &nbsp;}, } &nbsp;

I also thought the above was for starting the game and after the game starts if a user tips their device a certain way the screen will automatically shift in the direction the user tipped/turned their device…I am wrong for thinking that?, or?..  :o

Hi there,

Your build.settings looks OK to me except for one thing: you’re using an old way of including the androidPermissions.  The new way is to have a table called usesPermissions within the android table.  Check out the sample here: http://docs.coronalabs.com/guide/distribution/buildSettings/index.html#androidsettings.

As for orientation, in build.settings, “default” controls what orientation the app will start in (like you said), and “supported” will ensure that the app only stays in one of the supported orientations.  You’ve done the right thing by setting both the default and supported orientations as landscapeRight,  This means that, no matter how the user orients the device physically, the display won’t change – the display will still be oriented landscapeRight.

You can actually see what this is like in the Corona Simulator.  While your app is running in the Simulator, click Hardware on the menu, then click Rotate Right or Rotate Left.  This simulates a user rotating their device to a different orientation, and you can see that it should be fine / no visual issues like you were worried about.

  • Andrew

You know…I never thought of turning the simulator itself… :blink:

That just cleared up everything lol…I tried running the simulator and when i use ROTATE LEFT or ROTATE RIGHT, the device rotates and everything rotates with it. I thought I had to code all that in…hmm, okay well thanks aukStudios. :slight_smile:

No problem, happy to have helped.  :-)

  • Andrew

Hi there,

Out of curiosity, have you considered just making your app run as landscape-only?  I think your users will prefer that versus a popup.

You can make your app landscape-only via your build.settings file.  Check out the info here: http://docs.coronalabs.com/guide/distribution/buildSettings/index.html#orientation.

  • Andrew

Yes it is landscape in config.lua file. That’s already been taken care of. The problem is when a user turns their device to portrait mode for whatever reason…A user may tip their device portrait just to see what happens, people are weird like that…Anyway, the point is my game can NOT be played in portrait so I’m trying to safe guard against any problems a user may encounter by making sure the device checks to make sure it is ALWAYS in landscape position.

If anyone has any good solutions to this, please post. Thank you! :slight_smile:

It would be in the build.settings file, not the config.lua file, that you set your app’s orientation.  Have you set it correctly?

When you say your game “can NOT be played in portrait”, what do you mean exactly?  Do you mean that if the user rotates to portrait, the game literally won’t function properly (i.e., the display will look weird to the user, or it’ll crash)?  Or do you mean that playing in portrait would give the player some sort of unfair advantage or different gameplay experience that you want to avoid?

You said you want to “safe guard against any problems a user may encounter” by rotating to portrait.  Why would they encounter any problems at all?  If your app is set to be landscape-only, then if the user rotates to portrait, nothing will happen.  Think of Angry Birds or any other landscape-only game.

Of course, maybe your game is very unusual in how it uses, say, the gyroscope or accelerometer, in which case maybe I could understand that, even if the app is set to landscape-only, rotating to portrait would somehow be a bad thing.  Is that the issue?

  • Andrew

I meant the game wont function properly. There are some visual issues I wish to avoid. I would rather not go into specifics because I dont want to deviate from my question’s focus. Basically I just want my game to suspend until the device senses it is back to landscape position, and while in portrait mode to show a msg (“TAP SCREEN TO CONTINUE” or something like that) if the user puts their device in portrait mode. Sounds simple, but if it was I would be posting here lol…Thanks to anyone who can post a full or even somewhat solution of any kind.

i always get config and buildsettings mixed up when i think about them…Sorry for any confusion.