Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Mouse Help!

Started by Tysonclyde May 19, 2013 at 11:13 PM 0 replies 950+ views
Original Post
Tysonclyde
Tysonclyde

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("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!

GaldorPunk
GaldorPunk

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.