简体   繁体   中英

LibGDX variable accessed from within inner class

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

    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM