简体   繁体   中英

How to change the value of a Slider programmatically? (LibGDX)

I think I am missing something obvious, but I can't seem to change the value of a Slider (Scene2D.ui) programmatically. The reason that I want to do this is because I have a character that you can customize with the sliders but I want to have some preset characters that you can use as a starting point for customization.

What I have so far is something like this:

(ChangeListener)(slider.getListeners().get(0)) but I cannot call the changed(ChangeEvent event, Actor actor) method that is called when the slider is changed.

You can use setValue(float value) method of Slider to change value of slider programmatically.

public class GdxTest extends ApplicationAdapter {

    Stage stage;
    Slider slider;

    @Override
    public void create() {

        stage=new Stage();
        Gdx.input.setInputProcessor(stage);
        Skin skin=new Skin(Gdx.files.internal("skin/glassy-ui.json"));

        Table table=new Table();
        table.setFillParent(true);

        stage.addActor(table);
        table.add(slider=new Slider(0,100,1,false,skin));

        slider.setValue(50);   // value changed by 50%
    }

    ...
}

slider.setAnimateDuration(0.3f);

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