我正在尝试在我的按钮上绘制开始菜单的文字。
我可以画出按钮和除字体/文字之外的所有内容。
这是字体代码:
初始化:
buttonFont = new BitmapFont(Gdx.files.internal(
"StartMenu/gamefont.fnt"), false);
buttonFont.setScale(0.2f);
这是我绘制舞台的地方,舞台里面有字体:
Gdx.gl20.glClearColor(1, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
sb.begin();
stage.draw();
sb.end();
这是初始化更多东西的东西:
@Override
public void resize(int width, int height) {
if(stage == null)
stage = new Stage();
stage.clear();
Gdx.input.setInputProcessor(stage);
style.font = buttonFont;
style.up = skin.getDrawable("Button");
style.down = skin.getDrawable("Button");
startGameBtn = new TextButton("Start Game", style);
startGameBtn.setWidth(250);
startGameBtn.setHeight(150);
startGameBtn.setX(Gdx.graphics.getWidth() / 2 - startGameBtn.getWidth() / 2);
startGameBtn.setY((float) (Gdx.graphics.getHeight() / 1.5));
startGameBtn.addListener(new InputListener()
{
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
game.setScreen(new Gameplay(game));
}
});
stage.addActor(startGameBtn);
stage.addActor(lb);
}
非常感谢任何帮助! :)
答案 0 :(得分:0)
我觉得你在resize()
创建按钮很奇怪。
我建议以及对我有用的是初始化create()
中的所有内容,在render()
中绘制舞台并将监听器放入show()
初始化
create(){
//Define the stage where you'll draw the TextButton
stage = new Stage();
//Define your font
buttonFont = new BitmapFont(Gdx.files.internal("StartMenu/gamefont.fnt"), false);
//Define the skin that contains your images
textureAtlas = game.assets.get("Images.pack", TextureAtlas.class);
skin.addRegions(textureAtlas);
//Define the TextButtonStyle
textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("Button");
textButtonStyle.down = skin.getDrawable("Button");
textButtonStyle.font = buttonFont;
textButtonStyle.fontColor = Color.WHITE;
textButtonStyle.downFontColor = new Color(0.5f, 0.5f, 0.5f, 1);
//Define the TextButton
startGameBtn = new TextButton("Start Game", textButtonStyle);
startGameBtn.setWidth(250);
startGameBtn.setHeight(150);
startGameBtn.setX(Gdx.graphics.getWidth() / 2 - startGameBtn.getWidth() / 2);
startGameBtn.setY(Gdx.graphics.getHeight() / 1.5f);
//Add the TextButton to the stage
stage.addActor(startGameBtn);
}
绘制TextButton
render(){
Gdx.gl20.glClearColor(1, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
使用TextButton
show(){
startGameBtn.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
game.setScreen(new Gameplay(game));
}
});
}