I need some help setting this up?

On my previous post i had spoken about an issue i was having, i have since restarted my project, as the issues were coming from some serious mess ups in the code. Currently i need help with clicking and dragging the sprite. As of now i have it where when i click anywhere moves to my mouse and i can drag it around. I want it to be where i have to click the sprite. I would also like it to be where my it stays where the mouse clicked and where it doesnt move to the top left corner. Heres my code.

public class Game1 : Game
        {
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;
    InputManager input;

    private Player player;
    private List<Component> components;

    Rectangle playerRect;
    Point mousePoint;
    
    bool drag;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        base.Initialize();
        Utility.Game = this;
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);

        input = new InputManager(Utility.Game);
        input.Setup();

        player = new Player(Content.Load<Texture2D>("PlaceHolderPlayer"));

        playerRect = new Rectangle(player.Rectangle.X, player.Rectangle.Y, player.Rectangle.Width, player.Rectangle.Height);

        mousePoint = new Point(input.Mouse.CurrentCursorPosition.X, input.Mouse.CurrentCursorPosition.Y);

        components = new List<Component>()
        {
            player
        };
    }

    protected override void Update(GameTime gameTime)
    {
        input.Update(gameTime);
        
        if(playerRect.Contains(mousePoint) && input.Mouse.IsPressed(MouseButton.LeftButton))
        {
            drag = true;
        }
        else
        {
            drag = false;
        }

        if(drag == true)
        {
            player.X = input.Mouse.CurrentCursorPosition.X;
            player.Y = input.Mouse.CurrentCursorPosition.Y;
        }

        base.Update(gameTime);

        foreach (var component in components)
            component.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();
        foreach (var component in components)
            component.Draw(gameTime, _spriteBatch);
        _spriteBatch.End();

        base.Draw(gameTime);
    }
}

One issue you have is you’re not updating the playerRect variable after you move the player. So once you’re outside the bounds of that rect, the dragging will stop working.

EDIT:
I would move the playerRect to the Player object. Then you can update that anytime X or Y, Width or Height is updated. Alternatively you can make it a read-only property on the object that returns a new rec of the X,Y, W,H…

With this setup, I would expect the player dragging code to be done inside the player component. As for the click-drag logic itself, it might help to consider the particular cases you need to look for. Write 'em down in bullet points and see where that takes you :slight_smile: