Drag and Drop a single Sprite

Hello everyone, im new in monogame.
Im trying to drag and drop a single sprite with the mouse(the mouse is not a sprite, is the mouse of the desktop).
Im trying to do something like Pegman in google maps.

Welcome! What you want to do is rather simple in MonoGame:

  1. Fetch the current state of the mouse with Mouse.GetState().
  2. Use that state and check if its LeftButton == ButtonState.Pressed. This means the left button on the mouse is pressed down.
  3. Check if the position of the mouse on click collides with the sprite. To do this, you can create a Rectangle around the sprite and check if the mouse’s position is contained inside (with Rectangle.Contains).
  4. Set some sort of flag indicating that the sprite is grabbed.
  5. Each frame, if this flag is true, set the sprite to the mouse’s position.
  6. When the mouse is released (the mouse state’s LeftButton == ButtonState.Released), you can handle your logic for dropping the sprite.

I hope that helps!

3 Likes