AddListener到LibGdx Scene2d中actor的特定位置

时间:2017-12-31 13:02:00

标签: libgdx scene2d

我有这个演员 HintsWindow ,其中包含3个TextureRegions(背景,取消,继续)。

public class HintsWindow extends Actor {

    TextureRegion bg;
    TextureRegion cancel;
    TextureRegion proceed;

    public HintsWindow() {
        bg = new TextureRegion(Gdx.files.internal("background.png"));
        cancel = new TextureRegion(Gdx.files.internal("cancel.png"));
        proceed = new TextureRegion(Gdx.files.internal("proceed.png"));
     }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(bg, 171.031f, 484.090f);
        batch.draw(cancel, 234.837f, 542.926f);
        batch.draw(proceed, 407.900f, 542.926f);
    }
}

绘制TextureRegions工作正常,但这是我的问题:

是否有可能将addListener添加到整个actor但是取消并继续,以便它们像按钮一样?

类似的东西:

addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        // if proceed pressed --> do something
        // else if cancel pressed --> addAction(Actions.fadeOut(1.0f));
    }
});

1 个答案:

答案 0 :(得分:1)

您可以让HintsWindow包含Actors而不是TextureRegions。 将它们更改为图像。

    public class HintsWindow extends Actor {

    Image bg;
    Image cancel;
    Image proceed;

    public HintsWindow() {
        bg = new Image(new TextureRegion(Gdx.files.internal("background.png")));
        cancel = new Image(new TextureRegion(Gdx.files.internal("cancel.png")));
        proceed = new Image(new TextureRegion(Gdx.files.internal("proceed.png")));

        // here you can add listener
        cancel.addListener(new ClickListener() {
           @Override
           public void clicked(InputEvent event, float x, float y) {
             // if proceed pressed --> do something
             // else if cancel pressed --> addAction(Actions.fadeOut(1.0f));
    }
});
     }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        color.set(color.r, color.g, color.b, color.a * parentAlpha);
        bg.setColor(color);
        cancel.setColor(color);
        proceed.setColor(color);
    }
}