views:

29

answers:

2

I've already tried to explain what I'm trying to do to others, and failed horribly. Therefore, if you will excuse me, I'll just show you the code and attempt to explain a little.

        if (MovePetMoving)
        {
            if (MovePetSlope[0] > 0)
            {
                if (MovePetSlope[1] > 0 && Convert.ToDouble(pictureBoxPet.Location.X) + MovePetSlope[0] <= MovePetTarget[0] && Convert.ToDouble(pictureBoxPet.Location.Y) + MovePetSlope[1] <= MovePetTarget[1])
                {
                    MovePetWorker(pictureBoxPet, pictureBoxPet.Location.X + MovePetSlope[0], pictureBoxPet.Location.Y + MovePetSlope[1]);
                    //MsgBox("MovePetSlope[0] > 0 and MovePetSlope[1] > 0", "");
                }
                else if (MovePetSlope[1] < 0 && Convert.ToDouble(pictureBoxPet.Location.X) + MovePetSlope[0] <= MovePetTarget[0] && Convert.ToDouble(pictureBoxPet.Location.Y) + MovePetSlope[1] >= MovePetTarget[1])
                {
                    MovePetWorker(pictureBoxPet, pictureBoxPet.Location.X + MovePetSlope[0], pictureBoxPet.Location.Y + MovePetSlope[1]);
                    //MsgBox("MovePetSlope[0] > 0 and MovePetSlope[1] < 0", "");
                }
                else
                {
                    MovePetMoving = false;
                    //MsgBox("Error", "");
                }
            }
            else if (MovePetSlope[0] < 0)
            {
                if (MovePetSlope[1] > 0 && Convert.ToDouble(pictureBoxPet.Location.X) + MovePetSlope[0] >= MovePetTarget[0] && Convert.ToDouble(pictureBoxPet.Location.Y) + MovePetSlope[1] <= MovePetTarget[1])
                {
                    MovePetWorker(pictureBoxPet, pictureBoxPet.Location.X + MovePetSlope[0], pictureBoxPet.Location.Y + MovePetSlope[1]);
                    //MsgBox("MovePetSlope[0] < 0 and MovePetSlope[1] > 0", "");
                }
                else if (MovePetSlope[1] < 0 && Convert.ToDouble(pictureBoxPet.Location.X) + MovePetSlope[0] >= MovePetTarget[0] && Convert.ToDouble(pictureBoxPet.Location.Y) + MovePetSlope[1] >= MovePetTarget[1])
                {
                    MovePetWorker(pictureBoxPet, pictureBoxPet.Location.X + MovePetSlope[0], pictureBoxPet.Location.Y + MovePetSlope[1]);
                    //MsgBox("MovePetSlope[0] < 0 and MovePetSlope[1] < 0" + Convert.ToString(pictureBoxPet.Location.X) + MovePetSlope[0] + MovePetTarget[0], "");
                }
                else
                {
                    MovePetMoving = false;
                    //MsgBox("Error", "");
                }
            }
        }

    }

There it is. If you're wondering about all the references to "pet" are, I'm making a tamogotchi (or however you spell it) like game for my little little sister.

The problem I have is that the value of MovePetSlope[1] or [0] can either be positive or negative. I've come up with some comparisons that work for positive values, but none for negative values. I believe that in it's current state, it doesn't work at all.

Any help would be greatly appreciated.

Thanks in advance!

+1  A: 

Try using Math.Abs to simplify your comparisons. In general the pet should keep moving until Math.Abs(pictureBoxPet.Location.X-MovePetTarget[0]) < Math.Abs(MovePetSlope[0]) and analogously for Y and 1. You should end up with much simpler code.

If your pet is moving directly towards the target, this should do the trick:

if (MovePetMoving)
{
    if (Math.Abs(pictureBoxPet.Location.X-MovePetTarget[0]) < Math.Abs(MovePetSlope[0]))
        MovePetMoving = false;
    else
        MovePetWorker(pictureBoxPet, pictureBoxPet.Location.X + MovePetSlope[0], pictureBoxPet.Location.Y + MovePetSlope[1]);
}
Eric Mickelsen
Thanks Mick, I'll try that out right now. I'll look up "refractor" also. EDIT: haha, refractor is to simplify. :)
Jonathan Chan
It just means to rewrite the same code in a way that is easier to look at and maintain, or is just more elegant or useful.
Eric Mickelsen
It worked! I just had to change the less than sign to a greater than sign, but that's probably because I wasn't clear in explaining what the values would typically be.Thanks!
Jonathan Chan
+1  A: 

A different strategy :

  1. define a variable of type Rectangle that holds the targets bounds in screen co-ordinates : increase the bounds as necessary according to some threshold variable you define.

  2. define MouseUp and MouseDown event handlers for the PictureBox

  3. define a boolean variable (in form scope) that is set to true when the mouse goes down (in the MouseDown Event Handler) on the PictureBox, and false in the MouseUp handler for the PictureBox.

  4. define a MouseMove Event handler for the PictureBox in which :

    a. if the boolean variable is true (mouse is down)

    1. use the Rectangle.IntersectsWith method to see if the current bounds of the PictureBox overlaps the target bounds rectangle. MSDN Rectangle.IntersectsWith : if it does you know you can stop.

imho, using this stragety you can write greatly simplified code.

BillW
Thanks BillW, I will look into this also. Though I'm not using a mouse, "strategy tips" #s 1 and 4b will work for me.Thanks!
Jonathan Chan