Ali Salehi
Ali Salehi

Reputation: 5

XNA : Is there a way to prevent other items(sprites) when dragging?

I was looking for a way to block other object from the same class to be dragged, when I begin dragging one and hover another it id dragged as well...

Here is my main item definition,

class DrawableObject 
{
public Rectangle RectObject

public void Update(GameTime gameTime, MouseState ms)
        {
            if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
                if (RectObject.Intersects(new Rectangle(ms.X, ms.Y, 0, 0)))
                    Dragged = true;
            else
                Dragged = false;
                if (Dragged)
                    RectObject = new Rectangle(ms.X - RectObject.Width / 2, ms.Y - RectObject.Height / 2, RectObject.Width, RectObject.Height);               
        }
}

The issue will be more tight as I will be creating more than 50 instance of this class.

Upvotes: 0

Views: 80

Answers (1)

Pragmateek
Pragmateek

Reputation: 13364

You could keep a "global" property with the current object being dragged.

Then before accepting dragging each object will check if another is not yet dragged:

public void Update(GameTime gameTime, MouseState ms)
{
    if (currentObject != null && this != currentObject) return;

    if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
    {
        if (RectObject.Intersects(new Rectangle(ms.X, ms.Y, 0, 0)))
        {
            Dragged = true;
            currentObject = this;
        }
    }
    else
    ...
}

Upvotes: 1

Related Questions