【问题标题】:Draw Enum values assigned to Sprite绘制分配给 Sprite 的枚举值
【发布时间】:2014-08-01 09:15:48
【问题描述】:

我正在使用 LibGDX 制作游戏,我有 2 个类和一个枚举。我的枚举看起来像这样:

public enum Item {

    PEN, GLASS, AND, SO, ON;

    private static Item current = PEN;
    public static Item getNext() {
        current = values()[current.ordinal()];
        return current;
    }

在我的第一堂课中是我的游戏地图的片段,其中一些是使用此构造函数的枚举值初始化的:

public class MapPiece {

    private Sprite sprite;
    private Item item;

    public MapPiece (Sprite sprite, Item item) {
        this.sprite = sprite;
        this.item = item;
    }

这个类还有一个绘制函数,如下所示:

public void draw(Batch batch, float x, float y) {
    sprite.setPosition(x, y);
    sprite.draw(batch);
}

¨

在最后一节课中,我初始化了我的地图片段并将它们放入一个二维数组中,如下所示:

atlas = new TextureAtlas("atlas.pack");
mapPieces[0][0] = new MapPiece(atlas.createSprite("piece"), Item.getNext();
mapPieces[0][1] = new MapPiece(atlas.createSprite("piec2"), Item.getNext();

...以此类推,直到我添加了所有作品

itemSprites 是这样创建的:

for ( Item i : Item.values()) {
    Sprite itemSprite = atlas.createSprite(i.toString());

地图块正在像这样的精灵批次上绘制:

int mapPieceSize = 100;
for (int i = 0; i < mapPiece.length; i++) {
                for (int j = 0; j < mapPiece[0].length; j++) {
                    mapPiece[j][i].draw(spriteBatch, i * mapPieceSize, j * mapPieceSize);
                }
            }

现在我的问题。项目的纹理与地图块的大小相同,但除了项目所在的中间之外都是透明的。我这样做是为了更容易定位。

我想要做的是在包含项目枚举值的 mapPiece 顶部绘制项目的精灵。例如,如果mapPiece[0][5] 持有枚举值PEN,我想从我的atlas 中获取一个名为PEN 的纹理,然后在与mapPiece[0][5] 相同的位置上绘制。我希望这不会太混乱。

【问题讨论】:

  • 不清楚你的问题是什么......你描述了你是如何做的,但没有描述出了什么问题。顺便说一句,您的 Item 枚举的 getNext 方法的 current = values()[current.ordinal()]; 行没有做任何事情。也许应该是current = values()[(current.ordinal()+1)%values().length];
  • @Tenfour04 并不是真的有什么“出错”了,更多的是我不知道如何解决我的问题(我希望很清楚),我所有的尝试都失败了。
  • 等等,我知道它在哪里令人困惑。 tiles 数组是什么?它与mapPieces 数组有何不同?它们在两个维度上的大小是否相同?瓷砖阵列是背景,而 mapPieces 是前景元素吗?因为您的 for 循环与 mapPiece.length 比较但从 tiles 数组中提取似乎很奇怪。
  • @Tenfour04 Ops,抱歉,我为问题调整代码只是一个错误。我会纠正它。 *编辑更新,现在代码是正确的

标签: java enums libgdx


【解决方案1】:

我还不能完全确定我是否理解了这个问题,因为我不确定它比你已经做过的更困难,但我想你可以这样做的一种方法是这样的。

将纹理区域字段添加到枚举(非静态)。

public enum Item {

    PEN, GLASS, AND, SO, ON;

    private static Item current = PEN;
    public static Item getNext() {
        current = values()[(current.ordinal()+1)%values().length];
        return current;
    }

    public TextureRegion region;
}

然后在加载图集后立即将纹理区域填充到枚举实例中。

Item.PEN.region = atlas.findRegion("PEN");
Item.GLASS.region = atlas.findRegion("GLASS");
...

然后在遍历你的地图片段时,绘制背景图块和项目图块:

int mapPieceSize = 100;
for (int i = 0; i < mapPiece.length; i++) {
    for (int j = 0; j < mapPiece[0].length; j++) {

        //You had a typo on this line
        mapPiece[j][i].sprite.draw(spriteBatch, i * mapPieceSize, j * mapPieceSize);


        //draw the texture region directly without using a sprite instance
        spriteBatch.draw(mapPiece[j][i].item.region, i * mapPieceSize, j * mapPieceSize);

    }
}

另外,您提到您的物品周围有一堆不可见的填充物。这可能会通过绘制大量不可见像素来浪费性能。如果你想进入并调整它们的位置,你可以在 Item 实例中存储一个偏移量,或者为每个 MapPiece 实例存储一个偏移量,并在绘制项目时将其添加到 x 和 y 位置。

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    相关资源
    最近更新 更多