【问题标题】:libGDX Actor not receiving inputlibGDX Actor 没有接收到输入
【发布时间】:2015-12-05 18:26:43
【问题描述】:

我正在学习 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 但没有运气。

提前致谢!

【问题讨论】:

    标签: java libgdx scene2d


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2021-11-22
      • 2015-10-19
      • 2011-01-20
      相关资源
      最近更新 更多