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.