【问题标题】:„menuinterface.Menu.exit' is never assigned to, and will always have its default value null““menuinterface.Menu.exit”从未分配给,并且始终具有其默认值 null“
【发布时间】:2012-12-13 20:17:10
【问题描述】:

如何在另一个类中使用 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);
    }
}

【问题讨论】:

标签: c# xna


【解决方案1】:

这是你的问题:

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

【讨论】:

  • 但它会做预期的事情吗(退出游戏)?
  • 它不起作用。我在 Game1 类中收到以下错误消息:'menuinterface.Intro' 不包含采用 0 个参数的构造函数 我应该在 Game1 类中这一行的括号之间添加什么参数:currentState = new Intro();
  • @Andy:当您创建新的Intro 对象时,您需要这样做:currentState = new Intro(this);
【解决方案2】:

您没有为您的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);
    }
    // ...
}

【讨论】:

  • 这样不行。我收到此错误消息:当前上下文中不存在名称“退出”
  • 您正在尝试从主 Game 类中执行此方法 - 而不是 -。将整个 if (kbstate ... 放在 Game 的 Update 方法中。
  • 把Exit()写进Intro类不是更好吗?我只想在介绍或菜单中按 Escape 后退出游戏。
  • 您必须检查游戏是否在介绍屏幕中。喜欢if (kbState.IsKeyDown(Keys.Escape) &amp;&amp; gameState == "intro screen") Exit()
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多