So I'm trying to test an ImageButton on an Android device. I can get the button to appear on the screen, but when I press it nothing happens or appears on the log. Here is what I have in the Show() method of a class that implements the Screen class.

public void show() {
    uistage = new Stage();
    Texture tankSprite = new Texture(Gdx.files.internal("TankSpriteBase.png"));     
    tank = new Tank(0, true, tankSprite);
    tank.rect.x = 100;
    tank.rect.y = 100;
    tank.rect.width = 44;
    tank.rect.height = 45;

    dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
    rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));


    float w = 800;
    float h = 400;

    camera = new OrthographicCamera();
    camera.setToOrtho(false, w, h);
    batch = new SpriteBatch();
ImageButton.ImageButtonStyle ibs = new ImageButton.ImageButtonStyle();
    ibs.up = new TextureRegionDrawable(
                new TextureRegion(
                        new Texture(Gdx.files.internal("direction_left.png"))));
    ibs.down = new TextureRegionDrawable(
                new TextureRegion(
                        new Texture(Gdx.files.internal("direction_left_pressed.png"))));

    leftButton = new ImageButton(ibs);
    leftButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log(LOG, "down");
                return true;

        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log(LOG, "up");
    leftButton.setBounds(0, 0, ibs.up.getMinWidth(), ibs.up.getMinHeight());

            Gdx.app.log(LOG, "Width is "+Gdx.graphics.getWidth()+" and height is "+Gdx.graphics.getHeight());

    background = new Texture(Gdx.files.internal("background.png"));
    background.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion region = new TextureRegion(background, 0, 0, 512, 301);

    sprite = new Sprite(region);
    sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
    sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
    sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);

Might anyone know why this doesn't appear to be working?


Oh wow, completely bungled this. I forgot to add:


Why this is important is detailed here. Remember to check your code and read your docs, kids! (Eat your veggies too)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.