【问题标题】:Drawing Overlapping Images in Libgdx with a Table在 Libgdx 中使用表格绘制重叠图像
【发布时间】:2014-01-24 15:30:03
【问题描述】:

基本上,我正在尝试将一个空的生命值条绘制为图像,然后将其顶部的实际生命值条绘制为另一幅图像,以便在需要更新时缩短实际的生命值条。这是我到目前为止所拥有的:

    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);

但这将他们并排吸引。如何绘制它以使图像重叠(底部为空健康栏,顶部为健康栏)?

【问题讨论】:

  • 需要使用table吗?如果您需要尝试使用组,请将空的和正常的高度条添加到它并将组添加到表中
  • 效果很好,谢谢!
  • 我应该添加它作为答案吗?
  • @Springrbua 这是一个很好的答案,解决了我的问题,谢谢
  • @Kintaro 我很高兴能帮上忙 (:

标签: java android libgdx


【解决方案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();

【讨论】:

    【解决方案2】:

    您可能应该使用 Scene2D 的 WidgetGroup(或者,如果您不使用布局管理器,例如 TableGroup),例如

    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);
    

    请注意,您可以简单地通过在它们上使用.setPosition() 来抵消这些条,就像使用 libGDX 的 2.5D 对象一样。此外,您可以使用方法链使您的 libGDX 代码更简洁,尽管这主要是风格问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2014-04-21
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多