使用Libgdx中的Table绘制重叠图像

5

基本上,我正在尝试绘制一个空的健康条形图像,然后在其上方绘制实际的健康条形图像,以便在需要更新时只需缩短实际的健康条。这是我目前的代码:

    TextureAtlas HUDatlas = new TextureAtlas(Gdx.files.internal("data/ui/HUDPack/textures.pack"));

    emptyPlayerHealthBar = new Image(HUDatlas.findRegion("empty-health-bar"));
    playerHealthBar = new Image(HUDatlas.findRegion("health-bar"));

    //Creating the table
    table = new Table(skin);
    table.debug();
    table.setBounds(0, 0, Gdx.graphics.getWidth(), 50);
    table.left();
    table.top();
    table.add(playerHealthBar);
    table.add(emptyPlayerHealthBar);
    stage.addActor(table);

但是这会将它们并排绘制。我该如何绘制它,使图像重叠(空的生命条在底部,生命条在顶部)?

3
需要使用表格吗?如果需要,请尝试使用一个组,将空的和正常的生命条添加到其中,并将该组添加到表格中。 - Robert P
那个完美地运行了,谢谢! - user3232556
我应该将其添加为答案吗? - Robert P
@Springrbua,你的回答解决了我的问题,非常感谢。 - Don
@Kintaro 我很高兴我能帮到你 (: - Robert P
2个回答

1
我使用了这段代码将一个菱形放在一个方框上:

Image boxImage = new Image(Assets.instance.gifts.box);
Image diamondImage = new Image(Assets.instance.gifts.diamond);
Stack diamondBox = new Stack();
diamondBox.addActor(boxImage);
diamondBox.addActor(diamondImage);
tbl.add(diamondBox).width(20).height(20);
tbl.row();

0
你应该使用Scene2D的WidgetGroup(或者,如果你不使用布局管理器,例如Table、Group)来实现这个功能,例如。
TextureAtlas HUDatlas = new TextureAtlas(Gdx.files.internal("data/ui/HUDPack/textures.pack"));
emptyPlayerHealthBar = new Image(HUDatlas.findRegion("empty-health-bar"));
playerHealthBar = new Image(HUDatlas.findRegion("health-bar"));

// creating the group
WidgetGroup group = new WidgetGroup();
group.addActor(playerHealthBar);
group.addActor(emptyPlayerHealthBar);

// creating the table
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), 50);
table.debug().left().top().add(group);

stage.addActor(table);

请注意,您可以像处理libGDX的2.5D对象一样,通过在这些条形图上使用.setPosition()来进行偏移。此外,您可以通过使用方法链接使您的libGDX代码更加简洁,尽管这主要是一个风格问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接