Question

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!

Was it helpful?

Solution

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]);
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top