Hello everybody, I've been a bloody problem for a few days now. I created a button in LibGDX. This button I have packed on a table and the table I have packed on a stack and the stack again to a stage. The problem is that I have to push a few pixels next to the button in order to erase. That means when I press exactly on the button nothing happens. I will copy here the times the appropriate code slightly shortened. I could also shorten the code a bit.
public class HUD implements Disposable {
PlayScreen screen;
public static Stage stage;
private Viewport viewport;
private Stack stack;
public HUD(PlayScreen screen, SpriteBatch sb) {
this.screen = screen;
viewport = new FitViewport(PlayScreen.V_WIDTH, PlayScreen.V_HEIGHT, new OrthographicCamera());
stage = new Stage(viewport, sb);
stack = new Stack(); // This is the Stack
stack.setFillParent(true);
stack.addActor(buildHudLayer()); // Here I add another Table to the Stack
stack.addActor(buildButtonLayer()); // Here I add the Table with the Button to the Stack
stage.addActor(stack); // Here I add the Stack to the Stage
PlayScreen.multiplexer.addProcessor(stage); // Here I add the input of the Stage to an InputMultiplexer
screen.setInput();
}
private Label fpsLabel;
private Label levelLabel;
private Table buildHudLayer() { // This is another table
Table table = new Table();
table.top();
fpsLabel = new Label(Gdx.graphics.getFramesPerSecond() + "fps", new Label.LabelStyle(new BitmapFont(), Color.BLACK));
levelLabel = new Label(Masterrunner.currentLevel + " - " + Masterrunner.currentLevel, new Label.LabelStyle(new BitmapFont(), Color.BLACK));
table.add(fpsLabel).expandX();
table.add(levelLabel).expandX();
return table;
}
private Table buildButtonLayer() {
Table table = new Table(); // Here I create the table
table.setFillParent(true);
table.setDebug(true);
table.top();
Button.ButtonStyle style = new Button.ButtonStyle(); // This is the Button
style.up = new TextureRegionDrawable(PlayScreen.atlas.findRegion("Pause"));
style.down = new TextureRegionDrawable(PlayScreen.atlas.findRegion("Pause1"));
Button pauseButton = new Button(style);
table.add(pauseButton).size(20, 20); // Here I add the Button to the Table
return table; // Here I return the Table
}
public void update() {
fpsLabel.setText(Gdx.graphics.getFramesPerSecond() + "fps");
levelLabel.setText(Masterrunner.currentWorld.getName() + " - " + Masterrunner.currentLevel);
stage.act();
}
public void render() {
stage.draw();
}
}
I hope the information is sufficient, if not corrected me please.
Dear greetings UbuLin ;D
↧