Reputation: 3
EDIT: First Cemmentator "Second" was right. I forgot to set Gdx.input.setInputProcessor(stage); - Now everything is working as it should. Thanks!
My Libgdx Buttons "Back", "Menu" and "TryAgain" (which bring you back to another Screen) are not working.
I worked through every solution that I could find but they still not work properly. I also use Buttons and stages in my other Screens and they all work like a charm.
Do you guys see any mistakes in the code? CreateStage()
creates the stage and they get displayed through the render Method.
Any Help is greatly appreciated!
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();
}
Upvotes: 0
Views: 372
Reputation: 4259
You are probably missing the settings of the input processor to your stage (at least it's not in the code you have posted).
Gdx.input.setInputProcessor(stage);
Note:
Assuming the the Buttons class returns a proper actor.
(see my comment above)
Upvotes: 0