无法在我的libgdx Actor中使事件起作用

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);

我的演员(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());
    }            
}

看起来事件没有触发。奇怪的是,我已经使用了内置的UI小部件(例如TextButton),可以很好地触发这些事件。有人能看出我做错了什么吗?

1个回答

16

你还应该将 setBounds 应用于你的 actor。 如果你想要与纹理相同的大小,最好的方法是在构造函数中添加以下代码:

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

请注意,你也可以使用前两个参数设置边界的位置。


是的!谢谢你!我在想它是否有类似的东西。 - Micah Carrick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接