C# 对象引用未设置为对象的实例。怎么了?

C# 对象引用未设置为对象的实例。怎么了?,c#,xna,C#,Xna,我有两门课:游戏1和动画。 我总是得到“对象引用未设置为对象实例”的错误消息,在Game1类的这一行:animation.Draw(spriteBatch); 怎么了?我不知道该换什么 Animation class code: public class Animation { private int _animIndex; public TimeSpan PassedTime { get; private set; } public Rec

我有两门课:游戏1和动画。 我总是得到“对象引用未设置为对象实例”的错误消息,在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); 
    } 
}
动画类代码:
公共类动画
{            
私人综合指数;
公共时间跨度已过时间{get;private set;}
公共矩形[]SourceRects{get;private set;}
公共纹理2D纹理{get;private set;}
公共时间跨度持续时间{get;private set;}
公共动画(矩形[]源矩形,纹理2D纹理,时间跨度持续时间)
{ 
for(int i=0;i持续时间)
{ 
PassedTime-=持续时间;//zurücksetzen
} 
变量百分比=PassedTime.TotalSeconds/Duration.TotalSeconds;
_animIndex=(int)Math.Round(百分比*(SourceRects.Length-1));
} 
公共作废取款(SpriteBatch批量)
{ 
batch.Draw(纹理,新矩形(0,0,纹理.宽度/源矩形.长度,纹理.高度),源矩形[_animiindex],颜色.白色);
} 
}
游戏1类代码:
公共类游戏1:Microsoft.Xna.Framework.Game
{ 
图形管理器图形;
SpriteBatch SpriteBatch;
动画;
纹理2d-gegner;
矩形[]gegnerbilder=新矩形[10];
公共游戏1()
{ 
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
} 
受保护的覆盖无效初始化()
{          
base.Initialize();
} 
受保护的覆盖void LoadContent()
{            
spriteBatch=新spriteBatch(图形设备);
gegner=Content.Load(“kurzeanim”);
} 
受保护覆盖无效更新(游戏时间游戏时间)
{ 
KeyboardState kbState=Keyboard.GetState();
if(kbState.IsKeyDown(Keys.A))
{ 
动画=新动画(GegnerBinder、gegner、TimeSpan.FromSeconds(3));
动画。更新(游戏时间);
} 
更新(游戏时间);
} 
受保护覆盖无效绘制(游戏时间游戏时间)
{ 
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
动画绘制(spriteBatch);
spriteBatch.End();
基础。抽签(游戏时间);
} 
}

您的字段
动画
未初始化,您正在调用它的实例方法
Draw
,这就是为什么会出现此异常。在那一行,它是空的。它是在更新方法中初始化的,您可以在构造函数中初始化它以避免该异常

确保在调用该类的
Draw
-方法之前调用
Game1
-类的
update
-方法,或者在调用其成员之前确保
animation
不是
null

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

只有在调用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方法添加一些逻辑来检查空值。

animation=newanimation(gegnerbilder,gegner,TimeSpan.FromSeconds(3))

你真的在创建一个新的动画,并且每更新一次(可能是每秒60次)

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


然后在更新中,您仍然可以执行动画。update()但不使用构造函数。

动画
仅在
更新
中初始化,而不是在
绘制
中初始化。这就是为什么在
Draw
中会出现异常。使用构造函数初始化字段。我看不出这个问题在将来对其他读者有多大帮助。很高兴您发现了错误。您正在有条件地分配动画实例。你能保证你的动画对象不是空的吗?我试过了,但没用。这是我第一次使用属性<代码>公共类游戏1:Microsoft.Xna.Framework.Game{GraphicsDeviceManager图形;SpriteBatch SpriteBatch;动画;纹理2d敌人;矩形[]enemyframes;时间跨度时间;动画=新动画(矩形[]敌人,纹理2d enemyframes,时间跨度时间)那是什么代码?有错误吗?或者只是不显示?