有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在LibGDX场景2d中使演员临时移动到前台?

我有一个库存UI,它是scene2d中的一个表。然后桌子上有一堆基本上是库存槽的容器。它们有一个透明的灰色背景色。然后在每个容器中都有一个ItemStack,其中包含一个项目的图片和stacksize。当我将此ItemStack拉到稍后创建的容器上时,它会移到容器后面,透明的灰色背景将位于项目图片的前面。如何临时(或永久)将其移动到前台,以便在哪个容器前拖动项目都不重要

发生的情况的示例: https://i.imgur.com/yq4njex.gifv

以下是我创建容器的方式:

// Create inventory slots
table.row();
inventorySlots.clear();
for (int i = 0; i < inventory.getInventorySize(); i++) {
    if (i % SLOTS_PER_ROW == 0) table.row();

    Container cont = new Container();
    cont.fill();
    cont.background(Utils.createDrawable(1, 1, new Color(0, 0, 0, 0.4f)));

    inventorySlots.add(cont);
    table.add(cont).prefHeight(50).prefWidth(50).pad(5);
}

之后,我将每个ItemStack添加到容器/清单插槽列表中,如下所示:

// Add ItemStacks from Items to their respective slot
ArrayList<Item> items = inventory.getItems();
for(Container slot : inventorySlots) {
    Item item = items.get(inventorySlots.indexOf(slot));

    if(item != null) {
        ItemStack itemStack = new ItemStack(item);
        addInventoryEvent(itemStack, items);
        slot.pad(5);
        slot.setActor(itemStack);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您可以使用Actor.setZindex()将要拖动的参与者移动到前面

    Sets the z-index of this actor. The z-index is the index into the parent's children, where a lower index is below a higher index. Setting a z-index higher than the number of children will move the child to the front. Setting a z-index less than zero is invalid

    解决办法可能是

    image.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            image.setZIndex(stage.getActors().size);
            return true;
        }
    });
    

    enter image description here