# Mouse Drag And Drop Problem

Page 1 of 1

## 1 Replies - 972 Views - Last Post: 07 September 2012 - 11:36 PM

### #1 matanl1234

Reputation: 0
• Posts: 22
• Joined: 01-November 11

# Mouse Drag And Drop Problem

Posted 22 June 2012 - 05:44 AM

Hello, I used the next code to drag and drop a sprite, but when I move the mouse too quickly it gets out of the texture and the dragging stops (the ms,oldMS,pos,org,tex are defined earlier):
```public void Move()
{
Vector2 realPos = pos - org;
bool inTexture = ms.X >= realPos.X && ms.X <= (realPos.X + tex.Width) &&
ms.Y >= realPos.Y && ms.Y <= (realPos.Y + tex.Height);

if (oldMS.LeftButton == ButtonState.Pressed && inTexture)
{
int xDifference = ms.X - oldMS.X;
int yDifference = Y - oldMS.Y;

pos.X += xDifference;
pos.Y += yDifference;
}
oldMS = ms;
}

```

thanks for any help.

This post has been edited by matanl1234: 22 June 2012 - 05:45 AM

Is This A Good Question/Topic? 0

## Replies To: Mouse Drag And Drop Problem

### #2 LiberLogic969

Reputation: 34
• Posts: 85
• Joined: 03-September 12

## Re: Mouse Drag And Drop Problem

Posted 07 September 2012 - 11:36 PM

Try something like this.

```
//Field
bool isDragAndDropping;
Sprite spriteBeingMoved;

public void Update(GameTime gameTime)
{
for (int i = 0; i < Sprites.Count; i++)
{
Vector2 realPos = Sprites[i].pos - Sprites[i].org;
if (oldMS.LeftButton == ButtonState.Pressed)
{
if (ms.X >= realPos.X && ms.X <= (realPos.X + Sprites[i].tex.Width) &&
ms.Y >= realPos.Y && ms.Y <= (realPos.Y + Sprites[i].tex.Height)
{
isDragAndDropping = true;
spriteBeingMoved = Sprites[i];
}
}
}
if (isDragAndDropping)
{
int xDifference = ms.X - oldMS.X;
int yDifference = Y - oldMS.Y;
spriteBeingMoved.pos.X += xDifference;
spriteBeingMoved.pos.Y += yDifference;

if (oldMS.LeftButton == ButtonState.Released)
{
isDragAndDropping = false;
}
}
}

```

Hope this helps.