How to create a LibGDX ImageButton

I just started working with LibGDX, so I don’t know if there’s a better way to create a LibGDX Scene2d ImageButton, but I can confirm that this approach works:

Texture hikeTexture = new Texture(Gdx.files.internal("hike_btn.jpg"));
Texture hikeTexturePressed = new Texture(Gdx.files.internal("hike_btn_pressed.jpg"));
hikeButton = new ImageButton(
    new TextureRegionDrawable(new TextureRegion(hikeTexture)),
    new TextureRegionDrawable(new TextureRegion(hikeTexturePressed))
);
hikeButton.setPosition(60, 300);  //hikeButton is an ImageButton
stage.addActor(hikeButton);

I currently use this code in the show() method of a class that implements Screen, and it works as desired.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.