当我想创建在我的关卡编辑器中制作的关卡的 PNG 时,我遇到了类似的问题。该级别由创建者放置的图块组成,然后将整个级别保存为 .png 以在游戏中使用。
以PT解释的方式解决了它。我希望它对遇到同样问题的其他人有用。
在您的应用程序配置中:cfg.useGL20 = true; 需要 GL20 才能构建 FrameBuffer
public boolean exportLevel(){
//width and height in pixels
int width = (int)lba.getPrefWidth();
int height = (int)lba.getPrefHeight();
//Create a SpriteBatch to handle the drawing.
SpriteBatch sb = new SpriteBatch();
//Set the projection matrix for the SpriteBatch.
Matrix4 projectionMatrix = new Matrix4();
//because Pixmap has its origin on the topleft and everything else in LibGDX has the origin left bottom
//we flip the projection matrix on y and move it -height. So it will end up side up in the .png
projectionMatrix.setToOrtho2D(0, -height, width, height).scale(1,-1,1);
//Set the projection matrix on the SpriteBatch
sb.setProjectionMatrix(projectionMatrix);
//Create a frame buffer.
FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
//Call begin(). So all next drawing will go to the new FrameBuffer.
fb.begin();
//Set up the SpriteBatch for drawing.
sb.begin();
//Draw all the tiles.
BuildTileActor[][] btada = lba.getTiles();
for(BuildTileActor[] btaa: btada){
for(BuildTileActor bta: btaa){
bta.drawTileOnly(sb);
}
}
//End drawing on the SpriteBatch. This will flush() any sprites remaining to be drawn as well.
sb.end();
//Then retrieve the Pixmap from the buffer.
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
//Close the FrameBuffer. Rendering will resume to the normal buffer.
fb.end();
//Save the pixmap as png to the disk.
FileHandle levelTexture = Gdx.files.local("levelTexture.png");
PixmapIO.writePNG(levelTexture, pm);
//Dispose of the resources.
fb.dispose();
sb.dispose();
注意:我是 LibGDX 的新手,所以这可能不是最好的方法。