I'm new to Libgdx and just started looking at UI with tables. I have a textlabel (a font actually) and 2 image buttons. I'd like the font to be on one row and the 2 buttons below it. I've followed a tutorial but it seems the label's row is being rendered below the buttons row. Does anyone know why?
Here's my code
public MainScreen() {
spriteBatch = new SpriteBatch();
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false, spriteBatch);
//Create the text object that says "PennyPop"
TextLabel pennyLabel = new TextLabel();
//Create the sfx and api button button
ImageButton sfxButton = createSfxButton();
ImageButton apiButton = createApiButton();
Gdx.input.setInputProcessor(stage);
//Create the root table
Table rootTable = new Table();
rootTable.setFillParent(true);
rootTable.debug();
//Set up rows and columns
table = new Table();
table.add(pennyLabel);
table.row();
table.add(apiButton);
table.add(sfxButton).padLeft(10);
rootTable.add(table);
stage.addActor(rootTable);
}
/*ACTORS */
public class TextLabel extends Actor {
float textWidth;
float textHeight;
private BitmapFont font;
public TextLabel(){
font = new BitmapFont(Gdx.files.internal("assets/font.fnt"),false);
textWidth = font.getBounds("PennyPop").width;
textHeight = font.getBounds("PennyPop").height;
font.setColor(Color.RED);
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
font.draw(batch, "Pennypop", (stage.getWidth()-textWidth)/2, (stage.getHeight()-textHeight)/2);
//Also remember that an actor uses local coordinates for drawing within
//itself!
}
}
public ImageButton createSfxButton(){
ImageButton sfxButton;
buttonsUi = new TextureAtlas("assets/buttons/buttons.pack");
Skin buttonSkin = new Skin(buttonsUi);
ImageButtonStyle ButtonStyle = new ImageButtonStyle();
ButtonStyle.up = buttonSkin.getDrawable("sfxButton"); //Set image for not pressed
ButtonStyle.down = buttonSkin.getDrawable("apiButton"); //Set image for pressed
ButtonStyle.pressedOffsetX = 1;
ButtonStyle.pressedOffsetY = -1;
sfxButton = new ImageButton(ButtonStyle);
sfxButton.setBounds(0, 0, 200, 200);
//Initialize Sound Effect
final Sound wavSound = Gdx.audio.newSound(Gdx.files.internal("assets/button_click.wav"));
sfxButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
wavSound.play();
}
});
return sfxButton;
}
public ImageButton createApiButton(){
ImageButton apiButton;
buttonsUi = new TextureAtlas("assets/buttons/buttons.pack");
Skin buttonSkin = new Skin(buttonsUi);
ImageButtonStyle ButtonStyle = new ImageButtonStyle();
ButtonStyle.up = buttonSkin.getDrawable("apiButton"); //Set image for not pressed
ButtonStyle.down = buttonSkin.getDrawable("sfxButton"); //Set image for pressed
ButtonStyle.pressedOffsetX = 1;
ButtonStyle.pressedOffsetY = -1;
apiButton = new ImageButton(ButtonStyle);
apiButton.addListener(new ClickListener(){
//Fetch the weather api and add it to the stage
@Override
public void clicked(InputEvent event, float x, float y){
WeatherReport weatherReport = new WeatherReport();
}
});
return apiButton;
}
Aucun commentaire:
Enregistrer un commentaire