iHorse
04-28-2009, 10:22 AM
First of all, I am a beginner. I began using ActionScript about four days ago, but had some basic understanding of programming to get me started, and I seem to do fine. I do however, need some help as to how I can use arrays and how to remove a SPECIFIC item.
Here is the code and some explanation.
package
{
import player;
import player_info; //Imported from player_info is the variables carryLimit:Number and carryWeight:Number.
import item;
public class inventory
{
/*
When I drag an item to the inventory, that item will have the temporary
name addedItem, weight and name, item type and weight is stored.
Mark that, whenever I refer to addedItem in this code, I want it to
represent the item I am manipulating.
*/
public var addedItem:item;
public var inv_content:Array = new Array();
public function inventory()
{
inv_content.push("Item1");
inv_content.push("Item2");
trace(inv_content.length);
}
public function item_add()
{
if ((carryWeight:Number + addedItem.weight:Number) <= carryLimit:Number)
{
inv_content.push(addedItem.item_name);
carryWeight += addedItem.weigth;
}
else
{
trace("Your backpack is too heavy!")
}
}
public function item_remove()
{
}
}
}
What I am trying to do, is making a structure for an inventory system. I want it to be structured with an array, but I have some questions as to how to do what I want.
I want to store items in the array, but I need to know how to make each item in the inventory unique. I want to store each item in the array, including the items properties.
I also want to know how to tell one of the array values to go away. (Example inv_content.pissoff(2))
When I look through the documentation, I can only find methods like splice(//Replaces one with another) og shift(//???).
I also need to know: Will the use of a temporary object like addedItem be useful? What approach should I use instead, if this one is horrible?
Thanks in advance. :p
Here is the code and some explanation.
package
{
import player;
import player_info; //Imported from player_info is the variables carryLimit:Number and carryWeight:Number.
import item;
public class inventory
{
/*
When I drag an item to the inventory, that item will have the temporary
name addedItem, weight and name, item type and weight is stored.
Mark that, whenever I refer to addedItem in this code, I want it to
represent the item I am manipulating.
*/
public var addedItem:item;
public var inv_content:Array = new Array();
public function inventory()
{
inv_content.push("Item1");
inv_content.push("Item2");
trace(inv_content.length);
}
public function item_add()
{
if ((carryWeight:Number + addedItem.weight:Number) <= carryLimit:Number)
{
inv_content.push(addedItem.item_name);
carryWeight += addedItem.weigth;
}
else
{
trace("Your backpack is too heavy!")
}
}
public function item_remove()
{
}
}
}
What I am trying to do, is making a structure for an inventory system. I want it to be structured with an array, but I have some questions as to how to do what I want.
I want to store items in the array, but I need to know how to make each item in the inventory unique. I want to store each item in the array, including the items properties.
I also want to know how to tell one of the array values to go away. (Example inv_content.pissoff(2))
When I look through the documentation, I can only find methods like splice(//Replaces one with another) og shift(//???).
I also need to know: Will the use of a temporary object like addedItem be useful? What approach should I use instead, if this one is horrible?
Thanks in advance. :p