如何在libGDX中创建一个带有图像的button?

我是LibGDX的新手。 我正在尝试开发Android游戏(所以它会支持不同的屏幕尺寸),但我不知道如何把一个图像button。 我尝试ImageButton,但我无法调整它。 我已经把TextButtons放在我的应用程序中,它的工作原理也很好。

有什么建议么 ?

你有自由创建你自己的button类,附加一个精灵,做一些方法来处理input和你的好去。

public class SimpleButton { private Sprite skin; public SimpleButton(Texture texture, float x, float y, float width, float height) { skin = new Sprite(texture); // your image skin.setPosition(x, y); skin.setSize(width, height); } public void update (SpriteBatch batch, float input_x, float input_y) { checkIfClicked(input_x, input_y); skin.draw(batch); // draw the button } private void checkIfClicked (float ix, float iy) { if (ix > skin.getX() && ix < skin.getX() + skin.getWidth()) { if (iy > skin.getY() && iy < skin.getY() + skin.getHeight()) { // the button was clicked, perform an action System.out.println("Button clicked !"); } } } } 

对于屏幕尺寸问题,您可以使用屏幕尺寸的百分比:

 public float convertToPercents_width (float p) { return Gdx.graphics.getWidth()*p/100; } public float convertToPercents_height (float p) { return Gdx.graphics.getHeight()*p/100; } 

按照步骤:

创建一个图集:

 atlasUiPadrao = new TextureAtlas("ui/uiPadrao.pack"); 

(如何打包:把PNG图像放在一个文件夹中,并用libgdx纹理压缩包)。

创建一个皮肤:

 skinPadrao = new Skin(atlasUiPadrao); 

创建一个样式:ImageButtonStyle:

 fireButtonStyle = new ImageButtonStyle(); //Instaciate fireButtonStyle.up = skinPadrao.getDrawable("uFireUpI"); //Set image for not pressed button fireButtonStyle.down = skinPadrao.getDrawable("uFireDownI"); //Set image for pressed fireButtonStyle.over = skinPadrao.getDrawable("uFireOverI"); //set image for mouse over fireButtonStyle.pressedOffsetX = 1; fireButtonStyle.pressedOffsetY = -1; 

现在你可以在屏幕文件中创建button

 ImageButton item1Button = new ImageButton(Assets.fireButtonStyle); 

对于每个尺寸的屏幕,您必须定义要显示的图像

我使用scene2d库在button,文本,图像等屏幕上制作UI元素

最终结果如下所示: 在这里输入图像描述

  • 创建一个扩展Game类的类开始,并调用setScreen函数将屏幕设置为主菜单
  • MainMenu类实现screen.It有舞台,包含也有开/关状态的button,input监听器被添加到button来处理触摸input

你可以从这里findbutton.pack和new.fnt文件

开始上课

 public class Start extends Game{ public OrthographicCamera camera; public SpriteBatch batch; public BitmapFont font; @Override public void create() { camera=new OrthographicCamera(Constants.width,Constants.width*Constants.aspectRatio); batch=new SpriteBatch(); setScreen(new MainMenu(this)); } } 

MainMenu类

 public class MainMenu implements Screen{ Start game; private Stage stage; //** stage holds the Button **// private BitmapFont font; private TextureAtlas buttonsAtlas; //** image of buttons **// private Skin buttonSkin; //** images are used as skins of the button **// private TextButton button; public MainMenu(Start game) { this.game=game; } @Override public void show() { buttonsAtlas = new TextureAtlas("img/button/button.pack"); //**button atlas image **// buttonSkin = new Skin(); buttonSkin.addRegions(buttonsAtlas); //** skins for on and off **// font = new BitmapFont(Gdx.files.internal("font/new.fnt"),false); //** font **// stage = new Stage(); //** window is stage **// stage.clear(); Gdx.input.setInputProcessor(stage); //** stage is responsive **// TextButtonStyle style = new TextButtonStyle(); //** Button properties **// style.up = buttonSkin.getDrawable("buttonOff"); style.down = buttonSkin.getDrawable("buttonOn"); style.font = font; button = new TextButton("START", style); //** Button text and style **// button.setHeight(Gdx.graphics.getHeight()/3); //** Button Height **// button.setWidth(Gdx.graphics.getWidth()/4); //** Button Width **// button.setPosition(Gdx.graphics.getWidth()/2-button.getWidth()/2, Gdx.graphics.getHeight()); button.addListener(new InputListener() { public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { Gdx.app.log("my app", "Pressed"); //** Usually used to start Game, etc. **// // TODO Auto-generated method stub return true; } public void touchUp (InputEvent event, float x, float y, int pointer, int button) { Gdx.app.log("my app", "Rggggggeleased"); ///and level game.setScreen(new MyNextScreen(game)); dispose(); } }); MoveToAction moveAction = new MoveToAction();//Add dynamic movement effects to button moveAction.setPosition(Gdx.graphics.getWidth()/2-button.getWidth()/2, Gdx.graphics.getHeight()/2+ Gdx.graphics.getHeight()/6); moveAction.setDuration(.5f); button.addAction(moveAction); stage.addActor(button); } @Override public void render(float delta) { // TODO Auto-generated method stub Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); game.batch.begin(); stage.draw(); game.batch.end(); } @Override public void resize(int width, int height) { // TODO Auto-generated method stub } @Override public void pause() { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } @Override public void hide() { // TODO Auto-generated method stub } @Override public void dispose() { // TODO Auto-generated method stub } } 

我已经创建了一个介绍教程来在LibGDX上创建Button,ImageButton和TextButton: https ://libgdx.info/buttons-scene2d/

如果您有任何问题,请让我知道。