0
\$\begingroup\$

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:

enter image description here

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:

enter image description here

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Honestly I think your best bet here is to attach the debugger and step through some trials. I know this is very hard when you're debugging interactions. You can write code that attempts to detect the correct condition of your inputs, and break when those conditions aren't met. You might try writing a little bit of automation to repeat the inputs that cause these error states. Break when the bad state is entered, and check the values of your variables. \$\endgroup\$
    – House
    May 5, 2016 at 22:02
  • \$\begingroup\$ It's probably easier to write messages to the Visual Studio Output Window by using 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 like Trace.WriteLine($"i: {i} i2: {i2} i.clicked: {i.clicked}"); for example. \$\endgroup\$ May 6, 2016 at 1:57
  • \$\begingroup\$ @craftworkgames yeah I use this method, but I just can't figure out why that the layer of the item causes the problem. \$\endgroup\$
    – Jelle
    May 6, 2016 at 12:14
  • \$\begingroup\$ @Jelle When you found the solution yourself, then please write an own answer and accept it. This 1. helps others who have the same problem and 2. shows everyone at first glance that the question is solved. \$\endgroup\$
    – Philipp
    May 6, 2016 at 13:08
  • \$\begingroup\$ @Philipp I do this but the second problem isn't solved yet, so the question is not solved. update: I edited the question so it's more clear. \$\endgroup\$
    – Jelle
    May 6, 2016 at 13:12

1 Answer 1

0
\$\begingroup\$

I found the solution to my problems. I found out that some parts of my code were executed twice when an item was on a layer below the item in the inventory.

So I used some switches to prevent that the code was executed twice.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .