0
\$\begingroup\$

I have an inventory UI which is a table in scene2d. Then the table had a bunch of Containers which are basically inventory slots. They have a transparant grey background color. Then in each container is an ItemStack with a picture of an item and a stacksize. When I pull this ItemStack over a container that was created later, it moves behind the container and the transparant grey background will be in front of the item picture. How do I temporarily (or permanently) move it to the foreground so it doesn't matter in front of which container I drag the item?

Example of what happens: https://i.imgur.com/yq4njex.gifv

This is how I create the containers:

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

After that I add each ItemStack to the list of containers/inventoryslots like this:

// 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);
    }
}
\$\endgroup\$
1
\$\begingroup\$

The ZIndex of an Actor describes its position in respect to its siblings (other Actors in the same Group)

you can manipulate it with Actor.setZIndex(). There are also convienience methods for your usecase: Actor.toFront() and Actor.toBack()

The Group class has methods to add Actors in front or behind another Actor. It can allso swap the ZIndex of two of its children.

\$\endgroup\$
1
  • \$\begingroup\$ Yeah, this works. Thanks. However if the item needs to be drawn above another stage (Say the hotbar) and this stage is drawn later, I think there is no way to do this. Currently I'm using a flag that determines whether the hotbar or inventory should be drawn first, but I might consider not having multiple stages, but just edit 1 big stage. \$\endgroup\$
    – Tipsi
    Apr 8 '20 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.