Jump to content
  • Advertisement
Sign in to follow this  
Tysonclyde

Mouse Help!

This topic is 1860 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()
        {
          
 
            base.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)
                this.Exit();
 
 
            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;
                direction.Normalize();
 
                rotation = (float)Math.Atan2(
                              (double)direction.Y,
                              (double)direction.X);
                SpaceShipPos += direction * speed;
 
            }
 
            this.IsMouseVisible = true;
 
            
 
            base.Update(gameTime);
        }
 
   
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
 
            spriteBatch.Begin();
 
            spriteBatch.Draw(
                 SpaceShip,
                 SpaceShipPos,
                 null,
                 Color.White,
                 rotation,
                 new Vector2(
                     SpaceShip.Width / 2,
                     SpaceShip.Height / 2),
                 1.0f,
                 SpriteEffects.None,
                 1.0f);
 
            spriteBatch.End();
 
            base.Draw(gameTime);
        }
    }
}
 

 

 

 

 

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
Advertisement

I don't think you have properly described what you want to happen, and what is currently happening that you want to correct.  Please consider that people are taking their own free time to help answer your questions and help you out, so try to be as clear and expressive as possible.  The better job you do of this, the more likely you will get some responses.

 

It is especially bad to just dump a bunch of code without any real description - so please clarify what you are looking for!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!