-1

尝试获取鼠标位置时出现错误。

这是我的播放器类。我试图让玩家精灵通过鼠标光标移动

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

这是我的Actor类,它有位置变量

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

我的主要课程

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

2 回答 2

1

这条线是我认为的问题:

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

x并且y不会在MouseState. 您需要将它们大写(C# 区分大小写)。改用这个:

this.Position = new Vector2(mState.X, mState.Y);
于 2012-08-29T14:15:30.183 回答
0

“进行这些更改会给我一个不同的错误 Object reference not set to an instance of an object in line player = new Player(this.Content.Load("Player"),actor.position, this.spriteBatch,enemyManager); on我的主类它说错误'Shmup.Actor'不包含带有0个参数的构造函数——”

Actor 的构造函数需要 - Texture2D 纹理、Vector2 origin、SpriteBatch spriteBatch、Vector2 initialPosition。

所以你需要改变“Actor actor = new Actor();” 在您提供了声明新 Actor 所需的所有信息后,将其放入 LoadContent() 或单独的方法中。

它应该阅读以下内容(将每个参数替换为游戏类中的正确项目):

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

如果你这样做,它将修复错误,如果你不这样做,除非你在 Actor 中定义一个接受 0 个参数的构造函数,否则你将继续得到错误。希望这会有所帮助。

于 2013-04-01T03:39:29.720 回答