-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);
    }
}
Andy
  • 55
  • 1
  • 8
  • This might be helpful, if not a duplicate of the problem: http://stackoverflow.com/questions/4811155/field-xxx-is-never-assigned-to-and-will-always-have-its-default-value-null – user1306322 Dec 13 '12 at 10:26

2 Answers2

0

You do not assign anything to your exit object, which appears to be of type Game, so this is why you get this error. But why do you need this exit object in the first place?

If you want to quit the game after pressing Escape, use Exit() method. It doesn't require you to use it like in your code. It is as simple as this:

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

        base.Update(gameTime);
    }
    // ...
}
user1306322
  • 8,561
  • 18
  • 61
  • 122
  • It doesn't work like that. I get this error message: The name 'Exit' does not exist in the current context – Andy Dec 13 '12 at 11:29
  • You're trying to execute this method -not- from within the main Game class. Put the whole `if (kbstate ...` thing inside your Game's `Update` method. – user1306322 Dec 13 '12 at 11:34
  • Is it not better to write the Exit() into the Intro class? I just want to quit the game after pressing Escape during the intro or the menu. – Andy Dec 13 '12 at 12:02
  • You'll have to check if the game is in the intro screen. Like `if (kbState.IsKeyDown(Keys.Escape) && gameState == "intro screen") Exit()`. – user1306322 Dec 13 '12 at 12:05
0

Here's your problem:

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

In this class you are declaring an object called exit but this value is never assigned. You need to instantiate the object before you use it. In your case, I would add the following constructor to resolve your problem.

public Intro(Game1 game)
{
    exit = game;
}
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • But will it do what's expected (exit the game)? – user1306322 Dec 13 '12 at 14:45
  • It doesn't work. I get the following error message in Game1 class: 'menuinterface.Intro' does not contain a constructor that takes 0 arguments What argument should I add between the brackets of this line in Game1 class: currentState = new Intro(); – Andy Dec 13 '12 at 15:11
  • @Andy: when you are creating your new `Intro` object you need to do it like this: `currentState = new Intro(this);` – Neil Knight Dec 13 '12 at 15:57