Search code examples
javaandroidlibgdx

Button not responding after a few clicks lib-gdx


I cant seem to find an answer to this. Why does my start button freeze after a certain amount of clicks(3)? It works for the first 3 clicks and then decides to stop working. I can still see that the screen is responding(the button becomes red when clicked), but the text on it doesn't change. It's like the click listener stopped responding.

public class MainMenu implements Screen{

    private Game game;
    private Stage stage;

    private TextButton Start_btn;
    private TextButton LocalWifi_btn;
    private TextButton Internet_btn;

    private TextButton Settings_btn;


    private boolean Start_clicked = false;
    private boolean LocalWifi_clicked = false;
    private boolean Internet_clicked = false;
    private boolean Settings_clicked = false;


    public MainMenu(Game g){
        game = g; //The Wasteland

        stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight())); //create a new stage with viewport to draw 2d stuff on

        Gdx.input.setInputProcessor(stage); //all input set to stage


        Skin skin = new Skin(Gdx.files.internal("gui/uiskin.json"), new TextureAtlas(Gdx.files.internal("gui/uiskin.atlas"))); //need this before you can make a gui

        Start_btn = new TextButton("" + Start_clicked, skin);

        Start_btn.setPosition(500, 500);
        Start_btn.setSize(200, 200);
        Start_btn.getLabel().setFontScale(10, 10); //change text size

        Start_btn.addListener(new ClickListener(){
            @Override
            public void touchUp(InputEvent e, float x, float y, int point, int button){
                onStartClicked();
            }
        });
        stage.addActor(Start_btn);

    }

    private void onStartClicked(){

        if(!Start_clicked){
            Start_clicked = true;
            Start_btn.setText("" + Start_clicked);
            Gdx.app.log("Button", "" + Start_clicked);

        }
        else{
            Start_clicked = false;
            Start_btn.setText("" + Start_clicked);
            Gdx.app.log("Button", "" + Start_clicked);

        }

    }

    @Override
    public void render(float delta) {

        //this has to be before anything or else it will be drawn on top of everything else
        Gdx.gl.glClearColor(0, 0, 0, 1); //set background color
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clears the screen

        stage.act(delta); //send ammount of time since last render call, tells it to keep a steady fps

        stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        // use true here to center the camera
        // that's what you probably want in case of a UI
        stage.getViewport().update(width, height, true);


    }

}

What am I doing wrong?!?!?!


Solution

  • Don't override touchUp() without calling super.touchUp(), because its messing up the functionality of ClickListener. But that's not what you want to override anyway. You should override clicked() so it only triggers if you release the click while still over the button. Or better yet, use a ChangeListener. Buttons already have a ClickListener built in that fires a change event when the button is clicked. Adding another ClickListener is redundant when you can just use a ChangeListener.