문제

So i made an inventory like in this answer: Android game rpg inventory system

My question is whats the best way to display a gui of it on the screen. If it helps ill add im using Slick2D.

도움이 되었습니까?

해결책

I guess this is what you mean, in the comment.

To add "multiple" values to one key in a Map, then basically just create your own class Something to store all the values, objects, etc. you want it to.

This is pretty sketchy. Though first you would create the class Something in our case class Item like I said in the comments.

public class Item
{
    public static enum ItemType { FOOD, WEAPON, TOOL, ARMOR; }

    public ItemType type;
    public int weight;

    public Item() {

    }

    public Item(ItemType type, int weight) {
        this.type = type;
        this.weight = weight;
    }
}

Then after when you want to create and add items to your Map you would do the following. Where of course the Map key is a Integer because it would be the inventory index, which is what I get the feeling you're using.

HashMap<Integer, Item> inventory = new HashMap<Integer, Item>();

Item i1 = new Item(ItemType.FOOD, 10);
Item i2 = new Item(ItemType.WEAPON, 20);

inventory.put(0, i1);
inventory.put(1, i2);

If you don't want/need a dynamic inventory, then instead of using a Map you could just use an array of the class Item.

Item[] inventory = new Item[10];

Where 10 is the maximum amount of Items in the Inventory.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top