2012-04-19 40 views
1

现在的问题是,我不知道错误的含义是什么,但我不能为上帝的生活弄清楚可能是什么原因造成的。我已经搜寻了很多寻找答案,但我的痛苦的解决方案逃避我。很多次都试图克服我的障碍但失败了。现在,我把你希望的启蒙,也希望你们我的大脑安息:)XNA C#content.load <spritefont>对象引用未设置为对象的实例

这里谈到的问题: 的Game1运行并做它的事情与我的类名为Abstakt:

class Abstrakt 
{ 
    public ContentManager content; 
    public SpriteBatch spriteBatch; 
    public GraphicsDeviceManager graphics; 

    MenuComponent menuComponent; 
    StartGame startGame; 

    public string gameState = "Menu"; 

    public Abstrakt(SpriteBatch spriteBatch, ContentManager content, GraphicsDeviceManager graphics) 
    { 
     this.spriteBatch = spriteBatch; 
     this.content = content; 
     this.graphics = graphics; 
    } 

    public Abstrakt() { } 

    public virtual void Initialize() 
    { 
     menuComponent = new MenuComponent(); 
     startGame = new StartGame(); 

     menuComponent.Initialize(); 
     startGame.Initialize(); 
    } 

    public virtual void LoadContent() 
    { 
     menuComponent.LoadContent(); 
     startGame.LoadContent(); 
    } 
}   






class MenuComponent : Abstrakt 
{ 
    SpriteFont spriteFont; 

    public MenuComponent() { } 

    public override void LoadContent() 
    { 
     spriteFont = content.Load<SpriteFont>("GameFont"); <--Here the problem appears 
    } 
} 

伊夫删除代码是不重要的,所以它会更容易查看。第二个问题是: 未将对象引用设置为对象的实例。

感谢您的任何帮助和建议。

回答

3

我认为你不要初始化“内容”变量。

如果实例与默认的无参数的构造函数一个MenuComponent中,内容未分配..所以这将是等于空...

+0

+1,我认为这是唯一可行的方式来获得错误消息描述。 @ user1244948:我会学习如何更多地使用调试器。当它在这里崩溃时,您可以将鼠标放在变量上以查看其当前状态。只要将鼠标放在'content'上就可以当场指出问题。 – 2012-04-19 20:59:49

+0

谢谢,是的,这是问题所在。我使用它在游戏1类之前,它有一个值:) 虽然我用另一个peice代码看起来像这样:pPosition = new Vector2((graphics.PreferredBackBufferWidth - pTexture.Width)/ 2,(graphics.PreferredBackBufferHeight - pTexture.Height)/ 2); 在我的LoadContent方法中,试图解决它,但但...感谢您帮助我沿着方向迈进一步:D – user1344948 2012-04-19 21:07:16

+0

您应遵循@john mcdonald建议,使用调试器...并且不要使用图形外部游戏构造函数...使用GraphicsDevice.Viewport.Width/Height代替...并且你应该确定pTexture不为空;) – Blau 2012-04-20 00:30:16

相关问题