2012-09-11 62 views
0

我有兩個類:Game1和Animation。 我總是得到「沒有設置對象實例的對象引用」。錯誤消息在Game1類的這一行中:animation.Draw(spriteBatch); 有什麼不對?我不知道要改變什麼。未將對象引用設置爲對象的實例。哪裏不對?

Animation class code: 
public class Animation 
{    
    private int _animIndex; 
    public TimeSpan PassedTime { get; private set; } 
    public Rectangle[] SourceRects { get; private set; }  
    public Texture2D Texture {get; private set; } 
    public TimeSpan Duration { get; private set; } 

    public Animation(Rectangle[] sourceRects, Texture2D texture, TimeSpan duration) 
    { 
      for (int i = 0; i < sourceRects.Length; i++) 
      { 
       sourceRects[i] = new Rectangle((sourceRects.Length - 1 - i) * (Texture.Width/sourceRects.Length), 0, Texture.Width/sourceRects.Length, Texture.Height); 
      } 

     SourceRects = sourceRects; 
     Texture = texture; 
     Duration = duration; 
    }   

    public void Update(GameTime dt) 
    { 
     PassedTime += dt.ElapsedGameTime; 
     if (PassedTime > Duration) 
     { 
      PassedTime -= Duration; // zurücksetzen 
     } 

     var percent = PassedTime.TotalSeconds/Duration.TotalSeconds; 
     _animIndex = (int)Math.Round(percent * (SourceRects.Length - 1)); 
    } 

    public void Draw(SpriteBatch batch) 
    { 
     batch.Draw(Texture, new Rectangle(0, 0, Texture.Width/SourceRects.Length, Texture.Height), SourceRects[_animIndex], Color.White); 
    } 
} 

Game1 class code: 
public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Animation animation; 
    Texture2D gegner; 
    Rectangle[] gegnerbilder = new Rectangle[10]; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    {   
     base.Initialize(); 
    } 

    protected override void LoadContent() 
    {    
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     gegner = Content.Load<Texture2D>("kurzeanim"); 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     KeyboardState kbState = Keyboard.GetState(); 
     if (kbState.IsKeyDown(Keys.A)) 
     { 
      animation = new Animation(gegnerbilder, gegner, TimeSpan.FromSeconds(3)); 
      animation.Update(gameTime); 
     } 
     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 
     spriteBatch.Begin(); 
     animation.Draw(spriteBatch);  
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 
+2

「動畫」只在'Update'中初始化,而不在'Draw'中初始化。這就是爲什麼你在'Draw'中得到異常。使用構造函數初始化字段。 –

+1

我看不出這個問題將來會如何幫助其他讀者。很高興你發現了你的bug。 – Jodrell

+0

您正在有條件地分配動畫實例。你能確保你的動畫對象不爲null。 –

回答

0

確保Game1 -class的Update - 方法的Draw - 方法是類的調用之前或保證animationnull調用它的成員之前:

protected override void Draw(GameTime gameTime) 
{ 
    if (animation != null) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 
     spriteBatch.Begin(); 
     animation.Draw(spriteBatch);  
     spriteBatch.End(); 
    } 
    base.Draw(gameTime); 
} 
1

你的領域animation沒有初始化,您在調用它的實例方法Draw這就是爲什麼你得到這個例外。在特定的行中它是空的。它在更新方法初始化,可以在構造函數初始化它避免異常

0

你永遠只能實例化的動畫屬性如果在更新被稱爲A鍵被按下。

protected override void Update(GameTime gameTime) 
{ 
    KeyboardState kbState = Keyboard.GetState(); 
    if (kbState.IsKeyDown(Keys.A)) 
    { 
    animation = new Animation(gegnerbilder, gegner, TimeSpan.FromSeconds(3)); 
    animation.Update(gameTime); 
    } 
    base.Update(gameTime); 
} 

您需要提前(例如,在構造函數)初始化或添加一些邏輯來Draw方法來檢查空值。

0

animation = new Animation(gegnerbilder, gegner, TimeSpan.FromSeconds(3));

你真的創造一個全新的動畫和更新它的每一個Update()這可能是每秒60次。

您將需要將其添加到您的Initialize()方法(或當您需要更改動畫)

然後在更新你仍然可以做animation.Update(),但沒有構造函數。

+0

我嘗試過,但它不起作用。這是我第一次與物業合作。 '公開課Game1:Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; 動畫動畫; Texture2D敵人; 矩形[] enemyframes; TimeSpan時間; animation = new animation(Rectangle [] enemy,Texture2D enemyframes,TimeSpan time);' –

+0

那是什麼代碼?有錯誤嗎?或者只是不顯示? – Cyral

相關問題