开发者

How to simplify stopping movement of image when within threshold of target

开发者 https://www.devze.com 2022-12-17 01:45 出处:网络
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.

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", "");
    开发者_JAVA百科            }
            }
        }

    }

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:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消