我正在学习libGDX,只是为了好玩,并且很早就开始了。我的演员似乎没有接收触摸/鼠标输入。我已经广泛搜索并检查了所有常见错误(设置输入过程,设置边界,设置可触摸)但仍然没有运气。有人可以帮助我。
阶段
/* GameStage.java */
public class GameStage extends Stage{
private Game gameInstance;
public GameStage(Game gameInstance) {
super(new ScreenViewport());
Gdx.input.setInputProcessor(this);
Tile tile = new Tile(2);
addActor(tile);
}
}
演员
/* Tile.java */
public class Tile extends Actor{
public enum Side{
FRONT,
BACK
}
private int value;
private Texture backTexture;
private Texture frontTexture;
private Side currentSide;
public Tile(int value) {
this.value = value;
backTexture = new Texture("TileBack.png");
frontTexture = new Texture("Tile " + String.valueOf(value)+".png");
currentSide = Side.BACK;
setPosition(0, 0);
setSize(128, 128);
setBounds(getX(), getY(), getWidth(), getHeight());
setTouchable(Touchable.enabled);
addListener(new InputListener(){
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Touch");
super.touchUp(event, x, y, pointer, button);
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
if(currentSide == Side.BACK){
batch.draw(backTexture, getX(), getY(), getWidth(), getHeight());
}
else{
batch.draw(frontTexture, getX(), getY(), getWidth(), getHeight());
}
}
public int getValue() {
return value;
}
}
我错过了什么?我也尝试在舞台上实现touchUp并返回false和true但没有运气。
提前致谢!
答案 0 :(得分:1)
touchUp
返回touchDown
(默认为true
)时才会调用 false
事件。您可以通过执行以下操作来修复代码:
addListener(new InputListener(){
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Touch");
super.touchUp(event, x, y, pointer, button);
}
// Add this:
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true; // Important!
}
});
旁注:最好使用GDX logger代替System.out.println
。