Jump to content
  • Advertisement
Sign in to follow this  

Mouse Help!

This topic is 2033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I am new to programming and I need help with my current project.


Currently I am working on a simple space game that I hope one day will be bigger than it is right now, but I have one bump in the road from getting me there.


here is the current method I am working with but I want to make the sprite follow my mouse on mouse click.






using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace FirstGame
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D SpaceShip;
        Vector2 SpaceShipPos = Vector2.Zero;
        float rotation;
        float speed;
        Vector2 mousePosition;
        MouseState mouseState = Mouse.GetState();
        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        protected override void Initialize()
        protected override void LoadContent()
            spriteBatch = new SpriteBatch(GraphicsDevice);
            SpaceShip = Content.Load<Texture2D>("space_ship");
            SpaceShipPos = new Vector2(200, 200);
            rotation = 0;
            speed = 3;
        protected override void UnloadContent()
        protected override void Update(GameTime gameTime)
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.LeftControl))
                MouseState mouse = Mouse.GetState();
                mousePosition = new Vector2(mouse.X + 5, mouse.Y + 5);
                Vector2 direction = mousePosition - SpaceShipPos;
                rotation = (float)Math.Atan2(
                SpaceShipPos += direction * speed;
            this.IsMouseVisible = true;
        protected override void Draw(GameTime gameTime)
                 new Vector2(
                     SpaceShip.Width / 2,
                     SpaceShip.Height / 2),





Also if you could help me with another thing. Every time I stop my mouse from moving and my sprite moves into it. The sprite goes all crazy. Please help me and thank you!

Share this post

Link to post
Share on other sites

If you want the sprite to follow the mouse when you're holding the left button down, it would be:


if(Mouse.GetState().LeftButton == ButtonState.Pressed)



The crazy movement would be because you're moving the sprite even if it's less than 3 distance away from the mouse, so it's going back and forth around the mouse position. Just add a distance check around where you increment the position.

if (Vector2.Distance(SpaceShipPos, mousePosition) > speed)
        SpaceShipPos += direction * speed;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!