Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

    rainMusic.setLooping(true);
    rainMusic.play();

    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.setX(300);
    leftButton.setY(300);
    leftButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log(LOG, "down");
                System.out.println("down");
                return true;
        }

        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log(LOG, "up");
                System.out.println("up");
        }
    });
    leftButton.setWidth(ibs.up.getMinWidth());
    leftButton.setHeight(ibs.up.getMinHeight());
    leftButton.setBounds(0, 0, ibs.up.getMinWidth(), ibs.up.getMinHeight());
    leftButton.setTouchable(Touchable.enabled);

    uistage.addActor(leftButton);
            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?

share|improve this question
add comment

1 Answer

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

Gdx.input.setInputProcessor(uistage);

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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