ImageButtons在按下后不会更改图像-LibGDX

时间:2019-01-11 18:24:14

标签: java android user-interface libgdx

我目前正在libgdx的菜单屏幕上工作。我添加了 ImageButton 到屏幕上,但是当我要启动应用程序并单击按钮时,图像不会从按钮上移图像变为按钮下移图像。此外,添加侦听器(向按钮添加动作)不会产生任何效果。我想这与 addActor()函数无法正常工作有关吗?

public class Test extends UIScreen {
 public Test(final MainExecute test) {
        super();
        this.test = test;
    }

    @Override
    public void show() {
        stage = new Stage();
        batch = new SpriteBatch();
        table = new Table();
        txture = new createTexture();
        atlas = new TextureAtlas("atlases/main/unnamed.atlas");

        table.left();
        table.top();
        table.setFillParent(true);
        table.setDebug(true);
        background = new Texture(Gdx.files.internal("ui elements and skins/Settings screen/Background.png"));
        Texture up = new Texture(Gdx.files.internal("ui elements and skins/Settings screen/Settings.png"));
        Drawable upDraw = new TextureRegionDrawable(new TextureRegion(up));
        Texture down = new Texture(Gdx.files.internal("ui elements and skins/Settings screen/Settings Pressed.png"));
        Drawable downDraw = new TextureRegionDrawable(new TextureRegion(down));


        ImageButton settings = new ImageButton(upDraw, downDraw);
        table.add(settings).pad(30).expand();
        this.stage.addActor(table);

        settings.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                Gdx.app.exit();
            }
        });

    }

    @Override
    public void render(float delta) {
        batch.begin();
        batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
        this.stage.act(delta);
        this.stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        this.viewport.update(width, height);
        this.camera.position.set(this.camera.viewportWidth / 2, this.camera.viewportHeight / 2, 0);
        this.camera.update();
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}
  

UIScreen

   public UIScreen() {
        //Initialising camera and adding viewport
        camera = new OrthographicCamera();
        viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
        viewport.apply();
        //Camera position, watching for full screen
        camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
        camera.update();
        //Adding the viewport to the stage
        stage = new Stage(new ScreenViewport(new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight())));
        stage.setDebugAll(true);
        Gdx.input.setInputProcessor(stage);

    }

1 个答案:

答案 0 :(得分:1)

首先,您要在show()中创建一个新的Stage实例,以覆盖您在UIScreen中创建的实例。因此,该阶段未设置为InputProcessor。

只需删除show方法中的第一行stage = new Stage();,它就可以正常工作。

编辑:同样,您必须使用super.stage而不是this.stage来访问阶段,并确保它对Test可见。