2014-10-03 86 views
0

我如何创建和绘制图像以从我的资产设置背景?如何在MainScreen菜单(此处)添加背景图像?

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(); 
    } 
} 

回答

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(); 
} 
+0

谢谢,但仍然没有工作,当我把私人最终,可以使用MainScreen,和我不能使用的方法setSize和我不能得出我不后台知道为什么,也许因为我在另一堂课有另一个背景,或者我不知道。有任何想法吗? – JAnoob92 2014-10-03 10:06:00

+0

尝试实现屏幕不扩展AbstractScreen将重写呈现方法 – adnbsr 2014-10-03 14:53:15