Item System

70 views
Skip to first unread message

xhunterko

unread,
Nov 2, 2015, 10:17:25 PM11/2/15
to HaxeFlixel
Hi!

So, my I thought of something neat, and like every time I thin of something neat, I run into a code problem, and can't fix it cause I don't know how. I actually have 2 problems that need fixing and would like your help with.

The idea is this, in my game, when you kill an enemy, it might drop an item. When you walk over that item, you collect it into your inventory. When it's in your inventory, and you mouse over it, there's a text that tells you what to do, and you can either click it to use it or drag it over to a crafting box and combine it with items to make a new item. I thought, sounds simple, and I've done some of it before, this shouldn't be a problem. Ha.

I've got the adding to inventory bit partially working. I say partially because I know it should be more robust than it is. It should be added to an array, and I need the array to be split into 2 rows of 14, so at the 14th spot in the first row, it starts adding to the second row. I don't know how to do this either and thought this would be all I needed to ask. Ha.

I've got the text display information when mouse over part working. I got the click on item, something happens working with the test item and the item in the inventory is used up. The problem is it doesn't go away completely and I can still use the item and get display info text but the item isn't shown and it's been fairly frustrating.

Any ideas on how to fix this?


Code:

http://pastebin.com/5abQbmYh

That's a lot of code to sift through, sorry. But I don't know how else to ask and how else to fix this. Any help would be appreciated! Or if you've done something or seen an item inventory before done in haxeflixel and it's open source could you point me towards it please? I've looked and not found something like this but maybe another pair of eyes will help find something. Thanks for taking a look!

MegaLeon

unread,
Nov 3, 2015, 10:50:10 AM11/3/15
to HaxeFlixel
Hey, my suggestion is to use trace() to print messages into the console at every important step of your events, might be useful to see at which point your system breaks.

Sam Bellman

unread,
Nov 4, 2015, 4:17:56 AM11/4/15
to HaxeFlixel
First things first, adding the icons. I personally would simple add and position all the icons you need first, then hide the one you aren't using. For this you could use a function something like this: (the variables would be private in the class declaration, the for loop would be in your new function.)
var itemSpacingH:Int = 20;
var itemSpacingV:Int = 20;
var numItemsInRow:Int = 14;
var itemIcons:Array<CraftingItemIcon> = [];


for(i in 0...yourArray.length)
{
var itemIcon = new CraftingItemIcon();
itemIcon.setPosition(i % numItemsInRow * itemSpacingH, Math.floor(i / numItemsInRow) * itemSpacingV);
add(itemIcon);
itemIcons.push(itemIcon);
itemIcon.visible = false;
}

Then you would have a function in CraftHUD to set the inventory that looked like this. We're returning a value so that we know if there was enough space for the item - if not we could show a message.

public function AddNewIcon(itemClass:Int):Bool
{
for (i in 0...itemIcons.length)
{
if (itemIcons[i].ItemClass < 0)
{
itemIcons[i].setItemClass(itemClass);
return true;
}
}
return false;
}

then add functions in CraftingItemIcon that looked like this:

public function setItemClass(itemClass:Int):Void
{
visible = itemClass >= 0;
var changed:Bool = itemClass != ItemClass;
ItemClass = itemClass;
if (changed)
{
//set the correct item frame
}
}
public function removeItem():Void
{
ItemClass = -1;
visible = false;
}

The when the user clicks on the item call removeItem() instead of kill and remove.

Also, calling CraftStation.Identify; doesn't actually do anything. If you want Identify to change, set it in the IdentifyIcon function.


On Tuesday, 3 November 2015 03:17:25 UTC, xhunterko wrote:

xhunterko

unread,
Nov 11, 2015, 9:46:33 PM11/11/15
to HaxeFlixel
Oh. Oh thank you!

I forgot about this figuring not many would respond and eventually got something working as a result.

Still this should prove very helpful! Thank you!
Reply all
Reply to author
Forward
0 new messages