Android 如何在主屏幕菜单(此处)中添加背景图像?
如何从我的资源中创建、绘制和设置背景图像Android 如何在主屏幕菜单(此处)中添加背景图像?,android,eclipse,background,libgdx,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
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();
}
}
绘制背景非常接近,您应该将摄影机设置为填充所有背景。这可能会帮助您:
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();
}
谢谢,但仍然不工作,当我把私人决赛,可以使用主屏幕,我不能使用方法设置大小,我不能画背景我不知道为什么,可能是因为我有另一个背景在另一个类或我不知道。有什么想法吗?尝试实现Screen而不是扩展AbstractScreen它将覆盖渲染方法