2012-08-31 46 views
0

我在这一行收到错误消息:我得到一个空的错误

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

告诉我actor对象为null。我该如何克服这种情况?

确切的错误是:

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

那么,为什么你使用'actor.position',如果你从不向'actor'提供一个值呢? –

+1

你似乎没有创建一个actor对象;在startTheGame函数中首先初始化actor。 – daryal

+0

你还需要那个演员吗?也许用一些固定的位置来创造你的玩家,比如敌人? – JleruOHeP

回答

1

像错误说,你需要创建一个演员类的一个实例。

在游戏中构造函数或方法startTheGame你应该创建演员类的一个实例(创建Player类的实例之前),是这样的:

this.actor =新演员(变量...);

+0

我想如果OP了解错误信息说什么,他/他不会问。我建议你给你的答案增加一个例子或解释,否则它可能就是一个评论。 – stakx

+1

所以在游戏构造函数中,你应该这样说: this.actor = new Actor([fillInVariablesHere]); – MrFox