Rotate sprite with mouse

Started by
3 comments, last by menyo 12 years, 2 months ago
Argh! Hello.

I've been trying to get my sprite to move where I move my mouse, it worked when I didn't have the project OOP but I want it to look neat.
I have missed something here, the sprite is drawn but it's not following the mouse like it should.

Please do help ^^


game1.cs


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 RotateTowardsMouse
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

public Vector2 mousePosition;
Player player = null;



public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
}

protected override void Initialize()
{

Window.Title = "alpha" ;
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();



base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

player = new Player(spriteBatch, this);
player.ScreenBoundaries = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
player.Initialize();

}

protected override void Draw(GameTime gameTime)
{

GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

player.Draw(gameTime);

spriteBatch.End();


base.Draw(gameTime);
}

}
}



Player.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace RotateTowardsMouse
{
class Player : Entity
{

public Player(SpriteBatch spritebatch, Game game)
: base(spritebatch, game)
{
}

public override void Update(GameTime gameTime)
{
// Get the current mouse position
mousePosition.X = Mouse.GetState().X;
mousePosition.Y = Mouse.GetState().Y;

// Get The direction from the player position to the mouse position
Vector2 directionToMouse = mousePosition - position;

// Get the angle of the directional vector (Note that it must be a unit vector so we have to normalize it)
directionToMouse.Normalize();
playerRotation = (float)Math.Atan2((float)directionToMouse.Y, (float)directionToMouse.X);


base.Update(gameTime);
}


public override void Initialize()
{
screenBoundaries = new Rectangle(0, 0,
Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);

position = new Vector2(screenBoundaries.Width * 0.5f, screenBoundaries.Height * 0.5f);

base.Initialize();
}


protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

// Load our player sprite
texture = Game.Content.Load<Texture2D>("Player");

// Set the rotation point to be in the center of our character
playerRotationPoint = new Vector2(texture.Width / 2, texture.Height / 2);

// Set the default rotation in radians (facing to the right)
playerRotation = 0.0f;

// Just set the mouse position to a default value here (we will update it
// in the Update(GameTime)-method)
mousePosition = Vector2.Zero;
}



}
}



Entity.cs


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RotateTowardsMouse
{
class Entity : DrawableGameComponent
{

public Vector2 position = Vector2.Zero;
public float speed = 0.0f;

public Vector2 playerRotationPoint;
public float playerRotation;

public Vector2 scale = new Vector2(1.0f, 1.0f);
public Vector2 mousePosition;


protected Texture2D texture;
public SpriteBatch spriteBatch;
public Rectangle screenBoundaries;


public Rectangle ScreenBoundaries
{
get { return screenBoundaries; }
set { screenBoundaries = value; }
}

public Entity(SpriteBatch spritebatch, Game game)
: base(game)
{
this.spriteBatch = spritebatch;
}

public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();

spriteBatch.Draw(texture, position, null, Color.White, playerRotation, playerRotationPoint, 1.0f, SpriteEffects.None, 0.0f);

spriteBatch.End();
}
}
}

Advertisement
You are not calling the player.Update method in Game1.cs biggrin.png

Not sure what you did here...
protected override void Draw(GameTime gameTime) // should be protected override void Draw(SpriteBatch spriteBatch)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
player.Draw(gameTime);
spriteBatch.End();

base.Draw(gameTime);
}
//Update method... it's completely missing :D
protected override void Update(GameTime gameTime)
{
player.Update(gameTime);
base.Update(gameTime);
}


Argh! Hello.

I've been trying to get my sprite to move where I move my mouse, it worked when I didn't have the project OOP but I want it to look neat.
I have missed something here, the sprite is drawn but it's not following the mouse like it should.


Is it moving at all when you move the mouse, or moving sprrasically?

You are not calling the player.Update method in Game1.cs biggrin.png

Not sure what you did here...
protected override void Draw(GameTime gameTime) // should be protected override void Draw(SpriteBatch spriteBatch)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
player.Draw(gameTime);
spriteBatch.End();

base.Draw(gameTime);
}
//Update method... it's completely missing :D
protected override void Update(GameTime gameTime)
{
player.Update(gameTime);
base.Update(gameTime);
}




I do have player.Update, now ^^

Still no luck tho.

And Jacol, it's standing completly still.
Working now, woppelidoda! Thx :)
Your welcome! :D

This topic is closed to new replies.

Advertisement