Hey guys, I’m having a pretty weird problem with my code and am hoping yall would be able to help out. I’ll explain the code as I post it.
This is a method in my GameplayState class, which adds “ItemEntitys” to the game, as you can see I need to put in an x, y, the item number, and quantity.
public static void itemEntityAdder(){//x , y , item number, quantity
entities.add(0, new ItemEntity (100, 100, 1, 1));
entities.add(0, new ItemEntity (50, 50, 0, 1));
}
This is the constructor for the “ItemEntity” object
public ItemEntity (int x, int y, int itemID, int quantity){
id=itemID;
qnty=quantity;
xCord=x;
yCord=y;
}
This is the list of my different items
public class ItemList {
public static Items[] itemList = new Items[2]; //itemList Array
//list of the items name---sprite---quantity
public void itemList(){
itemList[0]= new Items("",Items.itemSprites[0],1); //air
itemList[1]= new Items("Basic Sword",Items.itemSprites[1],1);
}
}
Here is my Items class, also where I import my textures on lines 10-15
public class Items{
private String name;
private int itemID;
public static BufferedImage sprite;
private int quantity;
static BufferedImage [] itemSprites= new BufferedImage[2];
static BufferedImage itemSprite;
//imports sprites for items
public static void itemSprites(){
try {
itemSprites[0]=ImageIO.read(Items.class.getClassLoader().getResourceAsStream("air.png"));
itemSprites[1]=ImageIO.read(Items.class.getClassLoader().getResourceAsStream("sword.png"));
} catch (IOException e) {e.printStackTrace();}
}
//sets a default value for items (this is air)
public Items(){
name="";
itemID=0;
sprite=null;
quantity=0;
}
//parameterized constructor for an item
public Items (String nme, BufferedImage spte, int qnty){
setName(nme);
setSprite(spte);
setQuantity(qnty);
}
//getter method for name
private String getName(){
return name;
}
//setter method for name
private void setName(String nme){
name=nme;
}
//getter method for sprite
private BufferedImage getSprite(){
return sprite;
}
//setter method for sprite
private void setSprite(BufferedImage spte){
sprite=spte;
}
//getter method for quantity
private int getQuantity(){
return quantity;
}
//setter method for quantity
private void setQuantity(int qnty){
quantity=qnty;
}
}
Here is my getSprite method that goes to lines 10-15 of my Items class and gets the texture for the item of whichever item id you stated before.
public BufferedImage getSprite() {
itemSprite=Items.itemSprites[id];
return itemSprite;
}
Ok so heres whats going on. Whenever I add a new ItemEntity It changes both of the ItemEntitys to whatever the second or last one I add in. for example:
entities.add(0, new ItemEntity (100, 100, 1, 1));
entities.add(0, new ItemEntity (50, 50, 0, 1));
makes both items “item 0”
or
entities.add(0, new ItemEntity (100, 100, 0, 1));
entities.add(0, new ItemEntity (50, 50, 1, 1));
makes both items “item1”
Anyways I cant seem to figure it out at all, if yall are able to help that would be awsome, if you need a better explanation please let me know.