我在我的java游戏中使用LibGDX和Scene2D。我知道我的问题是连接到Scene2D的,因为我使用了EXACT相同的类将它正常传递给SpriteBatch(而不是通过Stage实例),并且它按预期工作。
我让Stage管理我作为actor的所有drawables实体。它使用Batch的实现者绘制所有内容; SpriteBatch是默认值。它一直工作,直到我想绘制一个多边形,必须由PolygonSpriteBatch绘制,而不是SpriteBatch ..所以在一个Stage.draw()调用期间,我需要使用它们。
我创建了一个CheckedPolygon类,它基本上是两个相互叠加的PolygonSprites(一个是半透明的)。在draw()方法中传递的SpriteBatch暂时结束以启用PolygonSpriteBatch片刻,绘制多边形并禁用它。
输出是空屏幕,我什么都没得到。同样,当我没有使用Stage类时,它也能正常工作。
这是班级,所以你可以更好地理解。 注意:我知道这在性能方面是不好的,因为我不会处理纹理并为一个对象保留批处理,但为了简单起见它。 注2:是的,我正确地将它传递到舞台,代码执行,我通过调试检查。
public class CheckedPolygon extends Actor {
private final PolygonSprite mBackground, mCross;
private final PolygonSpriteBatch mPolygonSpriteBatch;
private final Camera mCamera;
public CheckedPolygon(Camera camera, float[] vertices, short[] triangles) {
super();
mCamera = camera;
mPolygonSpriteBatch = new PolygonSpriteBatch();
Texture textureBack = new Texture(Gdx.files.internal("source/back.png"));
textureBack.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
PolygonRegion regionBack = new PolygonRegion(new TextureRegion(textureBack), vertices, triangles);
mBackground = new PolygonSprite(regionBack);
Texture textureCross = new Texture(Gdx.files.internal("source/cross.png"));
textureCross.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
PolygonRegion regionCross = new PolygonRegion(new TextureRegion(textureCross), vertices, triangles);
mCross = new PolygonSprite(regionCross);
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.end();
mPolygonSpriteBatch.setProjectionMatrix(mCamera.combined);
mPolygonSpriteBatch.begin();
mBackground.draw(mPolygonSpriteBatch);
mCross.draw(mPolygonSpriteBatch);
mPolygonSpriteBatch.end();
batch.begin();
}
}
我这样使用它:
CheckedPolygon polygon = new CheckedPolygon(mStage.getCamera(), mTextureAtlas, new float[]{0, 500, 0, 0, 0, 500}, new short[]{0, 1, 2});
mStage.addActor(polygon);
我检查了方法PolygonSprite.getVertices()和PolygonSprite.getBoundingRectangle()中的值并获得了一些奇怪的输出......