0

I have a tiled map on which I set characters(every character is of the size of one tile). I managed to make them clickable, even when screen resizes everything works perfect. Every time I click on character I want a button to show up above it. For a button I use stage and place the button in the place I clicked with small transition and it also works.

My problem is when I try to use clicklistener on this button. If the screen does not resize, clicklistener works. Problem starts when the screen get resized - clicks on players works well, only button does not work - after a resize the clicking space and the button space are not aligned. For test purposes I made a test map that shows me grids. It seems that stage is not properly resized. Picture for a reference. I tried many solutions I came upon on the Internet and still can't find a solution to my problem. I have shortened my code down to a minimum basic example:

public class Test extends ApplicationAdapter {

public static Stage stage;
public TiledMap tiledMap;
static OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
public static boolean showMenu;
GestureDetector gesture;
InputMultiplexer myInputMultiplexer;
public static int posx, posy;
public static Image move;
public Texture moveMenu;

@Override
public void create() {

    moveMenu = new Texture(Gdx.files.internal("move.png"));
    gesture = new GestureDetector(new MyGestureListener());
    myInputMultiplexer = new InputMultiplexer();
    float unitScale = 1 / 32f;
    camera = new OrthographicCamera();
    camera.setToOrtho(true, 33, 21);
    stage = new Stage(new ScreenViewport());
    stage.getViewport().setCamera(camera);
    tiledMap = new TmxMapLoader().load("test.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, unitScale);
    myInputMultiplexer.addProcessor(stage);
    myInputMultiplexer.addProcessor(gesture);
    Gdx.input.setInputProcessor(myInputMultiplexer);
    move = new Image(moveMenu);
    move.setWidth(2);
    move.setHeight(2);

    move.addListener(new ClickListener() {

        @Override
        public void clicked(InputEvent event,  float x, float y) {
            move(); //my action, works fine
            showMenu = false;


        }
    });
    stage.addActor(move);
}

@Override
public void render() {
    super.render();
    stage.act();
    tiledMapRenderer.setView(camera);
    camera.update();
    tiledMapRenderer.render();

 if (showMenu) {
        mainMenuDraw();
    }
}

public static void mainMenuDraw() {
    move.setPosition(posx, posy-2);     
    stage.draw();
}

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

public static OrthographicCamera getCamera() {
    return camera;
}

public static Vector3 unprojectCoords(Vector3 coords) {
    camera.unproject(coords);   
    return coords;
}   }

and part of my gesturelistener:

public boolean touchDown(float x, float y, int pointer, int button) {

    Vector3 temp_coord = new Vector3(x, y, 0);
    Vector3 coords = Test.unprojectCoords(temp_coord);

    return false;
}

@Override
public boolean tap(float x, float y, int count, int button) {

    Vector3 temp_coord = new Vector3(x, y, 0);

    Vector3 coords = Test.unprojectCoords(temp_coord);

    Test.posx = (int) coords.x;
    Test.posy = (int) coords.y;

    tap = true;
    Test.showMenu = true;

    return false;
}
Dick Tracy
  • 57
  • 1
  • 4

1 Answers1

0

I would suggest to you read more about re-sizing and displaying pixels.

You need to recalculate pixels always when you render something. - player, background image, buttons, events.

Actually you don't need to use resize method, just get the camera width and height.

You need to share more code, because it depends on everything. I don't see how you are doing rendering.

  • tiledMapRenderer.render(), background rendering / layers?
  • player rendering?
  • menu rendering?
    • buttons rendering?

Example: (the same should be for event handler)

public GameButton(TextureRegion reg, float x, float y, OrthographicCamera cam) {

        this.reg = reg;
        this.x = x;
        this.y = y;
        this.cam = cam;

        width = reg.getRegionWidth();
        height = reg.getRegionHeight();
        vec = new Vector3();

        Texture tex = Game.res.getTexture("hud");
        font = new TextureRegion[11];
        for(int i = 0; i < 11; i++) {
            font[i] = new TextureRegion(tex, 32 + i * 9, 16, 9, 9); //use height  and width here)
        }

    }
Dezigo
  • 3,220
  • 3
  • 31
  • 39