Question

I have seen lots of topics on LibGDX and screen/cam coordinates and also some on window resizing, but I just can't find the solution to the following problem I have.

When making a basic stage and a basic actor in this stage, say windowsize 480x320, everything is OK. I can click my actor and it will respond. But when I resize my window, say to 600x320, everything looks right, but my clicklistener is not working anymore. Also, the stage coordinates are moved or messed up.

I use the following code:

stage.addListener(new InputListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

        //determine if actor was hit
        System.out.println(stage.hit(x, y, true));
        return true;
    }
});

Also, I am resizing my stage camera viewport to correspond to the window:

    stage.getCamera().viewportWidth = Gdx.graphics.getWidth();
    stage.getCamera().viewportHeight = Gdx.graphics.getHeight();

So when resizing, I get the desired effect on screen, but my listener does not respond - the actor seems 'offset' of where I am clicking. What am I doing wrong? Should I move my actor or my cam, or zoom my cam according to the resize? Can someone please explain this to me?

Thanks a lot in advance!

EDIT: below is the complete code of my class.

public class HelpMePlease implements ApplicationListener{

// A standard simple Actor Class
class CustomActor extends Actor {

    Texture texture = new Texture(Gdx.files.internal("data/testTex2.png"));
    TextureRegion pixelTexture = new TextureRegion(texture, 0, 0, 1, 1);
    Sprite sprite = new Sprite(texture);

    public CustomActor() {
        setWidth(128);
        setHeight(128);
        setBounds(getX(), getY(), getWidth(), getHeight());
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        batch.draw(sprite, getX(), getY(), 0f, 0f, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
    }
}

public Stage stage;
public CustomActor actor;

@Override
public void create() {


    stage = new Stage(480,320,true);

    actor = new CustomActor();

    stage.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            //determine if actor was hit
            System.out.println(stage.hit(x, y, true));
            return true;
        }
    });

    Gdx.input.setInputProcessor(stage);
    stage.addActor(actor);

}

@Override
public void resize(int width, int height) {

    //resize cam viewport
    stage.getCamera().viewportWidth = Gdx.graphics.getWidth();
    stage.getCamera().viewportHeight = Gdx.graphics.getHeight();

}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    stage.getCamera().update(); //just to be sure, I don't know if this is necessary
    stage.act();
    stage.draw();
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

}

Was it helpful?

Solution

You can change your resize por this using the latest nightly.

@Override
public void resize(int width, int height) {
    stage.getViewport().update(width, height, true);
}

the last parameter "true" will center the camera in the screen

OTHER TIPS

I think your actor is positioning itself good enough, but your display may be a bit off.

Try

batch.draw(sprite, getX(), getY(), 0f, 0f, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());

Instead of

batch.draw(sprite, getX(), getY(), getWidth(), getHeight(), 0f, 0f, getScaleX(), getScaleY(), getRotation());

Spritebatch has the following arguments:

public void draw (Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth,int srcHeight, boolean flipX, boolean flipY)

I guess you just mixed some arguments up by mistake, would you kindly take a look at it?

Problem solved by updating my LibGDX Version using Gradle and using the new Viewport options! Thanks for taking the time everyone!

In my case adding

stage.getViewport().setScreenSize(width, height);

in resize() solved problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top