Creating inventory in array
Let's create some items in our inventory array.
Add to the second slot 21 coins and to the fourth 3 apples just like there:
Your item ID must be item.animationframe + 1. In this case, apple will have ID = 1, as I said before.
Here comes the hardest part of this tutotial. I promise, will be easier further %)
Now we need to visually display our inventory.
For creating visual part Im using this algorithm:
(check for full size in attached files)
But all is not as difficult as it seems to ( or not :D )
What this algorithm does:
- Automatically creates a new line for your inventory slots. If you want 5 slots in one line and 10 slots total, it will automatically split it.
This formula is probably scariest thing throughout algorithm :
Let's examine it in more detail:
X = (((inventory.curx)-(sn(a-1)))ss)+tx
- inventory.curx - current X index in a For Each loop
- sn - number of slots in one line
- a - just some variable for calculating. It is increased by 1 each time when inventory.curx MOD (remainder of division of one number by another) sn = 0
- ss - slot size (32 + border between slots (2)) = 34
- tx - my algorithm is not perfect, it is necessary to subtract some number from the X and Y coordinates if you want to get true coordinates. tx and ty are this "some number" to subtract.
Here is formula to calculate tx variable:
This algorithm is fully exposed to criticism. I will be very happy to hear your feedback to improve its performance :S
So, at first, it creates an inventory slot and sets the values of the variables according to the data in the array.
/ A small correction. Despite the fact that the inventory array starts at 0, the slot's ID we will start with 1. /
After, it checks for the object in the inventory by checking value at inventory.curx.
If its > 0, then object is exists and then it creates it.
That's why items ID are by 1 more than it's animation frame.
At the end, it checks if number of this item > 0. If it so, it also creates Text object.
It sets value item.number to number in the array and Text.text to item.number.