0

I want to make a method(addButton) that would all what's now done by the constructor, but taking some variables. Now i'm stuck cause there is an error which says that I need to make the boolean final, but I don't want to do that. How should I go about this?

Here is the code:

    public void addButton(Table table,String key,boolean bool){

        atlas=new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
        skin=new Skin(atlas);
        buttonStyle.up=skin.getDrawable(key+".up");
        buttonStyle.down=skin.getDrawable(key+".down");

        button=new Button(buttonStyle);
        button.addListener(new ClickListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                bool=true; //this boolean
                return super.touchDown(event, x, y, pointer, button);
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                bool=false; //and this
                super.touchUp(event, x, y, pointer, button);
            }
        });
        table1.add(button);

    }
  • What's the problem with making it `final`? Many people do that even without being "forced" to do so. Furthermore, please post only the relevant code... – noone May 27 '15 at 17:46
  • Making it final will make it unchangable, or am I wrong? –  May 27 '15 at 17:58
  • The way you are doing it doesn't work anyway, you are not changing anything that way. You should read about how paramaters in Java work. – noone May 27 '15 at 18:23
  • Could you suggest something that would do the job? –  May 27 '15 at 18:28
  • You could wrap that boolean in another class, make that final and then change the boolean field of it. – noone May 27 '15 at 18:39
  • I don't understand what you are saying. Could you provide some code? –  May 28 '15 at 13:17

1 Answers1

1

There are many ways to do it. If you just want to know if the button is pressed or not, you can use Button.isPressed(), because Button already keeps track of that.

If you want to do something else, you would be better off creating your own MyButton which extends Button. MyButton could have a field private boolean bool and add that ClickListener in the constructor. This click listener will then be able to access the field of the button via MyButton.this.bool and can change it.

public class MyButton extends Button {

private boolean bool;

public MyButton(...) {

    addListener(new ClickListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            MyButton.this.bool=true;
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            MyButton.this.bool=false;
            super.touchUp(event, x, y, pointer, button);
        }
    });
}
}

Another solution would be to keep your current setup, but wrap the primitive value in another class:

public class DataWrapper {
    public boolean bool;
}

public void addButton(Table table,String key, final DataWrapper data) {
    ...

    button=new Button(buttonStyle);
    button.addListener(new ClickListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            data.bool=true;
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            data.bool=false;
            super.touchUp(event, x, y, pointer, button);
        }
    });
    table1.add(button);
}
noone
  • 19,520
  • 5
  • 61
  • 76
  • But will this work, cause I want that method so I can easily create new buttons? –  May 28 '15 at 13:56
  • I do not understand your question. – noone May 28 '15 at 13:58
  • Ok I found another solution :) Instead of the boolean I passed in a Button and from another class just tested buttonName.isPressed(); –  May 29 '15 at 05:52