如何在MainScreen菜单中添加背景图像(此处)?

时间:2014-10-03 08:29:47

标签: android eclipse background libgdx

如何创建和绘制图像以设置我资产的背景?

public class MainScreen extends AbstractScreen {

    private SpriteBatch batch;
    private Button exit, play;

    public MainScreen(Main main) {
        super(main);

    }

    public void show(){
        batch= main.getBatch();
        Texture texture = new Texture(Gdx.files.internal("BotonExit.png"));
        int centroY = Gdx.graphics.getHeight() / 2 - texture.getHeight() / 2;
        int centroX = Gdx.graphics.getWidth() / 2 - texture.getWidth() / 2;
        exit = new ButtonExit(centroX, centroY - 50);
        play = new ButtonPlay(centroX, centroY + 50);


    }

    public void render(float delta){
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       
        exit.update(); 
        play.update();
        batch.begin();
        exit.draw(batch); 
        play.draw(batch); 
        batch.end();
    }
}

1 个答案:

答案 0 :(得分:0)

你非常接近绘制背景,你应该设置相机来填充所有背景。这可能会对您有所帮助:

private SpriteBatch batch;
private Button exit, play;
private final OrthographicCamera camera;

public MainScreen(Main main) {
    super(main);
}

public void show(){

   batch= main.getBatch();

   Texture background = new Texture(Gdx.files.internal("background.png"));
   background.setSize(800,480);

   Texture texture = new Texture(Gdx.files.internal("BotonExit.png"));
   int centroY = Gdx.graphics.getHeight() / 2 - texture.getHeight() / 2;
   int centroX = Gdx.graphics.getWidth() / 2 - texture.getWidth() / 2;
   exit = new ButtonExit(centroX, centroY - 50);
   play = new ButtonPlay(centroX, centroY + 50);

   camera = new OrthographicCamera();
   camera.setToOrtho(false, 800, 480);


}

public void render(float delta){
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       
    exit.update(); 
    play.update();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    background.draw(batch);
    exit.draw(batch); 
    play.draw(batch); 
    batch.end();
}