Java libGDX SpriteCache:图像的透明度未正确显示

Java libGDX SpriteCache:图像的透明度未正确显示,java,android,libgdx,Java,Android,Libgdx,我正在用libGDX开发一个塔防游戏。我刚刚开始,我能够显示路径、“环境”和沿着路径行走的敌人 我使用SpriteBatch对象显示环境,如下所示: public LevelController(Level level) { spriteBach = new SpriteBach(); atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack")); } public void set

我正在用libGDX开发一个塔防游戏。我刚刚开始,我能够显示路径、“环境”和沿着路径行走的敌人

我使用SpriteBatch对象显示环境,如下所示:

public LevelController(Level level) {
    spriteBach = new SpriteBach();
    atlas =  new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));
}

public void setSize() {
    spriteBatch.setProjectionMatrix(this.cam.combined);
}

public void render() {
    spriteBatch.begin();
    drawTowerBases();
    spriteBatch.end();
}

private void drawTowerBases() {
    // for each tower base (=environment square)
    TextureRegion towerBaseTexture = atlas.findRegion("TowerBase");

    if(towerBaseTexture != null) {
        spriteBatch.draw(towerBaseTexture, x, y, 1f, 1f);
    }
}
这工作正常,纹理显示良好:

现在,我想知道是否可以缓存背景。因为它保持不变,所以没有必要每次都计算它。我通过谷歌搜索找到了SpriteCache。因此,我将代码更改如下:

public LevelController(Level Level) {
    spriteCache= new SpriteCache();

    atlas =  new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));

    spriteCache.beginCache();
    this.drawTowerBases();
    this.spriteCacheEnvironmentId = spriteCache.endCache();
}

public void setSize() {
    this.cam.update();
    spriteCache.setProjectionMatrix(this.cam.combined);
}

public void render() {
    spriteCache.begin();
    spriteCache.draw(this.spriteCacheEnvironmentId);
    spriteCache.end();
}

private void drawTowerBases() {
    // for each tower base (=environment square)
    TextureRegion towerBaseTexture = atlas.findRegion("TowerBase");

    if(towerBaseTexture != null) {
        spriteCache.add(towerBaseTexture, x, y, 1f, 1f);
    }
} 
现在游戏看起来是这样的:

对我来说,透明度似乎没有得到适当的渲染。如果我在没有透明度的情况下拍摄图像,一切正常。有人知道为什么会发生这种情况吗?我该如何解决

先谢谢你

摘自:

请注意,SpriteCache不管理混合。您需要启用混合(Gdx.gl.glEnable(GL10.gl_BLEND);)并根据需要在调用draw(int)之前或之间设置混合函数


我想没什么好说的了。

谢谢。我让它在spriteCache.begin()之前调用这两行代码:Gdx.gl.glEnable(GL10.gl_BLEND);GL10.gl.glBlendFunc(GL10.gl_SRC_ALPHA,GL10.gl_ONE_减去SRC_ALPHA);我对SpriteCache有点困惑。SpriteCache文档中没有明确说明,但SpriteCache似乎是存储在GPU内存中的“刚刚渲染的内容的快照”。您可以使用其ID获取此快照并将其显示在屏幕上,而无需再次发送所有内容。对吗?这也意味着您可以在屏幕上的任何位置渲染快照-移动快照,对吗?博士对“不变的几何体”有点困惑。我不确定你的问题到底是什么。是的,您应该能够通过操纵缓存的投影矩阵来“在任何地方”渲染缓存。在使用SpriteCache渲染时,您可以创建渲染内容的形状快照,并将其存储在GPU中的ID下。您可以稍后使用此ID调用它。通过操纵SpriteCache projection matix,您可以在任意位置显示此快照。我的推理正确吗?:)我想是的,你为什么不试试呢?