Skip to main content
Improved code formatting, formatted the post.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

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 completlycompletely, 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:

In the init method I loop throughtthrough 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:

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

Thank you.

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 completly, 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:

In the init method I loop throught 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:

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

Thank you.

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:

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:

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

Java lwjgl game arraylist basedLWJGL inventory [help please]using ArrayList

So ImI'm making thisa game in javaJava with LWJGL 2.9.3 and Slick2dSlick2D, it's going awesome and now I'm working on thecreating an inventory. I managed to set it up, almost completly, but I ran into a problem: when Im. When I'm rendering the slots, only the last slot gets render. (I loop through a inventoryslots arraylistan ArrayList<InventorySlot> and then render each element of it.) Ill post. Here is the code bellow and explain it.for the Inventory:

In the init method I loop throught the inventoryslots arraylistArrayList<InventorySlot> and set each element to be a new InventorySlot class, and itwhich takes an x, y, an ID and a value (the item it contains; -1 isfor nothing). Then I render and update the InventorySlots in their class wich Ill show down here;each InventorySlot, as shown here:

It's pretty much the most basic thing you would want. theThe part were it mentions the Item class is also acctuallyactually simple,. Here is the Item class:

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

Thank you.

Java lwjgl game arraylist based inventory [help please]

So Im making this game in java with LWJGL 2.9.3 and Slick2d, it's going awesome and now I'm working on the inventory. I managed to set it up, almost completly but I ran into a problem: when Im rendering the slots, only the last slot gets render. (I loop through a inventoryslots arraylist and then render each element of it.) Ill post the code bellow and explain it.

In the init method I loop throught the inventoryslots arraylist and set each element to be a new InventorySlot class, and it takes an x, y, an ID and a value (the item it contains; -1 is nothing). Then I render and update the InventorySlots in their class wich Ill show down here;

It's pretty much the most basic thing you would want. the part were it mentions the Item class is also acctually simple, Here is the Item class:

Each Image is named with its items ID .png And when I run the game only the last slot appears. Allthough it works (I can put items into it, that's all Ive tried so far) I do need all of them to work, so could anyone please help me with this? Thank you

Java LWJGL inventory using ArrayList

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 completly, 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:

In the init method I loop throught 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:

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

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?

Thank you.

Typo in the title :/
Link
Ivan Jerza
  • 383
  • 3
  • 12
Loading
Source Link
Ivan Jerza
  • 383
  • 3
  • 12
Loading