-1

I am getting an error when I try to get the mouse position.

This is my player class. I am trying to make the player sprite move by mouse cursor

class Player : Actor
{
    public MouseState mState;

    EnemyManager enemyManager;

    public Player(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, EnemyManager enemyManager)
        : base(texture, origin, spriteBatch, new Vector2(250.0f, 516.0f))
    {

        this.enemyManager = enemyManager;
    }

    public override void Update(GameTime gameTime)
    {
        //KeyboardState keyboardState = Keyboard.GetState();

        //if (keyboardState.IsKeyDown(Keys.Left))
        //{
        //    if (this.Position.X > 32.0f)
        //    {
        //        this.Position -= 10.0f * Vector2.UnitX;
        //    }
        //}

        //if (keyboardState.IsKeyDown(Keys.Right))
        //{
        //    if (this.Position.X < 748.0f)
        //    {
        //        this.Position += 10.0f * Vector2.UnitX;
        //    }
        //}

        MouseState mState = Mouse.GetState();
        this.Position = new Vector2(mState.x, mState.y);


        // Collisions... 
        foreach (Enemy e in this.enemyManager.Enemies)
        {
            if (this.BoundingRectangle.Intersects(e.BoundingRectangle))
            {
                e.OnHit();

                break;
            }
        }

        base.Update(gameTime);
    }
}

and this is my Actor class which has position variables

namespace Shmup
{
    public class Actor
    {
       public Texture2D texture;
       public Vector2 origin;
       public SpriteBatch spriteBatch;
       public Vector2 position;
       public Rectangle boundingRectangle;

        public Vector2 Position
        {
             get { return position; }
             set { position = value; }
        }


        public Rectangle BoundingRectangle
        {
            get { return boundingRectangle; }
        }

        public Actor(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, Vector2 initialPosition)
        {
            this.texture = texture;
            this.origin = origin;
            this.spriteBatch = spriteBatch;
            this.position = initialPosition;

            boundingRectangle = new Rectangle((Int32)(initialPosition.X - origin.X), (Int32)(initialPosition.Y - origin.Y), texture.Width, texture.Height);
        }

        public virtual void Update(GameTime gameTime)
        {

        }

        public virtual void Draw(GameTime gameTime)
        {
            this.spriteBatch.Draw(texture, position - origin, Color.White);
        }
    }
}

my main class

public class MyGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player player;
    EnemyManager enemyManager;
    Actor actor;


    public MyGame()
    {
        graphics = new GraphicsDeviceManager(this);



        IsMouseVisible = true;

        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        Actor actor = new Actor();
        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        startTheGame(); 
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        {
            this.Exit();
        }

        this.player.Update(gameTime);
        this.enemyManager.Update(gameTime);



        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Maroon);

        this.spriteBatch.Begin();


        this.player.Draw(gameTime);
        this.enemyManager.Draw(gameTime);

        this.spriteBatch.End(); 

        base.Draw(gameTime);
    }

    void startTheGame()
    {
        enemyManager = new EnemyManager(this.Content.Load<Texture2D>("Enemy"), new Vector2(16), this.spriteBatch);

        player = new Player(this.Content.Load<Texture2D>("Player"),actor.position, this.spriteBatch, enemyManager);

        enemyManager.StartTheGame(10);
    }
}
Ndr Krty
  • 3
  • 3

2 Answers2

1

This line is the problem I think:

this.Position = new Vector2(mState.x, mState.y);

x and y are not publicly exposed on MouseState. You need to capitalize them (C# is case-sensitive). Use this instead:

this.Position = new Vector2(mState.X, mState.Y);
SomeWritesReserved
  • 1,075
  • 7
  • 17
  • making these changes gives me a different error Object reference not set to an instance of an object in the line player = new Player(this.Content.Load("Player"),actor.position, this.spriteBatch, enemyManager); on my main class it says Error 'Shmup.Actor' does not contain a constructor that takes 0 arguments – Ndr Krty Aug 29 '12 at 14:26
  • A different error on the same line or a different error somewhere else? You likely have other compiler errors elsewhere. – SomeWritesReserved Aug 29 '12 at 14:30
  • Different error in somewhere else i put my main class where new error accurs in the line player = new Player(this.Content.Load("Player"),actor.position, this.spriteBatch, enemyManager); – Ndr Krty Aug 29 '12 at 14:36
0

"making these changes gives me a different error Object reference not set to an instance of an object in the line player = new Player(this.Content.Load("Player"),actor.position, this.spriteBatch, enemyManager); on my main class it says Error 'Shmup.Actor' does not contain a constructor that takes 0 arguments –"

The constructor for Actor requires - Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, Vector2 initialPosition .

So you would need to change the "Actor actor = new Actor();" into LoadContent() or a separate method after you have supplied all the information required to declare a new Actor.

It should read something along these lines(replace each argument with the proper item from your game class):

Actor actor = new Actor( texture, origin, spriteBatch, initialPosition);

If you do that it will fix the error, If you don't you will continue to get the error unless you define a constructor in Actor that takes 0 arguments. Hope this helps somewhat.