By Alvin Alexander. Last updated: December 28, 2017
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.