【问题标题】:Cannot get events working in my libgdx Actor无法让事件在我的 libgdx Actor 中工作
【发布时间】:2012-09-11 07:26:13
【问题描述】:

我很难在 libgdx 中使用我的 Actor 处理事件。我正在使用每晚构建。

我的舞台设置在Screen 子类的show() 方法中:

stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
Gdx.input.setInputProcessor(stage);  
TestActor actor = new TestActor(); 
stage.addActor(actor);

我的演员类看起来像:

class TestActor extends Actor {
    private Sprite sprite;
    private TextureAtlas atlas; 

    public TestActor() {   
        atlas = new TextureAtlas(Gdx.files.internal("textures/images-packed.atlas"));
        sprite = atlas.createSprite("logo-96");

        setTouchable(Touchable.enabled);
        addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.debug(TestGame.TAG, "TestActor.touchDown()");
                return true;  // must return true for touchUp event to occur
            }
            public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.debug(TestGame.TAG, "TestActor.touchUp()");
            }
        });
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(sprite, getX(), getY());
    }            
}

事件似乎没有触发。奇怪的是,我使用了像 TextButton 这样的内置 UI 小部件,并且可以很好地触发这些事件。谁能看到我做错了什么?

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    您还应该为您的演员设置边界。 最好的方法(如果你想要与纹理相同的大小) 将这些行添加到您的构造函数中:

    setWidth(sprite.getWidth());
    setHeight(sprite.getHeight());
    setBounds(0, 0, getWidth(), getHeight());
    

    请注意,您还可以使用前 2 个参数设置边界的位置。

    【讨论】:

    • 是的!谢谢!我想知道它是否有类似的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多