Android screensize

Hey,

when doing

    trace(flash.Lib.current.stage.stageHeight);

on my nexus 7, I get: 1104
Newest openfl/lime with -Dv2
That is not true! My nexus has a screen height of 1200!
I assume that is the screensize without the menu buttons. But since the menu buttons are not shown during the game, it remains incorrect.

How can I get the correct screensize?
Thanks!
Nathan

A little remark: I get this wrong result, when I trace it during startup. Later, the result is different (I get the correct 1200). But I do not know when the change happens, and I need the correct size during initialization.

When exactly during startup are you outputting the width? Usually getting the dimensions after the Sprite has been added to the stage (Event.ADDED_ON_STAGE) with “stage.stageWidth/Height” works.

I do it in the main function. And when I wait for ADDED_ON_STAGE (on flash.Lib.current), it never fires.

I think you should add event listener to your Main class.

class Main extends Sprite 
{
        public function new() 
	{
		super();	
		addEventListener(Event.ADDED_TO_STAGE, added);
	}
        function added(e) 
	{
		removeEventListener(Event.ADDED_TO_STAGE, added);
		trace(stage.stageWidth, stage.stageHeight);
	}
}
1 Like

Can this also be done when my Main class does not extend Sprite?

Waiting for ADDED_TO_STAGE is entirely unnecessary nowadays. The Main class will already be onstage by the time you add that event listener, thanks to DocumentClass:

class DocumentClass extends Main {
    public function new() {
        Lib.current.stage.addChild(this);
        super();
        dispatchEvent(new Event(Event.ADDED_TO_STAGE, false, false));
    }
}

(I know it dispatches a fake ADDED_TO_STAGE event, so your code will still work. But why bother?)


If it isn’t a DisplayObject, it can’t be added to the stage. Fortunately, you can use Lib.current.stage to get a reference from anywhere.

var stage:Stage = Lib.current.stage;
trace(stage.stageWidth, stage.stageHeight);
1 Like

Nice post @player_03 now I know two new things =)

@Nathan_Husken maybe you can add a small wait to let android hide the button’s bar? Sys.sleep(0.05) just a hack for the moment

Why not wait for a RESIZE event? That should be dispatched when the bar goes away.

I dont know what @Nathan_Husken is trying to do but he posted

It may be dangerous to not initialize(his part) and wait for the RESIZE event. Not sure if RESIZE will be fire in all devices at start.
Maybe the best solution could be a combination, wait for the RESIZE, if it dosen’t arrive in x seconds init with the values from stage.

Yeah, that could work, but it would have to be the second RESIZE event. It turns out OpenFL dispatches one every single time, right after the ADDED_TO_STAGE event.

However, it’s generally better to respond to each RESIZE event as it happens. Not all Android devices will hide the bar that quickly. Plus, if the bar comes back, the app should resize so that nothing gets cut off.

Didn’t know openfl always fires a RESIZE event, good to know. I agree the best solution is to always auto adapt the screen.

I was thinking, maybe “Lib.current.stage.fullScreenHeight” and “Lib.current.stage.fullScreenWidth” have the right value?

openfl._legacy.display.Stage has no field fullScreenHeight

Maybe that is not available in openfl with -Dlegacy?
My project does not yet compile without it. I am working on it.

For now, waiting 0.5 seconds helps. I fear that it will not be the case on all devices, but for my nexus4 and nexus7 it works.

Listening to resize makes sense, but I am initilizing the size of some game elements based on the fullscreen size and I do not want to resize them later.

Thanks for all the input!

That’s exactly why I wrote this library.