libGDX 演员没有收到输入
libGDX Actor not receiving input
我正在学习 libGDX,只是为了好玩,而且很早就碰壁了。我的演员似乎没有收到 touch/mouse 输入。我已经广泛搜索并检查了所有常见错误(设置输入过程、设置边界、设置可触摸)但仍然没有运气。谁能帮帮我。
阶段
/* 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 但没有运气。
提前致谢!
touchUp
事件仅在 touchDown
returns 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
。
我正在学习 libGDX,只是为了好玩,而且很早就碰壁了。我的演员似乎没有收到 touch/mouse 输入。我已经广泛搜索并检查了所有常见错误(设置输入过程、设置边界、设置可触摸)但仍然没有运气。谁能帮帮我。
阶段
/* 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 但没有运气。
提前致谢!
touchUp
事件仅在 touchDown
returns 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
。