Ingame menu is not working correctly

Posted by Johnny on Game Development See other posts from Game Development or by Johnny
Published on 2012-12-19T22:50:45Z Indexed on 2012/12/19 23:15 UTC
Read the original article Hit count: 224

Filed under:
|

The ingame menu opens when the player presses Escape during the main game. If the player presses Y in the ingame menu, the game switches to the main menu. Up to here, everything works. But: On the other hand, if the player presses N in the ingame menu, the game should switch back to the main game(should resume the main game). But that doesn't work. The game just rests in the ingame menu if the player presses N. I set a breakpoint in this line of the Ingamemenu class:

KeyboardState kbState = Keyboard.GetState();

CurrentSate/currentGameState and LastState/lastGameState have the same state: IngamemenuState. But LastState/lastGameState should not have the same state than CurrentSate/currentGameState. What is wrong? Why is the ingame menu not working correctly?

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

    public enum GameStates
    {
        IntroState = 0,
        MenuState = 1,
        MaingameState = 2,
        IngamemenuState = 3
    }

    public void ChangeGameState(GameStates newState)
    {
        lastGameState = currentGameState;
        lastState = currentState;
        switch (newState)
        {
            case GameStates.IntroState:
                currentState = new Intro(this);
                currentGameState = GameStates.IntroState;
                break;
            case GameStates.MenuState:
                currentState = new Menu(this);
                currentGameState = GameStates.MenuState;
                break;
            case GameStates.MaingameState:
                  currentState = new Maingame(this);
                  currentGameState = GameStates.MaingameState;
                break;
            case GameStates.IngamemenuState:
                currentState = new Ingamemenu(this);
                currentGameState = GameStates.IngamemenuState;
                break;
        }
        currentState.Load(Content);
    }

    public void ChangeCurrentToLastGameState()
    {
        currentGameState = lastGameState;
        currentState = lastState;
    }

    public GameStates CurrentState
    {
        get { return currentGameState; }
        set { currentGameState = value; }
    }

    public GameStates LastState
    {
        get { return lastGameState; }
        set { lastGameState = value; }
    }

    private GameStates currentGameState = GameStates.IntroState;
    private GameStates lastGameState;

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

    protected override void Initialize()
    {
        ChangeGameState(GameStates.IntroState);
        base.Initialize();
    }

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

    protected override void Update(GameTime gameTime)
    {
        currentState.Update(gameTime);
        if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.IngamemenuState))
        {
            lastState.Update(gameTime);
        }
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.IngamemenuState))
        {
          lastState.Render(spriteBatch);
        }
        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
{
    Texture2D Titelbildschirm;
    private Game1 game1;       

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

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

    public void Update(GameTime gametime)
    {
        KeyboardState kbState = Keyboard.GetState();
        if (kbState.IsKeyDown(Keys.Space))
            game1.ChangeGameState(Game1.GameStates.MenuState);
    }

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


public class Menu:IState
{
Texture2D Choosescreen;
private Game1 game1;

    public Menu(Game1 game)
    {
        game1 = game;
    }

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

public void Update(GameTime gametime)
{
    KeyboardState kbState = Keyboard.GetState();
    if (kbState.IsKeyDown(Keys.Enter))
        game1.ChangeGameState(Game1.GameStates.MaingameState);
    if (kbState.IsKeyDown(Keys.Escape))
        game1.Exit();
}

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

public class Maingame : IState
{
    Texture2D Spielbildschirm, axe;
    Vector2 position = new Vector2(100,100);

    private Game1 game1;

    public Maingame(Game1 game)
    {
        game1 = game;
    }

    public void Load(ContentManager content)
    {
        Spielbildschirm = content.Load<Texture2D>("hauszombie");
        axe = content.Load<Texture2D>("axxx");
    }

    public void Update(GameTime gametime)
    {
          KeyboardState keyboardState = Keyboard.GetState();
          float delta = (float)gametime.ElapsedGameTime.TotalSeconds;
          position.X += 5 * delta;
          position.Y += 3 * delta;

          if (keyboardState.IsKeyDown(Keys.Escape))
            game1.ChangeGameState(Game1.GameStates.IngamemenuState);        
    }

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

public class Ingamemenu : IState
{
    Texture2D Quitscreen;
    private Game1 game1;

    public Ingamemenu(Game1 game)
    {
        game1 = game;
    }

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

    public void Update(GameTime gametime)
    {
        KeyboardState kbState = Keyboard.GetState();
        if (kbState.IsKeyDown(Keys.Y))
           game1.ChangeGameState(Game1.GameStates.MenuState);
        if (kbState.IsKeyDown(Keys.N))
          game1.ChangeCurrentToLastGameState();
    }

    public void Render(SpriteBatch batch)
    {
        batch.Draw(Quitscreen, new Rectangle(200, 200, 200, 200), Color.White);
    }
}

© Game Development or respective owner

Related posts about XNA

Related posts about c#