Android Detect rotation

Hi, I just want to know if the phone has changed orientation. I’ve made some tests with the default template with no good results

function added(e)
{
removeEventListener(Event.ADDED_TO_STAGE, added);
stage.addEventListener(Event.RESIZE, resize);
#if ios
haxe.Timer.delay(init, 100); // iOS 6
#else
init();
#end
}

function resize(e)
{
trace([stage.stageWidth, stage.stageHeight]);
}

[1920,1080] appears only at the start of the application, but nothing happens when I rotate the phone. Should I made something else (Tested on S5 with screen rotation activated)

Regards
Luis

You should get a RESIZE event every time you rotate the screen. Are you using the latest version of OpenFL and Lime?

I think so…

Lime 2,7,0
openFL 3,4,0

Android 5.0 On Samsung S5

AFAIK you can just do Stage.getOrientation();

Still can’t catch the rotation event. Just to be sure that is not “hidden problem”, I started a project from scratch:

package com.test.decartas;

import openfl.display.BitmapData;
import openfl.display.Bitmap;

import openfl.Assets;
import openfl.events.Event;

import openfl.display.Sprite;
import openfl.Lib;

/**

  • @author Luis Recagno
    */
    class Main extends Sprite
    {

public function new()
{
super();
var bd:BitmapData = Assets.getBitmapData(“img/splash.jpg”);
var bmp:Bitmap = new Bitmap(bd);
this.addChild(bmp);
this.addEventListener(Event.ADDED_TO_STAGE, added);
}

public static function main()
{
trace(‘enter main’);
Lib.current.addChild(new Main());
//
}

function added(e)
{
trace(‘enter added’);
removeEventListener(Event.ADDED_TO_STAGE, added);
this.addEventListener(Event.RESIZE, resize);
}

function resize(e)
{
trace(‘enter resize’);
trace([stage.stageWidth, stage.stageHeight]);
Lib.current.stage.scaleX = stage.stageWidth / 540;// 2;
Lib.current.stage.scaleY = stage.stageHeight / 960;//2;

}
}

lime test android…

The bitmap appears on screen and it “rotates” ONLY if I rotate the device 180 degrees. On the debug console on the pc appear the 2 messages (enter main and enter added) but IN ANY case the “enter resize”. Any advice or walkaround would be really really appreciated.

Best regards
Luis

Partially solved!!

The problem was in the project.xml file!!. If you set th window size there, the resize event is not thrown!..
After that I needed to detect the reverse portrait (180 degrees), which has been solved overriding the androidManifest file adding android:screenOrientation=“fullSensor”. I have now the event resize thrown in my application for the 4 possible orientations!!!..

Also I find out that Stage.getOrientation() is on legacy version ( you have to compile using -Dlegacy)… I tried and I receive 0 as a result no matter the orientation I have.

I also tried with getNormalOrientation which returns the same result ( always is 0 ) but as a Tint object and finally tried shouldRotateInterface(Stage.OrientationPortrait) that always return true…
Honestly I have no idea what else I should do… create an extension and use JNI?

Thanks in advance for any tip you can give.
rgds
Luis

There should be three modes – all four orientations, or portait-only or landscape only. it should dispatch a resize event if it rotates to a new size. Is that what you need, or do you need more?

Yes…right now it throwns the resize event with the 4 possible orientations. But no way to distinguish between 0 - 180 and 90-270. I will solve it adding a rotate button for the user allowing them to rotate it manually 180 degrees.
However I believe that know the rotation angle of the stage could be an interesting device property to know for game Development, either thru the resize event or maybe thru sensors? (as far as I saw only accelerometers are implmented)…

Thanks for the response

Regards
Luis

On Android and iOS, I thought the “flipped” orientation was done automatically. It should resize to landscape/portrait, but always be “right side up”

Yes it does the flip automatically but what if you need to modify your screen if you are at 0 or 180 ( for example for a chess table ). It would be great to know if I am at 0 or at 180.