Dani007
Dani007

Reputation: 151

Creating a simple button in libGDX

I'm new to libgdx, and I've been struggling to make a simple button for hours! First I tried generating it this way(That I found on this website):

public class TextButtonGenerator {

    public TextButton createButton(String label){
        BitmapFont font = new BitmapFont();
        TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal("sprite.txt"));
        Skin skin = new Skin(buttonAtlas);
//        skin.addRegions(buttonAtlas);
        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.getDrawable("button");
        textButtonStyle.down = skin.getDrawable("button");
        textButtonStyle.checked = skin.getDrawable("button");
        return new TextButton(label, textButtonStyle);

I used Textpacker to create sprites.txt in my asset folder, however, the program still doesn't run, and crashes. This is the error message I'm getting:

E/AndroidRuntime: FATAL EXCEPTION: GLThread 244475
    Process: com.mygdx.game, PID: 21525
    com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: sprite.txt (Internal)E/AndroidRuntime: FATAL EXCEPTION: GLThread 244475
    Process: com.mygdx.game, PID: 21525
    com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: sprite.txt (Internal)

It says the root of the problem in my code is where I construct TextureAtlas. I think the problem might be that it should be .pack file not .txt? But in this tutorial they're using txt and it works fine:

https://www.codeandweb.com/texturepacker/tutorials/libgdx-physics

I also found a different way of generating the button that did not require the pack:

        Skin skin = new Skin();
        Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();
        skin.add("white", new Texture(pixmap));
        skin.add("default", new BitmapFont());

        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.up = skin.newDrawable("white", new Color(0, 0, 0, 1));
        textButtonStyle.font = skin.getFont("default");
        skin.add("default", textButtonStyle);
        return new TextButton(label, skin);

Although this one didn't crash but there's no button! When I run it, I only get my background color on the screen. Quite frankly I don't even understand half the code I posted above, I'm trying to learn the engine on my but it just feels like I've barely made any progress in 2 days. Any help will be appreciated. Also I am adding the button to my stage and calling stage.draw() in my render method in the appropriate screen.

Upvotes: 0

Views: 379

Answers (1)

Rabi Mirjekar
Rabi Mirjekar

Reputation: 1

Create Button using Texture

we can use Texture as button.I have a simple trick and it uses a new class that is GButton (GameButton).To make a button simmply create a new class and name as GButton and extends by the Texture class to inherit Texture properties.Create a constructor and pass argument as FileHandle from the libgdx library.In the constructor body write super(FileHandle). create a method as boolean that is isClicked(). return Gdx.input.isTouched();

here is a simple code.

public class GButton extends Texture
{
    GButton(FileHandle s){
        super(s);
    }
    boolean isClicked(){
        return Gdx.input.isTouched();
    }
}

implementation of our new button

public class MyGdxGame implements ApplicationListener
{

    GButton b;

    @Override
    public void create()
    {
        b = new GButton(Gdx.files.internal("leftB.png"));
    }

    @Override
    public void render()
    {      
    
        Gdx.gl.glClearColor(1, 1, 1, 1); 
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
        batch.begin();
        batch.draw(b,0,0,b.getWidth(),b.getHeight());
        batch.end();
        
        if(b.isClicked()){
            //perform action when button is clicked
        }
}

@Override
public void dispose()
{
}

@Override
public void resize(int width, int height)
{
    
}

@Override
public void pause()
{
}

@Override
public void resume()
{
}


}

Upvotes: 0

Related Questions