Hello.
I'm fairly new to xna/c# and i'm starting to get a grasp on the whole game mechanics.
But i'ev run into an issue.
I want my sprite to move across the screen to my desired destination.
I can almost get this to work.
what i have so far:
i click somewhere on the screen, the sprite moves towards the mouse position, BUT if i move the mouse before the sprite reaches it's location it will follow the mouse. when it reaches the location, it stops and i can move the mouse.
what i want:
click anywhere on screen, sprite moves towards position while i can freely move mouse around, even click a new position and the sprite goes to that position instead.
here is my code so far:
MouseState mouseState = Mouse.GetState(); //get the current mouse state
mousePosition = new Vector2(mouseState.X, mouseState.Y); //get the target (the mouse cursor)
direction = (Vector2)mousePosition - Position; //get the direction from arrow to cursor
direction.Normalize();
Vector2 target;
if (mouseState.LeftButton == ButtonState.Released && oldState.LeftButton == ButtonState.Pressed)
{
target = mousePosition;
move = true;
}
target = mousePosition; //this is the line that causes issues, but i can't remove it either.
if (Vector2.Distance(target, Position) > 3 && move.Equals(true))
{
Position += direction * 5;
}
else
{
move = false;
}
oldState = mouseState; // this reassigns the old state so that it is ready for next time