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

4 回答 4

1

您的字段animation未初始化,您正在调用其实例方法Draw,这就是您收到此异常的原因。在那个特定的行它是空的。它正在更新方法中初始化,您可以在构造函数中对其进行初始化以避免该异常

于 2012-09-11T09:51:52.437 回答
0

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

您是否真的在创建一个新动画并每隔Update()60 次更新一次?

您将需要将其添加到您的Initialize()方法中(或者当您需要更改动画时)

然后在更新中你仍然可以做 animation.Update() 但没有构造函数。

于 2012-09-11T11:59:02.360 回答
0

确保-class 的Update-method在该类的 -method 之前调用,或者确保不在调用其成员之前调用:Game1Drawanimationnull

protected override void Draw(GameTime gameTime) 
{ 
    if (animation != null)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue); 
        spriteBatch.Begin(); 
        animation.Draw(spriteBatch);    
        spriteBatch.End(); 
    }
    base.Draw(gameTime); 
} 
于 2012-09-11T09:52:12.250 回答
0

如果在调用 Update 时按下 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 方法添加一些逻辑以检查空值。

于 2012-09-11T09:53:25.383 回答