数组列表中的纹理区域不会绘制到屏幕



我在LibGDX中使用spriteBatch绘制TextureRegions时遇到了一些问题。

所以我有一个承载游戏逻辑的主类。在构造函数中,我有:

atlas = new TextureAtlas(Gdx.files.internal("sheet.txt") );
this.loadTileGFX();

loadTileGFX()方法执行以下操作:

roseFrames = new ArrayList<AtlasRegion>();
roseFrames.add(atlas.findRegion("Dirt", 0));
roseFrames.add(atlas.findRegion("Dirt", 1));
roseFrames.add(atlas.findRegion("Dirt", 2));
roseFrames.add(atlas.findRegion("Dirt", 3));

然后我将AtlasRegions的arrayList传递给对象:

///in the main class
rsoe = new RoseSquare(roseFrames, st, col, row, tileWidth);
//in the constructor for the object to draw
this.textureRegions = roseFrames;

然后每个渲染()循环我调用:

batch.begin();
rose.draw(batch);
batch.end()

rose.draw()方法如下:

public void draw(SpriteBatch batch){
    batch.draw(this.textureRegions.get(1), rect.x, rect.y, rect.width, rect.height);
}

但问题是,这不会在屏幕上画任何东西

但是事情是这样的。如果我将代码改为:
 public void draw(SpriteBatch batch){
    batch.draw(new TextureAtlas(Gdx.files.internal("sheet.txt")).findRegion("Dirt", 0)), rect.x, rect.y, rect.width, rect.height);
}

然后画正确。有人能告诉我我可能犯了什么错误吗?保持清醒,我没有得到任何错误与"没有绘制"的代码。此外,我可以跟踪this.textureRegions.get(1)的细节,它们都是正确的....

谢谢。

如果你需要绘制一个有纹理的数组,你可以这样做:

batch.begin();
for (Ground ground : groundArray){
    batch.draw(ground.getTextureRegion(), ground.x, ground.y);
}
batch.end();

正如你所看到的,我在这里绘制TextureRegion。

你可以在我的回答中查看相关课程和其他信息。

回答drew的评论:

public TextureRegion customGetTextureRegion(int i){
    switch(i){
    case 1:  return atlas.findRegion("dirt1"); break;
    case 2:  return atlas.findRegion("dirt2"); break;
    case 3:  return atlas.findRegion("dirt3"); break;
    }
}

我已经找到解决我自己问题的办法了。

我还画了一些调试ShapeRenderer的东西。

问题似乎是libGDX不喜欢SpriteBatch和ShapeRenderer同时"打开":

//LibGDX Doesn't like this:
spriteBatch.begin();
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.drawRect(x, y, width, height);
shapeRenderer.end();
sprtieBatch.draw(texRegion, x, y, width, height);
spriteBatch.end();
喜欢

:

//LibGDX likes this:
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.drawRect(x, y, width, height);
shapeRenderer.end();

spriteBatch.begin();
sprtieBatch.draw(texRegion, x, y, width, height);
spriteBatch.end();

感谢大家的回复。

最新更新