【发布时间】:2013-05-03 07:19:48
【问题描述】:
我是 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() 方法 无论如何感谢您的帮助
【问题讨论】:
标签: java button touch libgdx actor