我是 libgdx 和学习 libgdx scene2d 的新手。
在菜单屏幕构造函数中,我创建了一个具有设备分辨率的阶段,并将阶段添加到输入处理器:
stage = new Stage(800, 480, false);
stage.getCamera().position.set(400, 240, 0);
Gdx.input.setInputProcessor(stage);
现在,我创建了一个按钮并将其添加到舞台:
PButton start = new PButton(Assets.btn, Assets.btn_p, Assets.font50,
"START");
start.setPosition(600, 400);
start.addListener(new ActorGestureListener() {
@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
getGame().setScreen(new GameScreen1(getGame()));
System.out.println("set game");
}
});
stage.addActor(start);
其中PButton
将Button
类扩展为:
public class PButton extends Button {
private BitmapFont font;
private String text;
private float sizex;
private float sizey;
private float posx;
private float posy;
public PButton(TextureRegion up, TextureRegion down, BitmapFont font,
String text) {
super(new TextureRegionDrawable(up), new TextureRegionDrawable(down));
this.font = font;
this.text = text;
this.sizex = up.getRegionWidth();
this.sizey = down.getRegionHeight();
setSize(sizex, sizey);
font.setScale(Initiate.getM_ratio());
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
font.draw(batch, text, posx + sizex / 2 - font.getBounds(text).width
/ 2, posy + sizey / 2 + font.getBounds(text).height / 2);
}
@Override
public void setPosition(float x, float y) {
this.posx = x;
this.posy = y;
super.setPosition(posx, posy);
}
}
但是当我点击按钮时,它没有得到输入。
更新:经过深度调试后问题解决了我发现我的 stage.dispose() 上一个屏幕的方法如果没有正确调用,谢谢你的帮助