如何解决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...
那么,有办法吗? 附上图片看看我想要什么。
非常感谢!
解决方法
简答:
您可以像这样使用 PolygonSpriteBatch
绘制 Sprite
和 PolygonSprite
:
polygonSpriteBatch.begin();
sprite.draw(polygonSpriteBatch);
polygonSprite.draw(polygonSpriteBatch);
更长的描述:
绘制 Sprite
或 PolygonSprite
与您问题中的示例代码略有不同。由于 SpriteBatch
都没有方法 draw(Sprite)
,PolygonSpriteBatch
也没有方法 draw(PolygonSprite)
,因此您不能执行 spriteBatch.draw(sprite)
。
这样做的方法是这样的:
spriteBatch.begin();
sprite.draw(spriteBatch);
polygonSpriteBatch.begin();
polygonSprite.draw(polygonSpriteBatch);
现在,由于 PolygonSprite.draw 将 PolygonSpriteBatch
作为参数,您将无法将 SpriteBatch
传递给此方法。
但是由于 Sprite.draw 将 Batch 对象作为参数,因此您可以传递 SpriteBatch
或 PolygonSpriteBatch
作为参数(因为这两个类都实现了 { {1}} 接口)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。