hi all,

im doing a project where i have to implment a rpg game...
i have a class called item and the constructor looks something like

public Item(int x, int y, String image_path, boolean z)
{
map_x = x;
map_y = y;
image = new Image (image_path);
pickup = z;
}

I also have another class called player which has an array of type Item that stores what items the player has.

In the class Item, i made a method called pickupitem which stores the item into the player's inventory if the player comes to close.

the problem is, i not sure how to enter a item into the array

so i have something like:

if player comes within 50 pixels of item, then:

player.inventory[0] = ?!?!?

im not sure wat to put there...i tried doing something like this.clone() but it wouldn't work. I also tried doing new Item (this.map_x, this.map_y, this.image, this.pickup) but also didn't work. Can someone please help?

Thanks alot!

The new Item approach you listed will definitely put an item into inventory, assuming that inventory is an array of Items. However, whether or not the item will be what you want it to be depends on what parameters you are passing in. Could you post the Item class and the class where you declare the array, in code tags?

Forgive me if I have misunderstood here, but it looks to me like you are trying to get an actual Object to put into the array - hence clone and new.
Arrays don't hold Objects. Just like variables, they hold references (pointers) to Objects. So if you have found a nearby Item, and stored (a reference to) it in variable nearebyItem, yo can simply say
player.inventory[0] = nearbyItem;
That copies the reference into the aray, so player.inventory[0] now refers to the same Item as nearbyItem does.

Forgive me if I have misunderstood here, but it looks to me like you are trying to get an actual Object to put into the array - hence clone and new.
Arrays don't hold Objects. Just like variables, they hold references (pointers) to Objects. So if you have found a nearby Item, and stored (a reference to) it in variable nearebyItem, yo can simply say
player.inventory[0] = nearbyItem;
That copies the reference into the aray, so player.inventory[0] now refers to the same Item as nearbyItem does.

True, but he could also use player.inventory[0] = new Item(); (assuming the constructor exists), and he said he tried that and it "didn't work", whatever that means.

no worries now, i solved the problem turns out that the arguments i was giving into new Item() was wrong because the constructor took a string and the actual object doesn not have string as a attribute instead it has a type Image. i just had to create a new string attribute for the items that specified the image path. Thanks everyone!

True, but he could also use player.inventory[0] = new Item(); (assuming the constructor exists), and he said he tried that and it "didn't work", whatever that means.

Yes. The reason I was (am still) worried is that the requirement was "pick up an item". I interpreted that to mean there was an existing item that was going to be put into the player's inventory. The "new" approach creates a new Item that may have the same attribute values as the original item, but still is not the the same item. We now have two items, one still on the ground and one in inventory. Is that what's required?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.