问题
I am using Libgdx to render a TileMap as a background of my game. I might render some different layers later on. Without the tilemap render and a single background picture it works correctly! Something strange happens when i use the TiledRender just take a look at it:

The two white areas are normally a touchpad and a character.
The touchpad and the character are rendered with:
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
sprite.draw(batch);
}
My Map where i render my TileMap looks like this:
public class Map extends Actor {
private TiledMap map;
private OrthogonalTiledMapRenderer render;
public int[][] mapArray = new int[Config.X_SIZE][Config.Y_SIZE];
public Map(TiledMap map){
this.map = map;
this.render = new OrthogonalTiledMapRenderer(map,(((float)Config.VIRTUAL_VIEW_HEIGHT/Config.Y_SIZE)/(float)Config.TILE_SIZE));
this.toBack();
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
this.render.setView((OrthographicCamera) this.getStage().getCamera());
render.render();
}
}
any sugestions to fix the SpriteBatch problems?
回答1:
Many of the Libgdx render helper objects assume they "own" the state in the OpenGL runtime, so the output gets confused if you have two of them making changes to OpenGL at the same time. In your case the OrthogonalTiledMapRenderer
has its own SpriteBatch
, so you have two SpriteBatch
(silently) fighting each other.
Try changing your Map.render()
method to end the in-progress SpriteBatch before rendering the map (and then restart it afterwards):
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.end();
this.render.setView((OrthographicCamera) this.getStage().getCamera());
render.render();
batch.begin();
}
If that helps, you might look at setting up your tile map to "share" the same SpriteBatch
as the rest of your code (as these are somewhat heavy objects). That should be more efficient and should remove the need to end/start the SpriteBatch
.
回答2:
I had a similar issue where I was using a batch to first draw a tilemap, then my own textures. It drew white rectangles at exactly the right size and position as the texture.
batch.setProjectionMatrix(camera.combined);
batch.begin();
mapRenderer.render();
GameObjectSystem.inst().render(RenderLayer.floor, batch);
GameObjectSystem.inst().render(RenderLayer.ground, batch);
batch.end();
Ending the batch after drawing the tilemap and restarting it fixed the problem.
batch.setProjectionMatrix(camera.combined);
batch.begin();
mapRenderer.render();
batch.end();
batch.begin();
GameObjectSystem.inst().render(RenderLayer.floor, batch);
GameObjectSystem.inst().render(RenderLayer.ground, batch);
batch.end();
来源:https://stackoverflow.com/questions/15883120/orthogonaltiledmaprenderer-and-normal-spritebatch-renders-a-white-box