3
\$\begingroup\$

I'm working on libgdx project. I basically recreate my game which I coded in xna to make it available for android. Here's the problem android has many screen resolutions, and I want to know how to get them.

i tried Gdx.graphics.getWidth() or getHeight() but it gives me null pointer exceptions.

\$\endgroup\$
10
  • \$\begingroup\$ Are you calling that before or after you new up a LwjglApplication? \$\endgroup\$
    – bornander
    Jan 19, 2015 at 14:32
  • \$\begingroup\$ sorry i don't understand \$\endgroup\$
    – Brsgamer
    Jan 19, 2015 at 14:33
  • \$\begingroup\$ The LwjglApplication is responsible for setting the static graphics property on the Gdx class, if you don't new up an instance of LwjglApplication you're going to have to set Gdx.graphics yourself. \$\endgroup\$
    – bornander
    Jan 19, 2015 at 14:35
  • \$\begingroup\$ listen, i don't know libgdx very good. let me show you the code public Player(Vector2 position){ this.pos = position; speed = 500; } this is as you can see constructor for player class. Player p = new Player(new Vector2(224, 48)); and i have this in main class after class opens. i want to change that hardcoded numbers with screen width and height and modify it with basic - or + operators. \$\endgroup\$
    – Brsgamer
    Jan 19, 2015 at 14:38
  • \$\begingroup\$ What platform are you building for? Desktop, Android or HTML? \$\endgroup\$
    – bornander
    Jan 19, 2015 at 14:42

3 Answers 3

3
\$\begingroup\$
camera = new OrthographicCamera();
camera.setToOrtho(false,800,400);

Above code will draw everything in your game on 800*400 virtual screen (in pixel) and fit, stretch, fill etc (see viewports ) on device's screen, independent of device screen size.

https://github.com/libgdx/libgdx/wiki/Orthographic-camera

https://github.com/libgdx/libgdx/wiki/Viewports

\$\endgroup\$
3
\$\begingroup\$

once there's a valid screen loaded Gdx.graphics must have a valid width and height defined, i think you're just asking for something null that's not related

i.e. float[] size; size[0] = Gdx.graphics.getWidth(); ...

which it will give you a nice NPE

\$\endgroup\$
1
\$\begingroup\$

You dont need to specify android screen fix size if you want you Libgdx project on all android Devices. I have test this. Just specify screen size that you can get from Gdx.graphics.getWidth() and also for Height. You can give some fix value to Camera.. Test it on different android devices and this will work same on all.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .