我收到一个空错误

时间:2012-08-31 08:50:09

标签: c# xna xna-4.0

我收到此行中的错误消息:

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

告诉我actor对象为空。我怎样才能克服这种情况呢?

确切的错误是:

  

永远不会将字段Shmup.MyGame.actor分配给,并始终使用其默认值null

这是我的代码:

class MyGame // [edit:] added based on below constructor
{
    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
        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);
    }
}

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;
             boundingRectangle = new Rectangle((Int32)(position.X - origin.X), (Int32)(position.Y - origin.Y), texture.Width, texture.Height);
         }
    }        

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

1 个答案:

答案 0 :(得分:1)

如错误所示,您需要创建一个Actor类的实例。

在游戏构造函数或startTheGame方法中(在创建Player类的实例之前),您应该创建一个Actor类的实例,如:

this.actor = new Actor(变量......);