I use FitViewport with a virtual size of 1920x1080.

 viewport = new FitViewport(AssetLoader.VIRTUAL_WIDTH, AssetLoader.VIRTUAL_HEIGHT); stage = new Stage(viewport); 

There is also a custom actor inherited from the class Actor . Below is the code of its constructor.

 public MyActor(String text, float x, float y) { this.text = text; FreeTypeFontGenerator generator = new FreeTypeFontGenerator(AssetLoader.robotoFontFile); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); parameter.size = 96; font = generator.generateFont(parameter); generator.dispose(); layout = new GlyphLayout(); layout.setText(font, text); super.setBounds(x, y, layout.width, layout.height); this.debug(); renderer = new ShapeRenderer(); this.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { Gdx.app.log("MyActor", text + " is touched!"); return true; } }); } 

And the overridden draw method:

 public void draw(Batch batch, float parentAlpha) { super.draw(batch, parentAlpha); batch.end(); renderer.begin(ShapeRenderer.ShapeType.Line); this.drawDebugBounds(renderer); renderer.end(); batch.begin(); font.draw(batch, text, getX(), getY()); } 

The problem is that when you start it turns out this:

bounds strange behavior

And pressing is triggered only if you press inside the green rectangle, although it was assumed that the green rectangle and the text will overlap.

Perhaps the problem with the broadcast, but I could not find anything suitable, tell me, please.

    1 answer 1

    The easiest solution was to add text height to its y-coordinate:

     font.draw(batch, text, getX(), getY() + layout.heigth);