PDA

View Full Version : [AS2] Help with game inventory system needed


Ransom1337
06-19-2009, 03:48 PM
Hello,

I am using the tutorial for the simple inventory system located here (http://www.flashkit.com/tutorials/Games/RPG_PART-CotteN_K-805/index.php).

I've got it working fine within my game I'm making. However, if the player uses an item in the middle (or, in fact, anywhere other than the end) of the inventory list, it leaves a gap where it once was. How would I go about making each of the found items in the inventory move back one slot upon the player using an item?

I've uploaded a sample of my current inventory system here (http://www.megaupload.com/?d=I7Z6JQIH) (I couldn't get it to attach here for some reason). To understand what I mean, pick up all four items (in any order) and then "use" one of the first three items you picked up. You should see a gap has been left in the inventory. I want this gap to be filled in by the remaining items in the inventory once said item has been used.

I've been thinking about it and I think a method that may work would be to do a hit test for all of the slots every time an item is used (to check if any slots don't have any items overlapping them), and move all of the items that are positioned to the right of it left by 60 pixels (as each slot and item is 60x60). This could work in theory but I'm not sure if it's the best method, or how to actually do it.

Does anyone know how I could achieve this? Or know of any better tutorials for inventory systems that include this feature already?

Thank you in advance.

rrh
06-22-2009, 12:58 AM
You could store them in an array, and each time you remove something, you splice it out of the array, then for loop through the array and re-assign the containers for each item based on the index in the array.

Hittest is probably unnecessary.

Ransom1337
06-26-2009, 12:37 PM
Okay, thanks for your advice. I'm not clued up on actionscript besides the basics, is there a chance you could give me an example of the code I'd use?

Ransom1337
06-28-2009, 06:45 PM
Anyone have any ideas?

rrh
06-29-2009, 03:56 PM
Look up

//look these up in your help files
Array
Array.splice()

//and the mixture of splice and looping trips people up enough, I'll give you this:
for (var i:Number=array.length-1;i>=0;i--) {
}

But I think in this case you won't do the splicing inside a loop, since you'll only remove one item at a time. So you can probably use the more common forwards loop.

Ransom1337
06-30-2009, 09:48 PM
Okay, thanks for your advice. It's a little over my head, to be honest. Does anyone know of a simpler method, or if the hittest thing would work?

kaptain kosher
06-30-2009, 11:17 PM
Will the Hittest work... probably.
However, it is much more complicated than using an Array. Plus, if you're serious about game design an understanding of how Arrays work is vital. And possibly most important for game design the Array uses considerably less memory than the hittest. Its always intimidating to learn a new thing but you need to know Arrays.

Underneath, I've tried to help you along the path I would take to make the inventory system.

For what you need, off the top of my head you'll need to know the basics of an array which you can learn by googling it. And you'll need to know the push() and splice() methods which you can learn off the actionscript documentation online by googling "Array actionscript 3", and the for loop which you can also google.

More specifically you need to have an inventory array. Then when you pick something up you add the item to the array [push()]. Then you'll need a method to assign the x and y values based on the items position in the array [this will employ the for loop]. Then you'll need to splice the item out of the array whenever it is used [splice()]. The only really complicated part of this is the for loop, and MIGHT look something like this.

inventoryArray[0].x = someValue;
inventoryArray[0].y = someValue;
for(var i:int = 1; i < inventoryArray.length, i++)
{
inventoryArray[i].x = inventoryArray[i - 1].x + someOtherValue;
inventoryArray[i].y = inventoryArray[i - 1].y + someOtherValue;
}

Keep in mind, this is a guideline, and there probably is a better way to do it (There almost always is), but this should help you get started.

Also, I work with AS3 but I'm pretty sure this code carries over to AS2, but in your documentation adventures double check

rrh
07-01-2009, 12:36 AM
It all should be the same in AS2 except there is no int in as2, just Number.

I probably wouldn't derive the X and Y from the preceding entry like that, but from another source, and then I could loop from 0 to length instead of 1 to length. Based on Ransom's tutorial, he has a bunch of container movie clips, and could derive the X and Y directly from those.

bluemagica
07-01-2009, 12:40 AM
yeh logically that is quite close, just one tiny thing.... if you add same value to x and y, you are gonna end up with a diagonal, instead of the normal box arrangement!