Monday, May 30, 2011
Back to the tester
So after re-writing items almost completely I began re-writing them again. The version I was working with was a simple array of item name's as strings. This worked really well but for one thing that doesn't give quantity. The big advantage was array.push(item_name); and that was the entire code.
But as has been a gripe for a while is milk creating tons of milk and I needed to figure out a way to also to have ammo. So I changed the simple array of strings to an array of classes. The advantage of that is obvious you can have more then one property in a nice set up for arrays. You lose the easy push though and have to use a constructor. Oh well. On the other hand you know who has made an item and feed your milk cow her own milk if your a pervert... like me.
The actual stacking mechanic is still not working as it requires a pop up, which is proving to be interesting work as a class. But I'm working on it. Though tonight is officially a lazy day... we shall see though.
Daisy
Subscribe to:
Post Comments (Atom)
when is the next download going to be available?
ReplyDeletety
Hm i wonder what's the remaining feature left to be implement? Is it still much? Hope someone compile it into one release and it's not too long.
ReplyDeleteI'm not sure where your problem is.
ReplyDeleteFlash arrays can contain elements of any type any type, so you can do:
Array.push(new StackableObject);
and then Array.pop() will give you back the last StackableObject in the array.
If all the objects are the same you can use a vector rather than an array - vectors can only contain one type, but as long as your vector will have only one type of StackableObject (i.e. Milk) then that's not a problem. push() and pop() will work as expected. So you'd have
var MilkVector:Vector.;
MilkVector = new Vector.();
MilkVector.push() and MilkVector.pop() then give you the behaviour you want.
I think your missing the point I can definitely add an object, that's what I'm doing. But it has to go through a constructor.
ReplyDeleteit was:
itemArray.push("item_name");
but now i have to define the items of the object unless i want to make a new type of class for every item...
public static function createNewItem(item_name:String, producer:String, number:int, tempArr:Array) {
var temp:Item = new Item();
temp.name = item_name;
temp.producer = producer;
if (number <= 0) {
temp.number = 1;
}
else {
temp.number = number;
}
tempArr.push(temp);
}
Daisy
Couldn't you just update the Item class to include that extra information? (After all, maybe the store's selling a 3-pack of healing potions, or you might find a couple apples on the ground, so it could be useful, even for items outside of the inventory.)
ReplyDeleteThen you could use something like:
var temp:Item = new Item(itemName, producerName, quantity);
itemArray.push(temp);
So, rather than an array of strings, you get an array of items.
Of course, you'll need to check to see if an item with that name already exists. If so, add the new item's quantity to the old one's. If not, push the new item. That's going to be a problem, no matter how you implement it, though.
If you don't need to provide all of those values for every item in the game,just provide some default values, so maybe the constructor might look something like this:
public Item(itemName:String, itemProducer:String = "unknown", itemQuantity:int = 1)
{
name=itemName;
producer=itemProducer;
quantity=itemQuantity;
}
Ah, apologies, I see what you're saying - rather than push() and pop() to manage inventory items you'll need to adjust a property of the class.
ReplyDeleteThe best way to handle this is to have two arrays, one associative and one not.
So what you can do is the following:
Inventory is an array of strings, and the strings are keys to the Items associative array. If the item doesn't already exist, you can create it with the new objects.
for example:
// I'm assuming that StackableObject has methods
// quantity() that gives the value of an
// internal quantity property, add()
// that adds a number to the quantity property,
// name() that returns the name property,
// and a constructor that takes a string for
// name and an integer for quantity.
m = new StackableObject("Milk", 3);
Items = new Array();
Items[m.name()] = m;
Inventory = new Array();
Inventory.push("Milk");
trace(Items["Milk"].quantity()); // 3
// So you've put a string in the inventory
// If you produce another milk you can just
// do the following assuming there's an add()
// method in the StackableObject class.
Items["Milk"].add(1);
trace(Items["Milk"].quantity()); // 4
Removing items from the inventory is more complex because delete doesn't change the length of the array, you just have an empty item - pop() removes the complexity so it's easier but as you noted, it's doesn't work as handily when your item can be anywhere in the array.
For this, I'd suggest looking at this thread which discusses extending the array object by adding an indexOf method that allows for searching by value. http://www.kirupa.com/forum/showthread.php?190234-Finding-Any-Value-in-Array-%28not-indexOf%29
Then you can use Array.splice to remove items entirely.