【发布时间】:2018-08-27 10:51:50
【问题描述】:
我开始使用 libGDX 和 Tiled 作为地图创建者创建 2D 游戏。我正在使用一些精灵作为 Tiled 中的图像集合。
问题是每当我向右移动并且某些精灵的左下角超出视口时,它就会像这样消失:
左边应该是一堵墙,一部分天花板和一部分地板,但当我向右移动时它消失了。
这是我的代码:
public class Main implements ApplicationListener {
private static final int VIEWPORT_WIDTH = 800;
private static final int VIEWPORT_HEIGHT = 480;
private TiledMap tiledMap;
private TiledMapRenderer tiledMapRenderer;
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture playerImage;
private Rectangle playerRect;
@Override
public void create() {
this.camera = new OrthographicCamera();
this.camera.setToOrtho(false, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
this.camera.update();
this.tiledMap = new TmxMapLoader().load("levels/demo_4x.tmx");
this.tiledMapRenderer = new OrthogonalTiledMapRenderer(this.tiledMap);
this.batch = new SpriteBatch();
this.font = new BitmapFont();
this.playerImage = new Texture(Gdx.files.internal("person-demo.gif"));
this.playerRect = new Rectangle();
this.playerRect.x = 276;
this.playerRect.y = 88;
this.playerRect.width = 128;
this.playerRect.height = 128;
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glBlendFunc(GL30.GL_SRC_ALPHA, GL30.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
this.camera.position.x = this.playerRect.x + (this.playerRect.width / 2);
this.camera.position.y = this.playerRect.y + (this.playerRect.height / 2);
this.camera.update();
this.tiledMapRenderer.setView(this.camera);
this.tiledMapRenderer.render();
this.batch.begin();
this.batch.draw(this.playerImage, this.playerRect.x, this.playerRect.y, this.playerRect.width, this.playerRect.height);
this.batch.end();
this.batch.setProjectionMatrix(this.camera.combined);
}
}
我一整天都想不出如何解决这个问题。我希望有人能够做到。
【问题讨论】:
标签: java libgdx sprite tiled tmx