[solved] problem with adding items

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.

Make sure [icode]ItemEntity.qnty[/icode] isn’t static.

I’m assuming [icode]entities[/icode] is a List of some kind? (ArrayList perhaps?)

The sprite of each Items object is static. Why is that? You know that means that every instance of Items is going to share that one sprite?

Yes entities is an array list that has all of my entities such as the player and these items.

If you were to implement a toString and call it, what do you get? I think it has something to do with the static sprite image. If you add this to the ItemEntity class, and call it, would it return the same for both instances of ItemEntity?

public String toString(){
return id + " " + qnty + " " + xCord + " " + yCord;
}

If the problem is what you say it is, I don’t see it.
How are you verifying the problem? Like this?

[icode]for(ItemEntity e: entities) System.out.println(e.id);[/icode]

Does it really print the same for each?

Edit: might as well do the toString() like Cold said

I’m actually testing that right now, cause my player class is non static also, so i’ll check real quick.

If you were talking about what I said, I think I got a bit confused.

I meant that my code snippet and yours plus a modified version of mine (take off the .id) would accomplish the same task, and your’s gives more information for future use as well.

I did this and it just prints out the id of whatever the last item on the list is.

What is the code you’re using to print?

for(ItemEntity e: entities) System.out.println(e.id);

Which then prints this?

or

yeah it reads just like that

Strange. Since your code is fairly convoluted (more than it needs to be really), I don’t think we can help you much without the entire program. If you decide to post it, use the pastebin.

I agree, there is definitely something we aren’t seeing here.

Here is all my code, I stayed up pretty late last night messing changing the code around with no better results than I already had, I changed the code around to more match the “Player” class since the player entity seems to be working just fine…

Like I said the problem is that the last ItemEntity that is added somehow makes all the other ItemEntitys that same one.
For example:
If I add a sword entity last it makes all the ItemEntitys swords
or if I add an air entity last it makes all the ItemEntitys air.

http://pastebin.java-gaming.org/c7e36000f97

Here is all my code, be warned its pretty amature stuff, I’m still kinda new at programming and all.

Just like I thought: ItemEntity.id is static, which means it is a variable that is ‘shared’ by all instances of that class. Further, Items.sprite is static, while it has non-static getters and setters.

Please educate yourself on what static actually means, it will save you hours upon hours of trial and error (and forum posts).

ah, thanks for pointing that out, but even after changing it to non static I still have the same issue

Worst comes to worst I think I’m going to just scrap everything I have that deals with items and such and make ItemEntity a separate interface and move everything that I currently have in ItemEntity to the Items Class