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

1 回答 1

1

就像错误说的那样,您需要创建一个 Actor 类的实例。

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

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

于 2012-08-31T08:55:31.673 回答