simle textButton libgdx

时间:2015-07-08 10:44:00

标签: android libgdx

我是libgdx的新手。试图实现简单的按钮,但屏幕上没有任何内容。

 font = new BitmapFont();
        skinButton=new Skin();
        buttonAtlas = new TextureAtlas("button_back/button.txt");
        skinButton.addRegions(buttonAtlas);
        textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = font;

        textButtonStyle.up = skinButton.getDrawable("rounded_rectangle_button");
        textButtonStyle.down = skinButton.getDrawable("rounded_rectangle_button");
        textButtonStyle.checked = skinButton.getDrawable("rounded_rectangle_button");
        button=new TextButton("Finish",textButtonStyle);
        button.setText("Finish");
        button.setHeight(230);
        button.setWidth(500);
        button.setPosition(50,50);
        stage.addActor(button);

button.txt

button.png
format: RGBA8888
filter: Linear,Linear
repeat: none
rounded_rectangle_button
  rotate: false
  xy: 2, 2
  size: 256, 108
  orig: 256, 128
  offset: 0, 10
  index: -1

我将button.png文件放在与button.txt相同的文件夹中

1 个答案:

答案 0 :(得分:1)

看起来您的代码很好。我将其复制+粘贴到一个准系统应用程序中,并在我的button.txt文件中更改了图像的名称,但是否则它有效。正如Tenfour04所说,您是否正确设置了相机/视口(或渲染代码)等?

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;

public class SimpleTextButton implements ApplicationListener {
   private SpriteBatch batch;
   private OrthographicCamera camera;
   private Stage stage;
   private BitmapFont font;
   private Skin skinButton;
   private TextureAtlas buttonAtlas;
   private TextButton.TextButtonStyle textButtonStyle;
   private TextButton button;

   @Override
   public void create() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();
      stage = new Stage();

      font = new BitmapFont();
      skinButton=new Skin();
      buttonAtlas = new TextureAtlas("img/sprites/buttons/button.txt");
      skinButton.addRegions(buttonAtlas);
      textButtonStyle = new TextButton.TextButtonStyle();
      textButtonStyle.font = font;

      textButtonStyle.up = skinButton.getDrawable("rounded_rectangle_button");
      textButtonStyle.down = skinButton.getDrawable("rounded_rectangle_button");
      textButtonStyle.checked = skinButton.getDrawable("rounded_rectangle_button");
      button=new TextButton("Finish",textButtonStyle);
      button.setText("Finish");
      button.setHeight(230);
      button.setWidth(500);
      button.setPosition(50,50);
      stage.addActor(button);         
   }

   @Override
   public void render() {
       stage.draw();
   }

   @Override
   public void dispose() {
       buttonAtlas.dispose();
       font.dispose();
       skinButton.dispose();
       buttonAtlas.dispose();
       batch.dispose();
   }

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

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

}