Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从xna中的另一个类(不是主类)调用类中的load content方法_C#_Xna - Fatal编程技术网

C# 如何从xna中的另一个类(不是主类)调用类中的load content方法

C# 如何从xna中的另一个类(不是主类)调用类中的load content方法,c#,xna,C#,Xna,我正在为学校制作一个游戏,里面有3个小游戏。我想把这些小游戏分成自己的班级,这样主班就不会太拥挤,也不会太难阅读,但每次我试着运行游戏时,它都会说 "An unhandled exception of type 'System.NullReferenceException' occurred in Summer Assignment.exe" 当我从类中取出加载内容的行,并且我以前使用过类时,游戏运行得很好,所以这不是问题所在,这是代码 class Quiz { QuizQuestio

我正在为学校制作一个游戏,里面有3个小游戏。我想把这些小游戏分成自己的班级,这样主班就不会太拥挤,也不会太难阅读,但每次我试着运行游戏时,它都会说

"An unhandled exception of type 'System.NullReferenceException' occurred in Summer Assignment.exe"
当我从类中取出加载内容的行,并且我以前使用过类时,游戏运行得很好,所以这不是问题所在,这是代码

class Quiz
{
    QuizQuestion no1;
    ContentManager theContentManager;
    SpriteBatch thespriteBatch;
    int question = 0;

    public void initialize()
    {
        no1 = new QuizQuestion();
    }

    public void LoadContent()
    {
        no1.LoadContent(this.theContentManager);
    }
在我从load content方法加载内容的类中

public void LoadContent(ContentManager theContentManager)
{
    font = theContentManager.Load<SpriteFont>("Font2");
}
public void加载内容(ContentManager内容管理器)
{
font=contentmanager.Load(“Font2”);
}

在添加下一个类之前,我在主游戏类中正确加载了该类,以确保您需要为字段指定实际对象。如果你看一下
测验。ContentManager
,你会发现你从来没有给它赋值。您可以通过从
Game1
中传入这些代码来修复此问题。例如,Game1应该如下所示:

public class Game1 : Microsoft.Xna.Framework.Game
{
    Quiz quiz;

    protected override void LoadContent()
    {
        quiz.LoadContent(Content);
    }

    protected override void Update(GameTime gameTime)
    {
        quiz.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        quiz.Draw(spriteBatch, gameTime);
    }
}
然后,您的测验类应该如下所示(注意,对于使用这种方法的任何XNA内容,您都不需要类字段):


谢谢,过去几天我一直在到处寻找解决方案,但我找不到我遇到的具体问题
public class Quiz
{
    QuizQuestion no1 = new QuizQuestion();

    public void LoadContent(ContentManager content)
    {
        no1.LoadContent(content);
    }

    public void Update(GameTime gameTime)
    {
        // Perform whatever updates are required.
    }

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        // Draw whatever
    }
}