我是Libgdx的新手,刚开始用表格查看UI。我有一个textlabel(实际上是一个字体)和2个图像按钮。我喜欢一行中的字体和下面的两个按钮。我已经按照教程进行了操作,但似乎标签的行正在按钮行下方呈现。有谁知道为什么?
这是我的代码
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();
//Set image for not pressed
ButtonStyle.up = buttonSkin.getDrawable("sfxButton");
//Set image for pressed
ButtonStyle.down = buttonSkin.getDrawable("apiButton");
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();
//Set image for not pressed
ButtonStyle.up = buttonSkin.getDrawable("apiButton");
//Set image for pressed
ButtonStyle.down = buttonSkin.getDrawable("sfxButton");
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;
}