Orientation change with MXML

I have been banging my head on the wall trying to figure out how to detect the device orientation on the BlackBerry PlayBook.  I initially tried to use the accelerometer function.  Unfortunately, the listener would continue to activate and determining the exact orientation was a little iffy.  So, a slight move of the device would set off the listener and the redrawing of the screen would keep going until the accelerometer stopped.  This was not a good way to test for orientation, it will be a great way to make a game that required moving the PlayBook around a lot.  After days of searching the internet and the forums I finally stumbled upon the answer.

The way to check the orientation is to determine it through the stage.  However using the Sparks/mxml scripting and not the full as3 coding to make the application, getting to the stage information isn’t very straight forward.  If you trying to add a event listener to the stage as you would in as3, the you get a “null” object error, because the stage hasn’t been instantiated or something.  With the actual solution, you have to go a level above the stage to the systemManager.


systemManager.stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, onOrientationChanging );

From there, it is just a matter of determining the correct orientation and then updating the display.


private function onOrientationChanging( event:StageOrientationEvent ):void {
 if(event.afterOrientation == StageOrientation.UPSIDE_DOWN || event.afterOrientation == StageOrientation.DEFAULT ) {
stageSize(false);
 } else {
stageSize(true);
 }
 }

About DeanLogic
Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.

About DeanLogic

Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.