簡體   English   中英

如何在 LibGDX scene2d 中讓演員暫時移動到前景?

[英]How do I make an actor move to the foreground temporarily in LibGDX scene2d?

我有一個清單 UI,它是scene2d 中的一個表。 然后桌子上有一堆容器,它們基本上是庫存插槽。 它們具有透明的灰色背景色。 然后在每個容器中都有一個 ItemStack,其中包含一個項目的圖片和一個堆棧大小。 當我將此 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);
    }
}

您可以使用Actor.setZindex()將您拖動的演員移動到前面。

設置這個actor的z-index。 z-index 是父子節點的索引,其中較低的索引低於較高的索引。 將 z-index 設置為高於孩子的數量會將孩子移到前面。 設置小於零的 z-index 無效

一個解決方案可能是

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM