any idea to make inventory system ?
to be able to get items, drag them and use them ?
any idea to make inventory system ?
to be able to get items, drag them and use them ?
Hi CrashHadBack.
What you asking is a very big question and quite dependent on the context of what you are making, but I will try to give you some things to think about.
1. I do not know what your items are, but we certainly need to store them somewhere, a simple list will do fine for this myItems = [];
2. You need to decide what your inventory items are. To keep things simple, I will assume an item is simply a wade scene object to which we add a type flag, we can then push these on to our list.
var item = new SceneObject();
item.type = "apple";
myItems.push(item);
3. We want to display our inventory. This is easy, we simply need to give our scene objects sprites, so our "apple" type items would have a picture of an apple or similar. These could be squares that we can arrange in a grid.
4. The user needs to be able to interact with the inventory. This is much more dependent on what you are doing so I cannot really answer accurately. A very simple system would require adding an onClick function to the item scene object, so that when the user clicks it, the item is equipped or used, maybe depending on the type flag we made. You might need a flag on your scene object that says whether or not an item is "equip_able" or "usable" to differentiate between say a sword which is equipped, and a potion which is consumed.
Overall there is not a single generic solution, there are many approaches. I suggest you look at games you like that have inventories and how the work. You could easily replicate the user interaction by adding various onMouseMove, onMouseUp, onMouseDown events to your objects.
I hope this at lest gives you a place to start, good luck!
that's very helpful
but I've 1 more questions if you please
the question is about point 3
is there a layout or something to use to put myItems objects into the scene ?
or I should loop on them and set position for each of them ?
As far as I know you have to add each object individually with wade.addSceneObject
Just looping over them with 2 for loops should be fine, and like you say just set the position there, should be fairly straight forward
Just to make sure we are on the same page, if you wanted the following layout, 2 rows 4 columns...
xxxx
xxxx
you could do
for(var i=0; i<myItems.length; i++)
{
var x = i > 3 ? (i-4)*100 : i*100; // If i is greater than 3 aka our 5th invent item, get correct x by subtracting 4 from i
var y = i > 3 ? 100 : 0; // If i is greater than 3, start next row by adding 100 to y from here on
myItems[i].setPosition(x, y);
wade.addSceneObject(myItems[i]);
}
obviously you can use your own numbers, or if you want to make it easy you could store your items in a 2d array and use 2 for loops. Given the annoying ternary operators I have to calculate x and y, a 2d array might be easier
k I'll try to do it today
thanks for help