libGDX 平铺地图隐藏视口外的精灵
libGDX Tiled Map hiding sprites that are out of viewport
我开始使用 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);
}
}
我一整天都不知道怎么解决这个问题。希望有人能做到。
您可以在设置 tiledMapRenderer.setView(cam);
时添加一个偏移集
而不是那样做
float width = cam.viewportWidth *cam.zoom;
float height = cam.viewportHeight * cam.zoom;
float w = width * Math.abs(cam.up.y) + height * Math.abs(cam.up.x);
float h = height * Math.abs(cam.up.y) + width * Math.abs(cam.up.x);
float x = cam.position.x - w / 2;
float y = cam.position.y - h / 2;
x -= offset;
w += offset;
tiledMapRenderer.setView(cam.combined,x,y,w,h);
我开始使用 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);
}
}
我一整天都不知道怎么解决这个问题。希望有人能做到。
您可以在设置 tiledMapRenderer.setView(cam);
而不是那样做
float width = cam.viewportWidth *cam.zoom;
float height = cam.viewportHeight * cam.zoom;
float w = width * Math.abs(cam.up.y) + height * Math.abs(cam.up.x);
float h = height * Math.abs(cam.up.y) + width * Math.abs(cam.up.x);
float x = cam.position.x - w / 2;
float y = cam.position.y - h / 2;
x -= offset;
w += offset;
tiledMapRenderer.setView(cam.combined,x,y,w,h);