I'm working on an inventory, and I've ran into some problems. Some code in my CollisionDetection
class doesn't work properly.
My CollisionDetection
class contains functions that involve mouse and item collision. You can pick up and place down items in your inventory, you can stack and split stackable items and you can replace items.
These is an example of the working function:
If you look closely you can see that the item that I'm holding with the mouse is drawn above the other item.
This is the example of the broken function:
I need to click twice before the item finally switches.
If you look closely you can see that the item that I'm holding with the mouse is drawn below the other item and the function doesn't work properly.
I think it has to do with the layer that the item is drawn on...
The stack/replace code (the whole function uses i
which is the item. The function is started by a for loop that loops through the item and inventory tiles lists):
Item i2;
// Searches item 2
i2 = items.Find(x => x.itemBox.X == r.X + 9 && x.itemBox.Y == r.Y + 9 && !x.clicked && x != i);
// If the item intersects another item (item 2)
if (i.itemBox.Intersects(i2.itemBox))
{
// If the item is stackable
if (i.stackable && i2.stackable)
{
// Hiding one item visually
if (!i.hide_Box_Def)
{
i2.quantity += i.quantity;
i.hide_Box_Def = true;
i.error = true;
// Bool for changing the item state (picking up/ placing down)
// But these 2 lines of code are not really used, I wrote these trying to fix this problem.
i.m_Switch2 = true;
i.watch.Start();
}
}
// When the item is not stackable -> item needs to be replaced by another item
else
{
// Item 1 (attached to the mouse)
// clicked: a bool that attaches the item to the mouse
i.clicked = false;
// itembox: collisionbox for the item
i.itemBox.X = i2.itemBox.X;
i.itemBox.Y = i2.itemBox.Y;
// Oldposition for the item
i.oldPosition.X = i.itemBox.X;
i.oldPosition.Y = i.itemBox.Y;
// info bool: do/ do not show item info
i.info = false;
// Item 2 (in inventory)
// mouse_Click_Left: bool that changes the item state (picking up/ placing down)
i2.mouse_Click_Left = true;
i2.info = true;
i2.clicked = true;
i2.itemBox.X = (int)coords.X - 15;
i2.itemBox.Y = (int)coords.Y - 15;
}
Summary:
The code works when the item that's attached to the mouse is above the other item.
The code doesn't work when the item that's attached to the mouse is below the other item.
UPDATE
I fixed the first problem, so I changed the question a bit.
Trace.WriteLine
. This way you'll be able to see what the code is doing without "pausing" the debugger. I'd put one inside each if / else block and output any state variables. Something likeTrace.WriteLine($"i: {i} i2: {i2} i.clicked: {i.clicked}");
for example. \$\endgroup\$