PDA

View Full Version : Creating Game Inventory



descalabro
February 13th, 2009, 05:27 PM
AS2/CS3

I'm using drag and drop and testing for my game inventory, but the problem is the way to manage the items already inside it.

My system works like this:
The dummy goes near the object -> the player presses ENTER -> the object's scale is changed to fit the inventory squares -> its position is changed to the first free square of the inventory -> player moves the dummy near the target object -> player drags the item from the inventory to the target object.

Items can be used only if they are dragged from the inventory to the object which will be activated by it, and the dummy must be near the target object at that moment.

I was able to actually do this using hitTest and _droptarget, but that's not the problem. The problem is that the way I'm doing it requires several huge switch(case), and I end up having about 1000 lines of code per each object (since I can't test my inventory squares vs anyOtherMovieClip).

Right now I have 51 squares on my inventory (3 lines of 17 squares each), each one has a Boolean variable associated with it: _global.menu1, _global.menu2, ..., _global.menu51 (I used global variables since I must initialize them outside the button which inside the dragged movieClip (the object)).

So, this is what I do:



onClipEvent (enterFrame) { // entering frame of car_MC, which is my object //
var xpos:Number; // declaring variables for later x and y values of car_MC //
var ypos:Number;
if (this, hitTest(_root.dummy_MC)) { //the dummy must close to objects to pick them//
if (Key.isDown(Key.UP)) { // i used key UP for testing only, final game will use ENTER //
this._xscale = 40; //scales the car_MC object to fit inside inventory squares //
this._yscale = 40;
for (counter=0; counter<52; ++counter) {
switch (counter) {
case 1 :
xpos = 398;
ypos = 636;
if (_global.menu1 == false) { // tests if square1 is free //
_global.menu1 = true; // sets first square to true, thus making it used //
counter = 52; //ends the cicle//
}
break;
case 2:
..
...
//my inventory is organized on 3 lines of 17 squares each.
So when I reach case 18 and 35, the value of ypos is increased //
........
....
..
break;
case 51 : // it goes up to case 51 //
xpos = 1255.55;
if (_global.menu51 == false) {
_global.menu51 = true;
counter = 52;
}
break;
}
}
this._x = xpos; // places the car_MC on the first empty square found before//
this._y = ypos;
car = 1; // sets variable car at value 1 - This means it is in the inventory //
}
}
}


Now, it is obvious this takes a lot of lines of code, and I know it could be easily done with something like:



for (counter=0; counter<52; ++counter) {
if (_global.menu[counter] == false) {
xpos += 53.6;
square[counter] = true;
counter = 52;
}
}
counter = 0;
this._x = xpos; // places the car_MC on the first empty square found before//
this._y = ypos;
car = 1; // sets variable car at value 1 - This means it is in the inventory //



I know it has to test the counter twice to increase the value of ypos, but this is the basic idea.

What is missing to make it work?

I can't get it to test for if (_global.menu[counter] == false).

what's the correct way to "attach" the var counter to the global var menu so the program reads _global.menu1, _global.menu2, ..., _global.menu51 ?

The part where I use drag and drop comes later, but it's the same basic idea, which now is making me use more than 700 lines of code with endless switch(case) actions.

Thanks for any help.

Favardin
February 14th, 2009, 01:29 PM
If I remember correctly (I've moved to as2/3 quite some time ago...) you can access these variables like this:



_global["menu"+counter] = ...


Otherwise I would suggest that you could simply use an array with 51 entries, that might be even easier...

descalabro
February 14th, 2009, 10:19 PM
If I remember correctly (I've moved to as2/3 quite some time ago...) you can access these variables like this:



_global["menu"+counter] = ...


Otherwise I would suggest that you could simply use an array with 51 entries, that might be even easier...

Thank you for your reply.

I apreciate your suggestions, I will try them both when I get the time.

For now, If have several questions for you, if you have the time to answer:

1) I'm still a novice using ActionScript (and programing in general), and your reply implies I'm using AS1.
I'm writing this code using an AS2 document from Flash CS3, so I'm a bit confused. What code am I using?

2) You suggest using an array, and I'm willing to learn how to do that but, would you say the array will save me time, lines of code and, more important, resources?

3) As you can see, each square of my menu has 3 elements associated with it: a movieClip named menu(1 to 51)_MC, a Boolean variable named menu(1 to 51) and an x, y position.
The third element, x, y position, can be excluded if I use the relative menu_MC x, y position to place my items, but the other two must be there.
My question: when I create my array, can I insert all these elements to it, or do I have to create an array for each? I'm just asking if this is possible, I don't mean to have all my homework done by the readers of this forum. :)

4) Would I get any advantage by using AS3 (after learning) to make my game, or is it a question of preference?

Thanks for your time.

bluemagica
February 15th, 2009, 06:25 AM
Here's an old example i made a long time ago! ask me if you don't understand something!

1)Using as3 is not a matter of preference, it is much better and powerful than as2! You will understand once you actually learn it!

2) Mention what you are using when asking a question, like for your case, write [As2/Cs3] that way we will know what to use to help you!

descalabro
February 15th, 2009, 08:58 AM
Hi.

Thanks a lot, but when I press that 'inventory_example.zip' what I get are these PHP attachments and notepad files named 1234693343, 1234693343_1, 1234693343_2, etc. Are these the correct files or should it really be a zip?

Thanks.

nathan99
February 15th, 2009, 10:53 AM
http://n99creations.com/vieweg.php?id=19

Favardin
February 15th, 2009, 01:04 PM
1) I'm still a novice using ActionScript (and programing in general), and your reply implies I'm using AS1.
I'm writing this code using an AS2 document from Flash CS3, so I'm a bit confused. What code am I using?


You are using AS2, sorry if I confused you.



2) You suggest using an array, and I'm willing to learn how to do that but, would you say the array will save me time, lines of code and, more important, resources?


First of all: don't worry about resources. Worry about writing correct and understandable code first -- you can optimize it later on (thought I bet you won't need to). Using an array will be an advantage, you can do a lot of stuff you cannot do by iterating through variables like above (i.e. copy a part of it, switch elements etc. etc.).



3) As you can see, each square of my menu has 3 elements associated with it: a movieClip named menu(1 to 51)_MC, a Boolean variable named menu(1 to 51) and an x, y position.
The third element, x, y position, can be excluded if I use the relative menu_MC x, y position to place my items, but the other two must be there.
My question: when I create my array, can I insert all these elements to it, or do I have to create an array for each? I'm just asking if this is possible, I don't mean to have all my homework done by the readers of this forum. :)


If you want to reuse your current structure: arrays can contain everything and you can wrap multiple things together with objects.



4) Would I get any advantage by using AS3 (after learning) to make my game, or is it a question of preference?


Oh, yes! But don't expect it to be easy, you should learn about object oriented programming first -- otherwise you will feel pretty lost.

descalabro
February 15th, 2009, 09:42 PM
http://n99creations.com/vieweg.php?id=19

Well, thanks. This seems to be a simple version of what I'm trying to do.

At this point I can do this already, but the inventory I'm trying to do uses drag and drop so the player is able to organize the items as he wishes, and that implies several checks for emtpy slots. Also, I've notice that the yellow items on your example will crop together as one once you drop them on the same place, and this is one of the many details I wish my inventory to complement.

Still, I thank you for this example and would like to take a look at the code if possible. I'm waiting for my register e-mail.

descalabro
February 15th, 2009, 10:18 PM
Thanks a lot for the info.

At this point I have made a good evolution: I had siwtch actions in a total of 1400 lines of code and now I have a full working inventory with 130 lines only(including many trace actions).

I'm not yet using arrays but I'm very happy with the result I got so far, because using counters to check my inventory slots is the basis to work with the array elements later as I understand.

I have one question, though:



If you want to reuse your current structure: arrays can contain everything and you can wrap multiple things together with objects.


I suppose there are other structures, but won't I need to have these 3 elements (a movieClip to drop my items into (and organize my items using drag and drop), a Boolean variable to tell me if the slot is empty or used and and x, y position to place my item inside the slot) to build a menu the way I mentioned?

If I had just a few items, I could make an array with all my inventory slots and hitTest those slots against my items, but since I got a lot of items, this simply isn't a good way to deal with it. As I understand it, it works better if I test my items against the menu and not the other way around (and I mean "test" in any possible way, not just hitTest).

What do you think?

Maybe I will get other ideas once I get to understand and work with arrays.

cjke.7777
February 16th, 2009, 06:32 AM
G'day,

The way I approached inventory-like problems in AS2 was with arrays of objects. This way each item in your inventory could have its own properties. (these properties might include what it could interact with - i.e. the hittest). An example might be something like this:




class item {
public var name:String; // The name of the item
public var description:String; // What is this item
public var quantity:Number; // How many of this item you are holding
public var usedWith:String; // A list of items that this item can interact with
}

// -- Somewhere else in the code - where you actually setup the items -- //
var myItems:Array;

var tempItem:item = new item();
tempItem.name = "Potion";
tempItem.description = "Heals HP";
tempItem.quantity = 3;
tempItem.usedWith = goodGuys;

myItems.push(tempItem);

tempItem:item = new item();
tempItem.name = "Hi Potion";
tempItem.description = "Heals alot of HP";
tempItem.quantity = 1;
tempItem.usedWith = goodGuys;

myItems.push(tempItem);

Don't be daunted by the code - it might seem complicated but in the long run some sort of setup like this will make things easier.

Ask follow up questions if needed

descalabro
February 16th, 2009, 08:01 AM
Hi.

Thanks a lot, cjke.7777, this looks pretty good.
I'll give you some more feedback as soon as I get comfortable using arrays. :)

cjke.7777
February 16th, 2009, 05:57 PM
Sure - not a problem. It would be very useful for you to understand Arrays quite clearly, it is a fundamental concept in programming. If you have any questions about arrays specifically just ask