【发布时间】:2013-10-22 17:27:22
【问题描述】:
我正在使用 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
现在,我想知道是否可以缓存背景。因为它保持不变,所以不需要每次都计算它。我通过谷歌搜索找到了 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
对我来说,似乎透明度没有正确呈现。如果我拍摄没有透明度的图像,一切正常。有谁知道为什么会发生这种情况以及我该如何解决这个问题?
提前谢谢你。
【问题讨论】: