“menuinterface.Menu.exit”永远不会被赋值,并且将始终具有其默认值null“

时间:2012-12-13 10:23:09

标签: c# xna

如何在另一个类中使用Exit()?我想在Intro类中使用它,但我总是得到“menuinterface.Menu.exit”从未被赋值,并且将始终具有其默认值null“错误消息。有什么问题?

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private IState currentState;

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

    protected override void Initialize()
    {
        currentState = new Intro();
        base.Initialize();
    }

    protected override void LoadContent()
    {       
        spriteBatch = new SpriteBatch(GraphicsDevice);
        currentState.Load(Content);
    }

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

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

        base.Draw(gameTime);
    }
}

public interface IState
    {
        void Load(ContentManager content);
        void Update(GameTime gametime);
        void Render(SpriteBatch batch);
    }

public class Intro : IState
{
    private IState currentState;
    private Game1 exit;
    Texture2D introscreen;

    public void Load(ContentManager content)
    {
        introscreen = content.Load<Texture2D>("intro");
    }

    public void Update(GameTime gametime)
    {
        KeyboardState kbState = Keyboard.GetState();
        if (kbState.IsKeyDown(Keys.Space))
          currentState = new Menu();
        if (kbState.IsKeyDown(Keys.Escape))
            exit.Exit();
    }

    public void Render(SpriteBatch batch)
    {
        batch.Draw(introscreen, new Rectangle(0, 0, 1280, 720), Color.White);
    }
}

2 个答案:

答案 0 :(得分:0)

您没有为exit对象分配任何内容,该对象似乎是Game类型,因此这就是您收到此错误的原因。但是为什么你首先需要这个exit对象?

如果您想在按Escape后退出游戏,请使用Exit()方法。它不需要您像在代码中一样使用它。就这么简单:

public class Game1 : Microsoft.Xna.Framework.Game
{
    // ...
    protected override void Update(GameTime gameTime)
    {            
        if (kbState.IsKeyDown(Keys.Escape))
            Exit();  

        base.Update(gameTime);
    }
    // ...
}

答案 1 :(得分:0)

这是你的问题:

public class Intro : IState
{
    private IState currentState;
    private Game1 exit;
    Texture2D introscreen;

    public void Load(ContentManager content)
    {
        introscreen = content.Load<Texture2D>("intro");
    }

    public void Update(GameTime gametime)
    {
        KeyboardState kbState = Keyboard.GetState();
        if (kbState.IsKeyDown(Keys.Space))
          currentState = new Menu();
        if (kbState.IsKeyDown(Keys.Escape))
            exit.Exit(); // ---- this object does not exist
    }

    public void Render(SpriteBatch batch)
    {
        batch.Draw(introscreen, new Rectangle(0, 0, 1280, 720), Color.White);
    }
}

在此类中,您声明了一个名为exit的对象,但从未分配此值。您需要在使用它之前实例化该对象。在您的情况下,我将添加以下构造函数来解决您的问题。

public Intro(Game1 game)
{
    exit = game;
}