0

i wanna make a sprite move to the mouseclick (like in most strategie games). But i made it only while i press and hold the left mouse button so far. When i change the if-clauses to while loops, the sprite goes immediately to the mouse position. Can someone help me? Is there maybe an easier way to make a sprite move to a specific position by clicking the mouse?

best regards, alex

        mCurrentMouseState = Mouse.GetState();

        if (mCurrentMouseState.LeftButton == ButtonState.Pressed)
            mDestination = new Vector2(mCurrentMouseState.X, mCurrentMouseState.Y);


        Vector2 direction = Vector2.Normalize(mDestination - mPosition);

        mPosition += direction * (float) gameTime.ElapsedGameTime.TotalSeconds * mSpeed;

        if (Vector2.Distance(mPosition, mDestination) < 1)
            direction = Vector2.Zero;
Sancho
  • 1
  • 2

1 Answers1

0

Your code only moves the sprite one pixel once per game tick. If you want to move it instantly just use :

if(mCurrentMouseState.LeftButton == ButtonState.Pressed)
{
      mPositionBat = mCurrentMouseState;
}
Mormund
  • 328
  • 3
  • 8
  • thats of course smarter than my code. But my main problem is that i want the sprite go to the MouseState even when I release the button. – Sancho May 02 '16 at 22:05
  • I'm not quite sure what you are trying to do. If you simply want something like I a mouse cursor just use `mPositionBat = mCurrentMouseState;` without any condition. _Also as a side note. If this is the first game you're trying to make I'd advise you against making a strategy game and instead try something easier like Pong or Tetris first._ – Mormund May 03 '16 at 11:14
  • mPositionBat = mCurrentMouseState; doesn't work because mPositionBat is a Vector2 and mCurrentMouseState is a MouseState. I am studying right now and my task is to make a sprite move to the point where you click your mouse. When i make mPositionBat.X = mCurrentMouseState.X;, the sprite is not moving slowly to the direction but jumps immediately to the point where i clicked the mouse. – Sancho May 03 '16 at 12:38
  • Then you can use `mCurrentMouseState.Position.ToVector2()` – Mormund May 03 '16 at 14:37
  • I made something different, I edited the Code. Now the sprite moves to the postiton where the mouse clicked, but it starts also with moving even though I haven't pressed any button. – Sancho May 03 '16 at 15:03