Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Contributor
Posts: 30
Registered: ‎04-04-2012
My Device: BlackBerry Playbook
My Carrier: Telus

How to obtain stage orientation change event, but not affect the display of the app?

My title pretty much says it all Smiley Happy

 

I work with FDT 5.5, and if I set the orientation to auto in Project Properties > FDT AIR Properties > BlackBerry > Building, I will obtain the event, but also my app display will change/rotate.

 

What if I just want to know when the orientation changed, but not affect the app? If I uncheck the previous option, I get no event anymore.

 

Thanks for any help!

--
mat janson blanchet
twitter: @jansensan, portfolio: http://jansensan.net/portfolio, tech blog: http://jansensan.net
New Contributor
Posts: 4
Registered: ‎04-18-2011
My Device: Dont hava a device yet
My Carrier: none

Re: How to obtain stage orientation change event, but not affect the display of the app?

[ Edited ]

How about this?

 

this.stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, onDeviceRotate);

 

private function onDeviceRotate(pEvent: StageOrientationEvent):void{

pEvent.preventDefault();

pEvent.stopInmediatPropogation();

}

 

Contributor
Posts: 30
Registered: ‎04-04-2012
My Device: BlackBerry Playbook
My Carrier: Telus

Re: How to obtain stage orientation change event, but not affect the display of the app?

Yea, this works, I can get the orientation change, but I can never get an event whenever I come back to the initial orientation. I guess that's what preventDefault() does, but I wish I could still get an event, but control what happens.

See my code below and let me know if you have any idea on how to fine tune this.

Thanks!

package net.jansensan.test
{
import flash.display.StageOrientation;
import flash.display.Bitmap;
import flash.events.StageOrientationEvent;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;


public class OrientationChange extends Sprite
{
privateconstWIDTH:uint = 1024;
privateconstHEIGHT:uint = 600;


[Embed(source="assets/images/arrow.png")]
privateconstArrowImageClass:Class;

privatevar_arrowSmiley Frustratedprite;


public function OrientationChange()
{
initStage();
init();
}


privatefunction initStage():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
}


privatefunction init():void
{
var image:Bitmap = new ArrowImageClass();
image.x = -int(image.width * 0.5);
image.y = -int(image.height * 0.5);

_arrow = new Sprite();
_arrow.addChild(image);
_arrow.x = WIDTH * 0.5;
_arrow.y = HEIGHT * 0.5;

addChild(_arrow);

stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, onOrientationChanging);
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, onOrientationChanged);
}


private function onOrientationChanging(_Smiley FrustratedtageOrientationEvent):void
{
trace("\n", this, "--- onOrientationChanging ---");

_.preventDefault();
_.stopImmediatePropagation();
_.stopPropagation();

trace("\t", "_.beforeOrientation: " + (_.beforeOrientation));
trace("\t", "_.afterOrientation: " + (_.afterOrientation));

switch(_.afterOrientation)
{
caseStageOrientation.ROTATED_LEFT:
_arrow.rotation = 90;    //arrow image is rotated, points up
break;

caseStageOrientation.ROTATED_RIGHT:
_arrow.rotation = -90;    //arrow image is rotated, points up
break;

caseStageOrientation.UPSIDE_DOWN:
_arrow.rotation = 180;    //arrow image is rotated, points up
break;

caseStageOrientation.DEFAULT:
_arrow.rotation = 0;    // arrow image is unaffected
break;
}
}


private function onOrientationChanged(_Smiley FrustratedtageOrientationEvent):void
{
trace("\n", this, "--- onOrientationChanged ---");

trace("\t", "_.beforeOrientation: " + (_.beforeOrientation));
trace("\t", "_.afterOrientation: " + (_.afterOrientation));
}
}
}

--
mat janson blanchet
twitter: @jansensan, portfolio: http://jansensan.net/portfolio, tech blog: http://jansensan.net
New Contributor
Posts: 4
Registered: ‎04-18-2011
My Device: Dont hava a device yet
My Carrier: none

Re: How to obtain stage orientation change event, but not affect the display of the app?

[ Edited ]

Well, your code looks pretty ok. So there's no problemSmiley Happy.

 

But yes, preventdefault makes sure the event doesn't dispatch higher up the tree. And so it will also not reach those other places you want to capture it. But that is something nice because events are slow is AS3. So you should use functions/callbacks when ever possible and preventDefault events when ever possible. Else those events will fly around your whole application eating up resources when you don't use it.

 

So the solution would be to make a custom "RotatingEvent" and dispatch that on the stage after you preventDefault the original event.

 

Than you can listen from pretty mutch everywhere on the stage to the "RotatingEvent" which is you custom event and will not be handled by air to rotate your stuff.

 

An other solution (which is faster and there for prefered) is to just call a function that goes down in your architecture. Something like

 

rotationChanged(pNewRotationSmiley Frustratedtring):void  

 

The down side of this is that you need to make a lot of functions when you have a deep structure.

 

Hope that helpsSmiley Happy

Contributor
Posts: 30
Registered: ‎04-04-2012
My Device: BlackBerry Playbook
My Carrier: Telus

Re: How to obtain stage orientation change event, but not affect the display of the app?

Actually, the part of handling manually the rotation is pretty much what I want to do, and I know how to handle that part.

The issue that I am facing is that I never get the StageOrientation.DEFAULT value:

switch(_.afterOrientation)
{
case StageOrientation.ROTATED_LEFT:
_arrow.rotation = 90; // arrow image is rotated, points up
break;

caseStageOrientation.ROTATED_RIGHT:
_arrow.rotation = -90; // arrow image is rotated, points up
break;

caseStageOrientation.UPSIDE_DOWN:
_arrow.rotation = 180; // arrow image is rotated, points up
break;

caseStageOrientation.DEFAULT:
_arrow.rotation = 0; // arrow image is unaffected
break;
}

Why would that be?

--
mat janson blanchet
twitter: @jansensan, portfolio: http://jansensan.net/portfolio, tech blog: http://jansensan.net
New Contributor
Posts: 4
Registered: ‎04-18-2011
My Device: Dont hava a device yet
My Carrier: none

Re: How to obtain stage orientation change event, but not affect the display of the app?

O that is a common issue. I don't know why that happens, but it does. The solution I always use it to listen to the Stage Resize event. That event is always triggered no matter the orientation.

Contributor
Posts: 30
Registered: ‎04-04-2012
My Device: BlackBerry Playbook
My Carrier: Telus

Re: How to obtain stage orientation change event, but not affect the display of the app?

[ Edited ]

To be honest, I don't want the stage to resize, because there is then that little animation i want to avoid.

Event.RESIZE happens only if I don't call preventDefault.

 

I just want an event that tells me the orientation changed, but it only happens for any orientation BUT the default one. I want information that the device changed its orientation, I don't want the display to be affected in anyway but by my code.

 

I have put my code on GitHub for all to play.

 

Thanks!

--
mat janson blanchet
twitter: @jansensan, portfolio: http://jansensan.net/portfolio, tech blog: http://jansensan.net
Retired
Posts: 427
Registered: ‎02-22-2012
My Device: BlackBerry Z10, BlackBerry Dev Alpha C, BlackBerry PlayBook
My Carrier: Bell

Re: How to obtain stage orientation change event, but not affect the display of the app?

[ Edited ]

Hi,

 

I believe what you're looking for is a sensor. We have the 2.1 AIR SDK coming out closer to the end of the month. It has some bug fixes as well as well as sensor support. Sensors are a prebuilt ANE that you can use to detect things like orientation changes. 

 

The sensors are:

 

  • Magnetometer Sensor
  • Gyroscope Sensor
  • RotationMatrix Sensor

Cheers,

Dustin

Follow me on Twitter: @dustinmalik
-----------------------------------------------------------------------------------------------------
Keep up to date on BlackBerry development: http://devblog.blackberry.com/
Contributor
Posts: 30
Registered: ‎04-04-2012
My Device: BlackBerry Playbook
My Carrier: Telus

Re: How to obtain stage orientation change event, but not affect the display of the app?

Thanks dmalik, I am looking forward to that. Although I still believe that an event should be fired when the device is put back to the default position (otherwise, how does on know it has changed?), the addition of sensors will most assuredly be welcome.

--
mat janson blanchet
twitter: @jansensan, portfolio: http://jansensan.net/portfolio, tech blog: http://jansensan.net