gpt4 book ai didi

java - LibGDX 按钮无法识别点击(Stage + ChangeListener)

转载 作者:行者123 更新时间:2023-12-02 05:11:56 25 4
gpt4 key购买 nike

编辑:第一水泥工“第二”是对的。我忘记设置 Gdx.input.setInputProcessor(stage); - 现在一切都正常进行。谢谢!

<小时/>

我的 Libgdx 按钮“后退”、“菜单”和“再试一次”(使您返回到另一个屏幕)不起作用。

我尝试了所有能找到的解决方案,但它们仍然无法正常工作。我还在其他屏幕中使用按钮和阶段,它们都非常有用。

你们发现代码中有什么错误吗? CreateStage() 创建舞台并通过渲染方法显示它们。

非常感谢任何帮助!

public class HighscoreScreen extends ScreenAdapter {

private final GameClass game;
private int score;
private String name;
private boolean menuView;

private SpriteBatch batch;
private BitmapFont scoreFont;

private String[] players;
private int[] scores;
private int counter = 0;
private OrthographicCamera camera;
private Stage stage;
private ScreenViewport v;

public HighscoreScreen (GameClass game, OrthographicCamera camera, int score, String name) {
this.camera = camera;
this.game = game;
this.score = score;
this.name = name;
this.batch = new SpriteBatch();
menuView = false;
batch = new SpriteBatch();

scoreFont = new BitmapFont(Gdx.files.internal("Fonts/score.fnt"),
Gdx.files.internal("Fonts/score.png"),false);
createStage();
}

public HighscoreScreen (GameClass game, OrthographicCamera camera){
this.game = game;
this.camera = camera;
this.batch = new SpriteBatch();
menuView = true;

scoreFont = new BitmapFont(Gdx.files.internal("Fonts/score.fnt"),
Gdx.files.internal("Fonts/score.png"),false);

}

public void createStage(){

v = new ScreenViewport();
v.setCamera(camera);
stage = new Stage(v, batch);

if (menuView) {

Buttons back = new Buttons("Back");
stage.addActor(back.createButton(GameInfo.WIDTH / 2 - 100,
GameInfo.HEIGHT / 2 - 200));

back.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MenuScreen(game));
}
});

} else {

Buttons tryAgain = new Buttons("Try Again");
stage.addActor(tryAgain.createButton(GameInfo.WIDTH / 2 - 200,
GameInfo.HEIGHT / 2 - 200));

tryAgain.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new SurvivalScreen(game, name));
}
});

Buttons menu = new Buttons("Menu");
stage.addActor(menu.createButton(GameInfo.WIDTH / 2,
GameInfo.HEIGHT / 2 - 200));

menu.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MenuScreen(game));
}
});
}
}

@Override
public void render(float delta) {

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

if (menuView) {

if (counter == 0) {

createStage();
writeScoresToFile();
readHighscore();
counter++;

}

batch.begin();
highscoreToFont();
batch.end();
stage.act();
stage.draw();


} else {

if (counter == 0) {

createStage();
writeScoresToFile();
readHighscore();
compareHighscore();
writeHighscore();
counter++;

}

batch.begin();
highscoreToFont();
batch.end();
stage.act();
stage.draw();

}

}

public void dispose(){
batch.dispose();
stage.dispose();
}

最佳答案

您可能缺少舞台输入处理器的设置(至少它不在您发布的代码中)。

Gdx.input.setInputProcessor(stage);

注意:
假设 Buttons 类返回一个正确的 actor。
(请参阅上面我的评论)

关于java - LibGDX 按钮无法识别点击(Stage + ChangeListener),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321168/

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