Jump to content

  • Log In with Google      Sign In   
  • Create Account


Mouse Help!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Tysonclyde   Members   -  Reputation: 102

Like
0Likes
Like

Posted 19 May 2013 - 05:13 PM

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!



Sponsor:

#2 GaldorPunk   Members   -  Reputation: 971

Like
0Likes
Like

Posted 19 May 2013 - 08:12 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS