Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a button in Libgdx?

I want to create a button that changes when the user hovers it, or clicking it. I created the following variable

Button buttonPlay = new Button(); 

I don't know what to do now, how to load the images? How to write text into the button? How to implement the events / effects (hover, click)?

It would be very helpful if someone could write some example code for a button.

like image 664
julian Avatar asked Jan 31 '14 19:01

julian


People also ask

What is scene 2D?

Essentially a scene graph is a data structure for storing the stuff in your world. So if your game world is composed of dozens or hundreds of sprites, those sprites are stored in the scene graph. In addition to holding the contents of your world, Scene2D provides a number of functions that it performs on that data.

What is stage Libgdx?

A 2D scene graph containing hierarchies of actors . Stage handles the viewport and distributes input events. setViewport(Viewport) controls the coordinates used within the stage and sets up the camera used to convert between stage coordinates and screen coordinates.

How to describe a bucket and raindrop in libGDX?

So, to describe both the bucket and raindrops we need to store their position and size. libGDX provides a Rectangle class which we can use for this purpose. Let’s start by creating a Rectangle that represents our bucket. We add a new field: In the create () method we instantiate the Rectangle and specify its initial values.

What is array class in libGDX?

The Array class tries to minimize garbage as much as possible. libGDX offers other garbage collector aware collections such as hash-maps or sets as well. We’ll store the time in nanoseconds, that’s why we use a long.

What file formats are supported by libGDX?

Note: libGDX supports MP3, OGG and WAV files. Which format you should use, depends on your specific needs, as each format has its own advantages and disadvantages. For example, WAV files are quite large compared to other formats, OGG files don’t work on RoboVM (iOS) nor with Safari (GWT), and MP3 files have issues with seemless looping.

How do sound effects work in libGDX?

Next we load the sound effect and the background music. libGDX differentiates between sound effects, which are stored in memory, and music, which is streamed from wherever it is stored. Music is usually too big to be kept in memory completely, hence the differentiation.


1 Answers

A button is simply an actor in libgdx. To render an actor you use a stage that contains all the actors of the screen, renders them and updates them. I assume you want a button with text, so you should use the class TextButton and add it to a stage. A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to 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? I am creating a stage, a font and a textureatlas with all the textures for the buttons in "buttons.pack". Then I initialize an empty TextButtonStyle and and i add the font and the textures for the up, down and checked states. font, up, down and checked are all static variables of type Drawable so you can really pass it any kind of Drawable (texture, 9-patch etc). Then simply add the button to the Stage.

Now in order to do something when the button is actually clicked, you have to add a listener to the button, a ChangeListener.

    button.addListener(new ChangeListener() {         @Override         public void changed (ChangeEvent event, Actor actor) {             System.out.println("Button Pressed");         }     }); 

Of course instead of adding the button directly to the Stage you should add it to a Table and add the Table to the Stage but I didn't want to make this post too confusing. Here is a good tutorial on tables in libgdx.

like image 163
danielz Avatar answered Sep 22 '22 12:09

danielz