1
\$\begingroup\$

I'm making a game in Java with LWJGL 2.9.3 and Slick2D, and I'm working on creating an inventory. I managed to set it up almost completely, but I ran into a problem. When I'm rendering the slots, only the last slot gets render (I loop through an ArrayList<InventorySlot> and then render each element of it).

Here is the code for the Inventory:

package com.jarza.ageofquest.inventory;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Inventory {

    public static int x, y;
    public static int slotnum;
    static Image invbg;

    public static ArrayList<InventorySlot> inventoryslots = new ArrayList<InventorySlot>();

    public Inventory(int slotNum){
        slotnum = slotNum;

        try {
            invbg = new Image("res/inv/invbg.png");
        } catch (SlickException e) {
            e.printStackTrace();
        }
        x = (1080/2) - invbg.getWidth();
        y = 20;
    }

    public static void init(){
        // Initializes all the slots
        int k = 0;
        for(int i = 0; i < (slotnum / 10); i++){
            for(int j = 0; j < 10; j++){
                inventoryslots.add(new InventorySlot((i * 32), (j * 32), k, 1));
                System.out.println((i * 32) + " / " + (j * 32) + " / " + k);
                k++;
            }
        }

        Item.init();
    }

    public static void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.drawImage(invbg, x, y);
        for(InventorySlot s : inventoryslots){
            s.render(g);
            //System.out.println("ID: " + s.getID());
        }


    }

    public void update(Graphics g){
        for(InventorySlot s : inventoryslots){
            s.update();
        }
    }

    public static void addItem(int id){
        for(InventorySlot s : inventoryslots){
            if(s.getItem() == -1){
                s.setItem(id);
            }
        }
    }
}

In the init method I loop through the ArrayList<InventorySlot> and set each element to a new InventorySlot, which takes an x, y, an ID and a value (the item it contains; -1 for nothing). Then I render and update each InventorySlot, as shown here:

package com.jarza.ageofquest.inventory;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import com.jarza.ageofquest.states.Play;

public class InventorySlot {

    Image slot;
    public static int x, y, id, item;

    @SuppressWarnings("static-access")
    public InventorySlot(int x, int y, int id, int item) {
        try {
            slot = new Image("res/inv/slot.png");
        } catch (SlickException e) {
            e.printStackTrace();
        }
        this.x = x;
        this.y = y;
        this.id = id;
        this.item = item;
    }

    public void render(Graphics g) {
        // Draws the slot, Ill later add drawing items
        g.drawImage(slot, x, y);
        g.drawImage(Item.getImg(item), x, y);
        //System.out.println("ID:" + id + " item: " + item);
    }

    public void update(){
        // To be continued...
    }

    public int getID(){
        return id;
    }

    public int getItem(){
        return item;
    }

    public void setItem(int iid) {
        item = iid;
    }
}

It's pretty much the most basic thing you would want. The part where it mentions the Item class is also actually simple. Here is the Item class:

package com.jarza.ageofquest.inventory;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Item {

    public static int dmg, tier;
    public static int  maxItems = 2;
    public static String name;

    public static Image noitem;
    public static Image[] img = new Image[2]; 
    public static String[][] names = new String[maxItems][1];

    public Item(String name, int dmg, int tier){
        this.name = name;
        this.dmg = dmg;
        this.tier = tier;
    }

    public static void init(){
        try {
            noitem = new Image("res/inv/items/noitem.png");
        } catch (SlickException e1) {
            e1.printStackTrace();
        }

        for(int i = 0; i < maxItems; i++){
            try {
                img[i] = new Image("res/inv/items/" + i + ".png");
            } catch (SlickException e) {
                e.printStackTrace();
            }
        }
    }

    public static Image getImg(int itemid){
        if(itemid != -1){
            return img[itemid];
        }else{
            return noitem;
        }
    }

    public String getName(int itemid){
        return names[itemid][0];
    }

    public String getTier(int itemid){
        return names[itemid][1];
    }
}

Each Image is named with it's items ID .png. When I run the game, only the last slot appears. Although it works (I can put items into it) I do need all of them to work, so could anyone please help me with this?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The properties x, y, id, item of your InventorySlot class are declared as static, thus not being bound to single instances.
This results in overriding these values for all instances upon changes, which is, why only your last slot is being rendered. In fact, they are all being rendered, though, all at the same position.

Simply remove the static keyword in your declaration, to get unique values for each InventorySlot instance:

public int x, y, id, item;
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .