libGDX SpriteCache:图像透明度无法正确显示

时间:2013-10-22 17:27:22

标签: java android libgdx

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

我使用SpriteBatch-Object显示环境,如下所示:

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);
    }
}

这样做正常,纹理显示得很好:Tower Defense using spriteBatch

现在,我想知道是否可以缓存背景。因为它保持不变,所以不需要每次都计算它。我通过Google-Search找到了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);
    }
} 

现在游戏看起来像这样:Tower Defense using spriteCache

对我来说,似乎透明度没有正确呈现。如果我拍摄没有透明度的图像,一切正常。有没有人有一个想法,为什么会发生这种情况以及如何解决这个问题?

提前谢谢你。

1 个答案:

答案 0 :(得分:2)

取自SpriteCache documentation

  

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

我想没有更多要说的了。