0
votes

I've been working on my first Xna game, and yes, that's my excuse for the poor coding. I've managed to fix most problems, but not this one. I can't really make scense of this. I'm trying to make the game go back to default when my player dies (called sprite). Can you guys help? To be clear, when my player dies, i want the GameState to go back to MainMenu, and the buttons to go back to normal. Now, when the player dies, the buttons can't be pressed. Thanks in advance. I'll provide everything that has to do with the Menu/buttons. In Game1:

enum GameState
    {
        MainMenu, 
        Play,
        HighScore,
        GameOver,
    }
    GameState CurrentGameState = GameState.MainMenu;

``

cButtonplay btnPlay;
    cButtonHS btnHighScore;
protected override void LoadContent()
    {
        IsMouseVisible = true;
        btnPlay = new cButtonplay(Content.Load<Texture2D>("button_play"), graphics.GraphicsDevice);
        btnPlay.setPosition(new Vector2(300, 250));

        btnHighScore = new cButtonHS(Content.Load<Texture2D>("button_highscore"), graphics.GraphicsDevice);
        btnHighScore.setPosition(new Vector2(300, 300));
    }

And my update logic:

protected override void Update(GameTime gameTime)
    {

      //MENU
        isSpriteAlive = true;
        MouseState mouse = Mouse.GetState();
        switch (CurrentGameState)
        {
            case GameState.MainMenu:

                if (btnPlay.isClicked == true)
                {
                    btnPlay.isClicked = false;
                    CurrentGameState = GameState.Play;
                }
                btnPlay.Update(mouse);
                if (btnHighScore.isClicked == true)
                {
                    btnHighScore.isClicked = false;
                    CurrentGameState = GameState.HighScore;
                   // btnHighScore.Update(mouse);
                }

                break;
                case GameState.Play:

                isSpriteAlive = true;
                // Here is alot of various code
                case GameState.HighScore:
                if (btnHighScore.isClicked == true) CurrentGameState =                        GameState.HighScore;
                btnHighScore.Update(mouse);
                break;

            case GameState.GameOver:
                btnHighScore.isClicked = false;
                btnHighScore.Update(mouse);
                break;

And finally my draw method:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.ForestGreen);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null);

        //MENU
        switch (CurrentGameState)
        {
            case GameState.MainMenu:
                spriteBatch.Draw(Content.Load<Texture2D>("menu_background"), new Rectangle(0, 0, screenWidth, screenHeight), Color.WhiteSmoke);
                btnPlay.playDraw(spriteBatch);
                btnHighScore.highScoreDraw(spriteBatch);
                printText.PrintMainMenu("By: Felix Claesson\nand Carl Wikstrom", spriteBatch, 250, 500);
                //printText.Print("$ " + points * 25, spriteBatch, 0, 0);

                break;

            case GameState.Play:
                if (isSpriteAlive == true)
                {
                    spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
                    foreach (Enemies enemy in enemies)
                        enemy.Draw(spriteBatch);
                    foreach (Bullets bullet in bullets)
                        bullet.Draw(spriteBatch);
                    foreach (healthBars healthbar in healthbars)
                        healthbar.Draw(spriteBatch);
                    if (isSpriteAlive == true)
                        spriteBatch.Draw(spriteTexture, spritePosition, new Rectangle(56 * frames, 0, 56, 51), Color.White, playerRotation, spriteOrigin, 1f, SpriteEffects.None, 0);
                    printText.Print("$ " + points * 25, spriteBatch, 0, 0);
                }
                else CurrentGameState = GameState.GameOver;
                break;

            case GameState.HighScore:
                printText.Print(":-)", spriteBatch, 350, 300);
                break;

            case GameState.GameOver:
                printText.Print("h", spriteBatch, 350, 300);
                btnHighScore.highScoreDraw(spriteBatch);
                break;
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

Do you have any tips at all? I can't play twice and my buttons won't work twice. Best regards.

1
Are cButtonplay and cButtonHS your own classes? Can you please paste the code for them? Also, where are the button .isClicked properties being set to true? - user3256944 salutes Monica
Mmmm, it's hard to understand the issue with the code provided... I understand that healthbars is a collection of the players healthbars? If so, on your update method the "IsSpriteAlive" (is this the player?) should be set to a value depending on whether or not all healthbars are 0 or not. With that set, if he is not alive, then just change the game state value. - Nahuel Ianni

1 Answers

0
votes

You should definitely look into using GameComponents instead of using switch statements. Less messy and you can separate the concerns of each screen, as the functionality is held in each screen class. Here is the documentation.

From the page:

Game components provide a modular way of adding functionality to a game. You create a game component by deriving the new component either from the GameComponent class, or, if the component loads and draws graphics content, from the DrawableGameComponent class.

You then add game logic and rendering code to the game component by overriding GameComponent.Update,DrawableGameComponent.Draw and GameComponent.Initialize. A game component is registered with a game by passing the component to Game.Components.Add.

A registered component will have its draw, update, and initialize methods called from the Game.Initialize, Game.Update, and Game.Draw methods.