java - How to create a button in Libgdx? -
i want create button changes when user hovers it, or clicking it. created following variable
button buttonplay = new button();
i don't know now, how load images? how write text button? how implement events / effects (hover, click)?
it helpful if write example code button.
a button actor in libgdx. render actor use stage contains actors of screen, renders them , updates them. assume want button text, should use class textbutton , add stage. textbutton takes string render , buttonstyle, in case textbuttonstyle, class contains information button (font, drawable render while not pressed, drawable render while pressed etc).
public class buttonexample extends game{ stage stage; textbutton button; textbuttonstyle textbuttonstyle; bitmapfont font; skin skin; textureatlas buttonatlas; @override public void create() { stage = new stage(); gdx.input.setinputprocessor(stage); font = new bitmapfont(); skin = new skin(); buttonatlas = new textureatlas(gdx.files.internal("buttons/buttons.pack")); skin.addregions(buttonatlas); textbuttonstyle = new textbuttonstyle(); textbuttonstyle.font = font; textbuttonstyle.up = skin.getdrawable("up-button"); textbuttonstyle.down = skin.getdrawable("down-button"); textbuttonstyle.checked = skin.getdrawable("checked-button"); button = new textbutton("button1", textbuttonstyle); stage.addactor(button); } @override public void render() { super.render(); stage.draw(); } }
so whats happening here? creating stage, font , textureatlas textures buttons in "buttons.pack". initialize empty textbuttonstyle , , add font , textures up, down , checked states. font, up, down , checked static variables of type drawable can pass kind of drawable (texture, 9-patch etc). add button stage.
now in order when button clicked, have add listener button, changelistener.
button.addlistener(new changelistener() { @override public void changed (changeevent event, actor actor) { system.out.println("button pressed"); } });
of course instead of adding button directly stage should add table , add table stage didn't want make post confusing. here tutorial on tables in libgdx.
Comments
Post a Comment