gpt4 book ai didi

java - 如何检测 libGdx 中的文本点击

转载 作者:行者123 更新时间:2023-12-01 09:44:13 24 4
gpt4 key购买 nike

如何检测对文本的点击(使用 LibGdx)。我只想在用户单击文本时更改字体颜色。这就是我到目前为止所得到的。

public class MainMenuScreen implements Screen,InputProcessor {

final MainClass game;
String playButton = "PLAY";
private int screenWidth;
private int screenHeight;

public MainMenuScreen(final MainClass gam){
game=gam;
Gdx.input.setInputProcessor(this);

screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();
}

@Override
public void render(float delta) {

Gdx.gl.glClearColor(0, 0.2f, 0, 10);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

game.batch.begin();
game.font.draw(game.batch,playButton,screenWidth/3,screenHeight/2);
game.batch.end();
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
//here, how to detect that the user clicked on the text??

return true;
}

我已将文本显示在屏幕左中附近的某个位置。如何知道用户点击了该文本?

最佳答案

您需要创建一个带有文本边界的直肠。为此,您需要 GlypLayout 来获取文本的高度和宽度。

public class MainMenuScreen implements Screen,InputProcessor {

final MainClass game;
String playButton = "PLAY";
private int screenWidth;
private int screenHeight;

Rectangle recPlayButton;
GlyphLayout layout;

public MainMenuScreen(final MainClass gam){
game=gam;
Gdx.input.setInputProcessor(this);

screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();\

layout = new GlyphLayout();
recPlayButton = new Rectangle();

layout.setText(game.font, playButton);
recPlayButton.set(screenWidth / 3, screenHeight / 2, layout.width, layout.height);
}

@Override
public void render(float delta) {

Gdx.gl.glClearColor(0, 0.2f, 0, 10);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

game.batch.begin();
game.font.draw(game.batch,playButton,screenWidth/3,screenHeight/2);
game.batch.end();
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 touchPos = new Vector3(screenX, screenY, 0);

if (recPlayButton.contains(touchPos.x, touchPos.y)) {
Gdx.app.log("Test", "Button was clicked");
return true;
}

return false;
}

关于java - 如何检测 libGdx 中的文本点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38197588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com