我正在制作2D Android游戏。问题是我的平铺游戏地图没有渲染,虽然我的GUI类很好。看起来永远不会调用renderer.render()
。
这是我手机的屏幕截图。
Main.class:
public class Main extends ApplicationAdapter implements Screen {
private ZombieGame game;
private OrthographicCamera camera;
private Viewport gameView;
private GUI gui;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
public Main(ZombieGame game){
this.game = game;
camera = new OrthographicCamera();
gameView = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
gui = new GUI(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera.position.set(gameView.getWorldWidth() / 2, gameView.getWorldHeight() / 2, 0);
}
@Override
public void show(){
}
@Override
public void hide(){
}
@Override
public void create () {
}
public void keyEvent(float dt){
if(Gdx.input.isTouched()){
camera.position.x -= 100 * dt;
System.out.println("COORD: " + camera.position.x);
}
}
public void update(float dt){
keyEvent(dt);
camera.update();
renderer.setView(camera);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
game.batch.setProjectionMatrix(gui.stage.getCamera().combined);
gui.stage.draw();
}
@Override
public void dispose() {
}
}
ZombieGame.class:
public class ZombieGame extends Game{
public SpriteBatch batch;
@Override
public void create(){
batch = new SpriteBatch();
setScreen(new Main(this));
}
@Override
public void render(){
super.render();
}
}
答案 0 :(得分:0)
我认为你应该在create()中使用 setToOrtho()方法,
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
而不是FitViewport,
gameView = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
camera.position.set(gameView.getWorldWidth() / 2, gameView.getWorldHeight() / 2, 0);
试试看,让我知道。感谢...