0
\$\begingroup\$

Well I've read the related questions-answers but still cant find a way to move my champion to the mouse position after a right-button mouse-click. I use this code at the top:

float speed = (float)1/3;

And this is in my void Update:

  //check if right mouse button is clicked
        if (mouse.RightButton == ButtonState.Released && previousButtonState == ButtonState.Pressed)
        {
            // gets the position of the mouse in mousePosition
            mousePosition = new Vector2(mouse.X, mouse.Y);

            //gets the current position of champion (the drawRectangle)
            currentChampionPosition = new Vector2(drawRectangle.X, drawRectangle.Y);

            // move champion to mouse position:
            //handles the case when the mouse position is really close to current position
            if (Math.Abs(currentChampionPosition.X - mousePosition.X) <= speed &&
                Math.Abs(currentChampionPosition.Y - mousePosition.Y) <= speed)
            {
                drawRectangle.X = (int)mousePosition.X;
                drawRectangle.Y = (int)mousePosition.Y;
            }
            else if (currentChampionPosition != mousePosition)
            {
                drawRectangle.X += (int)((mousePosition.X - currentChampionPosition.X) * speed);
                drawRectangle.Y += (int)((mousePosition.Y - currentChampionPosition.Y) * speed);
            }
        }
        previousButtonState = mouse.RightButton;

What that code does at the moment is on a click it brings the sprite 1/3 of the distance to the mouse but only once. How do I make it move consistently all the time? It seems I am not updating the sprite at all.

EDIT I added the Vector2 as Nick said and with speed changed to 50 it should be OK. I tried it with if ButtonState.Pressed and it works while pressing the button. Thanks. However I wanted it to start moving when single mouse clicked. It should be moving until reaches the mousePosition.

The Edit of Nick's post says to create another Vector2, But I already have the one called mousePosition. Not sure how to use another one.

            //gets a Vector2 direction to move *by Nick Wilson
            Vector2 direction = mousePosition - currentChampionPosition;

            //make the direction vector a unit vector
            direction.Normalize();

            //multiply with speed (number of pixels)
            direction *= speed;

            // move champion to mouse position
            if (currentChampionPosition != mousePosition)
            {
                drawRectangle.X += (int)(direction.X);
                drawRectangle.Y += (int)(direction.Y);
            }
        }
        previousButtonState = mouse.RightButton;
\$\endgroup\$
0
\$\begingroup\$

In your update code:

drawRectangle.X += (int)((mousePosition.X - currentChampionPosition.X) * speed);
drawRectangle.Y += (int)((mousePosition.Y - currentChampionPosition.Y) * speed);

Since the speed is 1/3, you're only moving it 1/3 of the distance to the mouse's position.

If you want it to immediately jump to that position, set speed to 1.

If you want it to slowly move to that position, you'll want speed to be something smaller (unless you want the champion to move to whatever position the user clicked in in three frames). However, it will still take equal time to get to any position with your current implementation.

You'll want to get a Vector2 that goes from the champion position to the mouse position. IE:

    Vector2 direction = mousePosition - currentChampionPosition;

Then you'll want to make the player move an equal amount along that vector. You can do this my normalizing the vector, and then multiplying it by your speed. (In this case, the vector will be multiplying 1 by the speed, so you'll want to put the speed as how many pixels you want the player to move every frame.)

    direction.Normalize();
    direction *= speed;

    drawRectangle.X += (int)(direction.X);
    drawRectangle.Y += (int)(direction.Y);

You'll also want to check if doing this will move the player too far past the mouse position, so that you don't get back-and-forth jittering when the player is near the destination.

Edit:

It seems that you're only applying this once, when the user clicks. Save a Vector2 for the position that you want the player to move to, and on every frame where the player needs to move, apply your move code.

\$\endgroup\$
  • \$\begingroup\$ Yes, thanks. The new Vector2 direction works but still the buttonPressed && Released messes up things. How to make it start moving when the button is pressed and released? Then it should move to the end location on its own. I just cant understand your EDIT. But I have the feeling it contains the solution :-) \$\endgroup\$ – I.Hristov Oct 20 '13 at 21:07
  • \$\begingroup\$ @I.Hristov Because you have all of that code wrapped in that if, all of the code within will only fire once (on the frame that the user clicks.) You want your position to be updated more than once, but you also want the player only to move to where the user clicked. You should save a Vector2 of the mouse position when the user clicks, and on every frame where that isn't null, have the player move toward it. When the player arrives, set it to null. That way, the user can change their destination while the player is on the move. \$\endgroup\$ – l'- Oct 23 '13 at 0:12
0
\$\begingroup\$

What are you doing this in?

The only thing that comes to mind looking at your code is the way you are checking the state of the button. Don't store the past click if you don't have to. You should be able to get the same result by doing something like mouse.RightButton == ButtonState.Down

Also, there is not need to do your check of if the mouse is really close to the character, just handle all in one block. Make sure to multiply your movement by delta time as well.

\$\endgroup\$
  • \$\begingroup\$ I guess that is what is wrong with my code after fixing the moving part. I just want to make it start moving when right button is clicked and then to continue on its own. Still cant do it curses, \$\endgroup\$ – I.Hristov Oct 20 '13 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.