Copy link to clipboard
Copied
Hey guy, i want to make a game inventory and here is the file:
https://hotfile.com/dl/163389724/dc84168/inventory.fla.html
Lets say that the 2 big green rectangles are the inventories bg images, the small red are the empty item slots and the blue one is an item. I want the player to be able to transfer items from the one inventroy to another, can you help me?
Copy link to clipboard
Copied
Please explain it clearly...
You want to transfer items automatically or manually?
Copy link to clipboard
Copied
Well, you can always set up the inventories as 2 separate arrays:
Ex.
var inv1:Array = new Array(); //inventory 1
var inv2:Array = new Array(); //inventory 2
Then each array element would be a slot, and each item would be defined as a string.
Ex.
inv1 = ["open slot", "open slot", "money"];
inv2 = ["open slot', open slot", "open slot"];
Then a function of this sort would swap an item to the first open slot:
function swap(target_inv:String, target_slot:int, destination_inv:String, destination_slot:int):void
{
var item1:String = this[target_inv][target_slot];
var item2:String = this[destination_inv][destination_slot];
this[target_inv][target_slot] = item2;
this[destination_inv][destination_slot] = item1;
}
I would also make the items MovieClips, remove them from the stage, then link them for actionscript in the library; that way, you can attach the movieclips to a movieclip holder, then destroy them and re-add them whenever a swap is made.
Copy link to clipboard
Copied
Thank you, id like to transfer the items via pick-up / drop, and also if you can explane me what means " this
Copy link to clipboard
Copied
A. Transfering movieclips from library to stage.
*Note: Even when a MovieClip is deleted from the stage, it still exists until it is actually deleted from the library.
1. Go to the Library Panel, find the MovieClip you want > right click > properties > check export for actionscript > give it a class name where it says "class". This will allow you to access that movieclip from the library using the name you give it.
2. You can simply add this so the stage by writing:
var o:your_class_name = new your_class_name();
o.name = some_name;
o.x = some_x_position;
o.y = some_y_position;
some_mc_holder.addChild(o);
some_name is a name you can access that specific instance by from the stage later on by using: some_mc_holder.getChildByName(some_name);
3. However, your items will be given different class names, so you'll want to add MovieClips with a varying class name. To do this we will define a Class variable:
var c:Object = getDefinitionByName(your_class_name as a string);
var o:MovieClip = new c();
o.name = some_name;
o.x = some_x_position;
o.y = some_y_position;
some_mc_holder.addChild(o);
B. this
^ this makes absolutely no sense, and has no use what so ever.
C. Drag and Drop
*Note: this is actually quite difficult and tedious to program.
1. use the variables: mouseX and mouseY to get the position of the mouse.
2. use the event MouseEvent.MOUSE_DOWN and MouseEvent.MOUSE_UP to listen for when the mouse is being held. During this time, assign the x and y coordinates of a target object to that of the mouse. This takes a while to do right.
Copy link to clipboard
Copied
Thank you, but about the B problem i meant this: this[target_inv][target_slot];
and i likened target_inv with x and target_slot with y for alla the occations. Nm what does it mean?
Copy link to clipboard
Copied
object1.object2
object1["object2"]
^these mean the same thing, the brackets just allow you to define an object on another object dynamically.
The reason this
Copy link to clipboard
Copied
Thank you again but i still dont understand these lines:
var item1:String = this[target_inv][target_slot];
var item2:String = this[destination_inv][destination_slot];
this[target_inv][target_slot] = item2;
this[destination_inv][destination_slot] = item1;
Could you explane them?
Copy link to clipboard
Copied
this[target_inv] is an array that holds the items for the selected inventory and target_slot is the index (or item) referenced.
Ex:
target_inv = "inv1";
target_slot = 1;
so
this[target_inv][target_slot]
is the same as
this.inv1[1]
which is also the same as
inv1[1]
same thing for the destination.