Hello.
I am working on iPad application. I have 2 versions of my UI - one for portrait and another for landscape device orientation.
I know how to use StageOrientationEvent in order to switch my display object from portrait to landscape version. This works fine when changing the orientation of my iPad.
The problem is that I don't know how to detect the device orientation during the startup.
Unfortunately Srage.deviceOrientation returns 'unknown".
I added if statetement in my Event.ADDED_TO_STAGE event handler. My scrip looks like this:
public function MyPadApplication():void {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
if(stage.deviceOrientation == StageOrientation.ROTATED_LEFT || stage.deviceOrientation == StageOrientation.ROTATED_RIGHT){
//Show the landscape version of my UI
} else if(stage.deviceOrientation == StageOrientation.DEFAULT || stage.deviceOrientation == StageOrientation.UPSIDE_DOWN){
//Show the portrait version of my UI
} else {
//In case of unknown device orientation
}
}
MyPadApplication is the name of my document class. In init function stage.deviceOrientation is always "unknown". So I always go in the last else.
Later, after the first orientation change stage.deviceOrientation starts to get the device orientation properly.
Do you have any suggestions how to solve this problem?
Thank you in advance.
p.s. In case you need this information - I set autoOrients to true and Aspect ratio to portrait. I also set the stage to TOP_LEFT and NO_SCALE.
Detecting stage.orientation on the first frame (via enter frame event) does nothing for me. It always returns "default" no matter what.
If I launch the test app on my iPad holding it in portrait mode (I have the default orientation on portrait) and it launches fine; the Default-Portrait.png opens, followed by the staged at the correct orientation, stage.orientation returns "default", and any rotations to the device work properly thereafter.
However, if I launch the app while holding the iPad in landscape mode, Default-Portrait.png still opens (I have all of the correct Default.pngs included), stage.orientation still returns "default", and my stage shows up in portrait mode while the device is in landscape. After about a second, my ORIENTATION_CHANGE event listener fires and detects that it should switch the stage to landscape mode.
I can't help wondering if this problem's solution lies in showing the correct Default.png. I've seen some threads on here asking about it, but the only "solution" I've seen involves making sure you include all of the Default.pngs.
I also updated to AIR 2.5 beta extension (http://labs.adobe.com/technologies/flashpro_extensionforair/). No change.
My code, on the first and only frame:
import flash.events.StageOrientationEvent;
import flash.events.Event;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
// Detect orientation on startup
stage.addEventListener(Event.EXIT_FRAME, startupOrient, false, 0, true);
function startupOrient(event:Event):void{
var startOrient:String = stage.orientation;
trace("Startup Orientation:",startOrient);
changeOrientation(startOrient);
stage.removeEventListener(Event.EXIT_FRAME, startupOrient);
// Start listener for orientation change events
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, detectOrientation, false, 0, true);
}
// Detect orientation on orientation change
function detectOrientation(event:StageOrientationEvent):void{
trace("Orientation was:",event.beforeOrientation);
changeOrientation(event.afterOrientation);
}
// My 2 UI MovieClips, one for portrait and one for landscape
viewLandscape.visible = false;
viewPortrait.visible = false;
// With Auto orientation set to true and default aspect as portrait (iPhone OS Settings)
// show different display on orientation change
function changeOrientation(ORIENTATION:String):void{
if(ORIENTATION == StageOrientation.ROTATED_RIGHT || ORIENTATION == StageOrientation.ROTATED_LEFT){
viewLandscape.visible = true;
viewPortrait.visible = false;
} else {
viewLandscape.visible = false;
viewPortrait.visible = true;
}
trace("Orientation is now:",ORIENTATION);
}
iPhone OS Settings:
Aspect ratio: Portrait
Full screen: ON
Auto orientation: ON
Rendering: Auto
Device: iPad
Included files:
HelloWorld.swf
HelloWorld-app.xml
Default-Landscape.png
Default-Portrait.png
Default.png
Stage size is 768 x 1024 px.
What am I doing wrong? Or is this an issue that never gets solved, and everyone's given up on?
hi, perhaps its not this easy on the iphone, but on android i
stage.addEventListener(Event.RESIZE,doStageLayout);
In my case I have only landscape set. The event fires once at startup , i layout my items and remove the listener, as it will never be needed again.
but if you do expect oriantation changes,
if (width>height) , your in landscape
i have seen this issue addressed, at least in what i was seeing on my ipad. in the last packager, i had this code:
private function stageOrientationChangingHandler(event:StageOrientationEvent):void
{
if (event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN) {
event.preventDefault();
}
}
and it would still not work - it woudl start up upsdie down, or re-orient regardless of of it was in the right otientation or not.
in the new AIR SDK, this work as expected now. very nice.
North America
Europe, Middle East and Africa
Asia Pacific