LibGdx:在同一批次上渲染 Sprite 和 PolygonSprite?

问题描述

对于我的应用程序,我需要在同一帧中渲染精灵(或纹理)和多边形精灵(或网格)。 对于精灵,我在 Spritebatch 上渲染它们,对于 polygonesprites,我应该在 polygonspritebatch 上渲染它们。但我真的做不到:

spriteBatch.begin()
spritebatch.draw(sprite)
...
spriteBatch.end()

polygonspritebatch.begin()
polygonspritebatch.draw(polygonsprite)
...
polygonspritebatch.end()

spriteBatch.begin()
spritebatch.draw(sprite)
...
spriteBatch.end()

etc...

那么,有办法吗? 附上图片看看我想要什么。

Sprite & PolygonSprite

非常感谢!

解决方法

简答:

您可以像这样使用 PolygonSpriteBatch 绘制 SpritePolygonSprite

polygonSpriteBatch.begin();

sprite.draw(polygonSpriteBatch);
polygonSprite.draw(polygonSpriteBatch);

更长的描述:

绘制 SpritePolygonSprite 与您问题中的示例代码略有不同。由于 SpriteBatch 都没有方法 draw(Sprite)PolygonSpriteBatch 也没有方法 draw(PolygonSprite),因此您不能执行 spriteBatch.draw(sprite)

这样做的方法是这样的:

spriteBatch.begin();
sprite.draw(spriteBatch);

polygonSpriteBatch.begin();
polygonSprite.draw(polygonSpriteBatch);

现在,由于 PolygonSprite.drawPolygonSpriteBatch 作为参数,您将无法将 SpriteBatch 传递给此方法。

但是由于 Sprite.drawBatch 对象作为参数,因此您可以传递 SpriteBatchPolygonSpriteBatch 作为参数(因为这两个类都实现了 { {1}} 接口)。