Libgdx widget with scene not working
2 months ago I stumbled upon a problem that my game I was working on was not scalable with other devices. So I decided to use viewports as people said it was the best way and also the easy way. But it's been about two months since I started working and I don't have a working example yet. I read every page I could find, read the wiki several times and asked several people, but never got an answer that helped me get it to work.
So the problem is, I have to do 3 things. First I have a scene, it would be if I included all my actors (buttons and stuff), then I need my camera in this case OrthographicCamera, and then I need a viewport to scale everything.
This is my final test: http://pastebin.com/ZvZmeHLs
My creation
@Override
public void create () {
cam = new OrthographicCamera(800, 600);
viewport = new FillViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
stage = new Stage(viewport);
atlas = new TextureAtlas("LoadingScreen.pack");
Logo = new Image(atlas.findRegion("VeslaLogo"));
stage.addActor(Logo);
}
and my size
@Override
public void resize(int Width, int Height) {
viewport.update(Width, Height, true);
Logo.setSize(700, 500);
Logo.setX(Width / 2 - Logo.getWidth() / 2);
Logo.setY(Height / 2 - Logo.getHeight() / 2);
Logo.setOrigin(Logo.getWidth() / 2, Logo.getHeight() / 2);
}
At first I thought it was working, it looked good on one of them, everything was on the screen, but when I tested it on the s4 mini it got big. Now I don't know if it's because I just don't understand viewports or what I think is too complicated. The only thing I know is that I have completely lost and have no idea what else I could try. So it would be great if someone could nudge me in the right direction or explain to me why this is not working.
ps: I also tested it with fixed dimensions, but for some reason it didn't help
Fixed size example: http://pastebin.com/mpjkP9ru
private int VIRTUAL_WIDTH = 600;
private int VIRTUAL_HEIGHT = 900;
@Override
public void create () {
stage = new Stage(new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT));
cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
}
and my size
@Override
public void resize(int Width, int Height) {
Width = VIRTUAL_WIDTH;
Height = VIRTUAL_HEIGHT;
stage.getViewport().update(Width, Height, true);
}
If you need another example or something else, please let me know, I am able, I would try something to get it to work.
Edit: I managed to get it to work with @donfuxx. for those interested in the code: http://pastebin.com/8v2PB2t9
source to share
In your posted code, you seem to be using the resize method incorrectly because you are updating the viewport with a constant (!) Value (so you always set the width / height to 600/900, don't be surprised if you don't see the settings for different screen sizes):
@Override
public void resize(int Width, int Height) {
Width = VIRTUAL_WIDTH;
Height = VIRTUAL_HEIGHT;
stage.getViewport().update(Width, Height, true);
}
You have to do it as suggested in the wiki viewfinders and set the correct width / height values:
@Override
public void resize(int width, int height) {
// use true here to center the camera
// that what you probably want in case of a UI
stage.getViewport().update(width, height, false);
}
BTW: Also get rid of that logo defining code inside your resize method!
Update: Also I noticed that you are better off initializing your viewfinder and camera like this (use the viewport constructor with the camera parameter):
cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
viewport = new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, cam); //notice cam param here!
stage = new Stage(viewport);
Instead:
stage = new Stage(new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT));
cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
source to share
I personally developed my app for 1920x1080 and just scaled everything on smaller screens. My sprite camera:
camera = new OrthographicCamera(screen_width, screen_height); batch.setProjectionMatrix(camera.combined);
ensures sprites are drawn between -screen_width / 2.0 and screen_width / 2.0 on each screen (same for height)
using the default stage constructor outputs every actor between 0 and screen_width
stage = new Stage();
then I set the scale:
float global_scale = 1920.0f/screen_height;
actor.setScale(global_scale);
There are obviously other ways to do this, such as with a viewport.
source to share