0

I have created a button and I want to change its appearance on hover and on click. I get no errors, but it isn't working. It doesn't change the image when it is clicked or when it is hovered. The only image that is displayed is the one from playButtonStyle.up.

Here is my code:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.starships.MainClass;
import com.sun.prism.paint.Color;
import helpers.Info;

public class MainMenuScreen implements Screen {

    MainClass game;
    Stage stage;    
    private Texture background;    
    private AssetManager assets;
    private TextureAtlas atlas;   
    private Skin skin;

    private Table table;
    private Button playButton;

    public MainMenuScreen(MainClass mainClass) {
        game = mainClass;

        Gdx.input.setInputProcessor(stage); 
        stage = new Stage();
        background = new Texture(Gdx.files.internal("Background.png"));

        assets = new AssetManager();
        assets.load("Buttons/PlayButtonAtlas.atlas", TextureAtlas.class);
        assets.finishLoading();
        atlas = assets.get("Buttons/PlayButtonAtlas.atlas");

        skin = new Skin(atlas);

        table = new Table(skin);
        table.setBounds(0, 0, Info.WIDTH, Info.HEIGHT);

        Button.ButtonStyle playButtonStyle = new Button.ButtonStyle();
        playButtonStyle.up = skin.getDrawable("PlayButton");
        playButtonStyle.over = skin.getDrawable("PlayButtonHover");
        playButtonStyle.down = skin.getDrawable("PlayButtonPressed");

        playButton = new Button(playButtonStyle);   
        table.add(playButton);
        stage.addActor(table);
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0.7f, 0.8f, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
iulyus01
  • 37
  • 8
  • Possible duplicate of [Button ClickLIstener is not Working in LibGdx game?](https://stackoverflow.com/questions/11373390/button-clicklistener-is-not-working-in-libgdx-game) – Cedric Martens Jun 17 '17 at 22:17

1 Answers1

3

Set InputProcessor after initialisation of Stage like this :

public MainMenuScreen(MainClass mainClass) {
   game = mainClass;
   stage = new Stage();
   Gdx.input.setInputProcessor(stage);   // This call should be after initialisation of stage.
   background = new Texture(Gdx.files.internal("Background.png"));
   ...
   ...
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • 1
    Thanks. It worked. I spend the entire day trying to make a button.. :\ – iulyus01 May 23 '17 at 17:42
  • 2
    @iulyus01 If you want multiple input processors you can use `InputMultiplexer`. Put the `InputProcessors` (stage is an `InputProcessor` as well) with higher priorities in first and then add the `InputMultiplexer` instead of just the stage like shown in this answer. – Madmenyo May 24 '17 at 06:42