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);
}
}