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:
- Fetch the current state of the mouse with
Mouse.GetState()
. - Use that state and check if its
LeftButton == ButtonState.Pressed
. This means the left button on the mouse is pressed down. - 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 (withRectangle.Contains
). - Set some sort of flag indicating that the sprite is grabbed.
- Each frame, if this flag is true, set the sprite to the mouse’s position.
- 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