-1

How I can use Exit() in another class? I want to use it in the Intro class, but I always get that „menuinterface.Menu.exit' is never assigned to, and will always have its default value null“ error message. What is wrong?

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

2 回答 2

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;
}
于 2012-12-13T12:17:10.300 回答
0

您没有为您的exit对象分配任何东西,它似乎是 type 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);
    }
    // ...
}
于 2012-12-13T10:31:12.560 回答