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!
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: