0
\$\begingroup\$

I'm trying to make a start menu for my game and my code uses Enum's to moniter the screen state.

Now i want to change the screenstate declared in the main class, in my Background class

Screen screen = new Screen();

is declared in the Game1 class

Background(ref screen);

This is in the update method for the Background Class

KeyboardState keystate = Keyboard.GetState();
switch (screen)
{
    case Screen.Start:
        if (isPressed && keystate.IsKeyUp(Keys.Up) && keystate.IsKeyUp(Keys.Down) && keystate.IsKeyUp(Keys.Enter))
        {
            isPressed = false;
        }
        if (keystate.IsKeyDown(Keys.Down) && isPressed != true)
        {
            if (menuState == MenuState.Options)
                menuState = MenuState.Credits;
            if (menuState == MenuState.Play)
                menuState = MenuState.Options;
            isPressed = true;
        }

        if (keystate.IsKeyDown(Keys.Up) && isPressed != true)
        {
            if (menuState == MenuState.Options)
                menuState = MenuState.Play;
            if (menuState == MenuState.Credits)
                menuState = MenuState.Options;
            isPressed = true;
        }

        switch (menuState)
        {
            case MenuState.Play:
                arrowRect.X = 450;
                arrowRect.Y = 220;
            if (keystate.IsKeyDown(Keys.Enter) && isPressed != true)
                screen = Screen.Play;
                break;
            case MenuState.Options:
                arrowRect.X = 419;
                arrowRect.Y = 340;
                if (keystate.IsKeyDown(Keys.Enter) && isPressed != true)
                    screen = Screen.Options;
                    break;
            case MenuState.Credits:
                arrowRect.X = 425;
                arrowRect.Y = 460;
                if (keystate.IsKeyDown(Keys.Enter) && isPressed != true)
                    screen = Screen.Credits;
                break;
            }
            break;
       }
 }

For some reason when I play this and I hit the enter button the Background class's screen is changed but the main class's screen isn't how can i change this?

EDIT 1*

class Background
{
    private Texture2D background;
    private Rectangle backgroundRect;
    private Texture2D arrow;
    private Rectangle arrowRect;
    private Screen screen;
    private MenuState menuState;
    private bool isPressed = false;

    public Screen getScreenState(ref Screen screen)
    {
        this.screen = screen;
        return this.screen;
    }

    public Background(ref Screen screen)
    {
        this.screen = screen;
    }

    public void Update()
    {
        KeyboardState keystate = Keyboard.GetState();
        switch (screen)
        {
            case Screen.Start:
                if (isPressed && keystate.IsKeyUp(Keys.Up) && keystate.IsKeyUp(Keys.Down) && keystate.IsKeyUp(Keys.Enter))
                {
                    isPressed = false;
                }
                if (keystate.IsKeyDown(Keys.Down) && isPressed != true)
                {
                    if (menuState == MenuState.Options)
                        menuState = MenuState.Credits;
                    if (menuState == MenuState.Play)
                        menuState = MenuState.Options;
                    isPressed = true;
                }

                if (keystate.IsKeyDown(Keys.Up) && isPressed != true)
                {
                    if (menuState == MenuState.Options)
                        menuState = MenuState.Play;
                    if (menuState == MenuState.Credits)
                        menuState = MenuState.Options;
                    isPressed = true;
                }

                switch (menuState)
                {
                    case MenuState.Play:
                        arrowRect.X = 450;
                        arrowRect.Y = 220;
                        if (keystate.IsKeyDown(Keys.Enter) && isPressed != true)
                            screen = Screen.Play;
                        break;
                    case MenuState.Options:
                        arrowRect.X = 419;
                        arrowRect.Y = 340;
                        if (keystate.IsKeyDown(Keys.Enter) && isPressed != true)
                            screen = Screen.Options;
                        break;
                    case MenuState.Credits:
                        arrowRect.X = 425;
                        arrowRect.Y = 460;
                        if (keystate.IsKeyDown(Keys.Enter) && isPressed != true)
                            screen = Screen.Credits;
                        break;
                }
                break;
            case Screen.Pause:
                break;
            case Screen.Over:
                break;


        }
    }

    public void LoadStartContent(ContentManager Content, GraphicsDeviceManager graphics)
    {
        background = Content.Load<Texture2D>("startBackground");
        arrow = Content.Load<Texture2D>("arrow");
        backgroundRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
        arrowRect = new Rectangle(450, 225, arrow.Width, arrow.Height);
        screen = Screen.Start;
    }

    public void LoadPlayContent(ContentManager Content, GraphicsDeviceManager graphics)
    {
        background = Content.Load<Texture2D>("Background");
        backgroundRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
        screen = Screen.Play;
    }

    public void LoadOverContent(ContentManager Content, GraphicsDeviceManager graphics)
    {

    }

    public void Draw(SpriteBatch spritebatch)
    {
        if (screen == Screen.Start)
        {
            spritebatch.Draw(background, backgroundRect, Color.White);
            spritebatch.Draw(arrow, arrowRect, Color.White);
        }
        else
            spritebatch.Draw(background, backgroundRect, Color.White);
    }
}

Thats my background class!

\$\endgroup\$
3
  • \$\begingroup\$ Can you provide some more code of you Background class? You are passing the ref Screen into the Backgrounds constructor? If the logic of menuState changing is in a method you should rather pass the screen there. This is a good read to understand more about value and reference passing: yoda.arachsys.com/csharp/parameters.html \$\endgroup\$
    – floAr
    Commented Oct 11, 2013 at 10:22
  • \$\begingroup\$ There's my background class i understand passing the screen there that makes sense but how can i do that? Is it because the loaded content is all one variable and it doesnt switch properly? \$\endgroup\$ Commented Oct 11, 2013 at 18:05
  • \$\begingroup\$ There are some quirks with your code. The bottom line is that you are mixing up two different concepts: 1) storing the screen instance in the background class & 2) passing the screen instance from elsewhere. If you only got one screenState in your whole game take a look at the singleton pattern else decide if you want the background class to host the screen instance or if you want to pass it down. Feel free to tell me what you decided to do (It is hard to tell for me without knowing your programm) and I will help you further \$\endgroup\$
    – floAr
    Commented Oct 12, 2013 at 13:05

1 Answer 1

-1
\$\begingroup\$

The final solution was this

    #region Properties

    private Texture2D background;
    private Rectangle backgroundRect;
    private Texture2D arrow;
    private Rectangle arrowRect;
    private Texture2D SecondScreen;
    private MenuState menuState;
    private bool isPressed = false;
    public Level level;
    public Screen screen;

    #endregion

    public void Update(ContentManager Content, GraphicsDeviceManager graphics, ref int? playerLives, ref int p1Score, Paddle paddle, Ball ball)
    {
        KeyboardState keystate = Keyboard.GetState();

        if (isPressed && keystate.IsKeyUp(Keys.Up) && keystate.IsKeyUp(Keys.Down) && keystate.IsKeyUp(Keys.Enter) && keystate.IsKeyUp(Keys.P))
        {
            isPressed = false;
        }

        switch (screen)
        {
            #region ScreenStart
            case Screen.Start:
                if (keystate.IsKeyDown(Keys.Down) && !isPressed)
                {
                    if (menuState == MenuState.Options)
                        menuState = MenuState.Credits;
                    if (menuState == MenuState.Play)
                        menuState = MenuState.Options;
                    isPressed = true;
                }

                if (keystate.IsKeyDown(Keys.Up) && !isPressed)
                {
                    if (menuState == MenuState.Options)
                        menuState = MenuState.Play;
                    if (menuState == MenuState.Credits)
                        menuState = MenuState.Options;
                    isPressed = true;
                }
                #region Menu States
                switch (menuState)
                {
                    case MenuState.Play:
                        arrowRect.X = 450;
                        arrowRect.Y = 220;
                        if (keystate.IsKeyDown(Keys.Enter) && !isPressed)
                        {
                            LoadPlayContent(Content);
                        }

                        break;
                    case MenuState.Options:
                        arrowRect.X = 419;
                        arrowRect.Y = 340;
                        if (keystate.IsKeyDown(Keys.Enter) && !isPressed)
                            screen = Screen.Options;
                        break;
                    case MenuState.Credits:
                        arrowRect.X = 425;
                        arrowRect.Y = 460;
                        if (keystate.IsKeyDown(Keys.Enter) && !isPressed)
                            screen = Screen.Credits;
                        break;
                }
                #endregion
                break;
            #endregion
            #region ScreenPause
            case Screen.Pause:
                if (keystate.IsKeyDown(Keys.P) && !isPressed)
                {
                    isPressed = true;
                    screen = Screen.Play;

                }
                if (keystate.IsKeyDown(Keys.Back))
                {
                    screen = Screen.Start;
                    LoadStartContent(Content, graphics, ref playerLives, paddle, ball);
                }
                break;
            #endregion
            #region ScreenOver
            case Screen.Over:
                LoadOverContent(Content);
                if (keystate.IsKeyDown(Keys.Enter) && !isPressed)
                {
                    LoadStartContent(Content, graphics, ref playerLives);
                    isPressed = true;
                }
                break;
            #endregion
            #region ScreenPlay
            case Screen.Play:
                if (playerLives < 1)
                {
                    screen = Screen.Over;
                    LoadOverContent(Content);
                    playerLives = 50;
                    p1Score = 0;
                }
                if (keystate.IsKeyDown(Keys.P) && !isPressed)
                {
                    isPressed = true;
                    LoadPauseContent(Content);
                }
                #region LevelLogic
                if (p1Score >= 50 && level == Level.One)
                    level = Level.Two;
                if (p1Score >= 100 && level == Level.Two)
                    level = Level.Three;
                if (p1Score >= 150 && level == Level.Three)
                    level = Level.Four;
                if (p1Score >= 200 && level == Level.Four)
                    level = Level.Five;
                if (p1Score >= 250 && level == Level.Five)
                    level = Level.Six;
                if (p1Score >= 300 && level == Level.Six)
                    level = Level.Seven;
                if (p1Score >= 350 && level == Level.Seven)
                    level = Level.Eight;
                if (p1Score >= 400 && level == Level.Eight)
                    level = Level.Nine;
                if (p1Score >= 450 && level == Level.Nine)
                    level = Level.Ten;
                if (p1Score >= 500 && level == Level.Ten)
                    level = Level.Eleven;
                if (p1Score >= 550 && level == Level.Eleven)
                    level = Level.Twelve;
                if (p1Score >= 600 && level == Level.Twelve)
                    level = Level.Thirteen;
                if (p1Score >= 650 && level == Level.Thirteen)
                    level = Level.Fourteen;
                if (p1Score >= 700 && level == Level.Fourteen)
                    level = Level.Fifteen;
                #endregion
                LoadPlayContent(Content);
                break;
            #endregion
            #region ScreenCredits
            case Screen.Credits:
                LoadCreditsContent(Content);
                if (keystate.IsKeyDown(Keys.Back))
                {
                    screen = Screen.Start;
                    p1Score = 0;
                    LoadStartContent(Content, graphics, ref playerLives, paddle, ball);
                }
                break;
            #endregion
        }
    }

    #region LoadContents

    #region LoadStartContentsx2
    public void LoadStartContent(ContentManager Content, GraphicsDeviceManager graphics, ref int? playerLives, Paddle paddle, Ball ball)
    {
        background = Content.Load<Texture2D>("startBackground");
        arrow = Content.Load<Texture2D>("arrow");
        backgroundRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
        arrowRect = new Rectangle(450, 225, arrow.Width, arrow.Height);
        screen = Screen.Start;
        playerLives = 5;
        paddle.AIpaddlePos.Y = graphics.GraphicsDevice.Viewport.Height / 2 - paddle.AIpaddle.Height / 2;
        paddle.paddlePos.Y = graphics.GraphicsDevice.Viewport.Height / 2 - paddle.AIpaddle.Height / 2;
        ball.gameStarted = false;
        ball.ballPos.X = graphics.GraphicsDevice.Viewport.Width / 2 - ball.ball.Width / 2;
        ball.ballPos.Y = graphics.GraphicsDevice.Viewport.Height / 2 - ball.ball.Height / 2;
        level = Level.One;
    }

    public void LoadStartContent(ContentManager Content, GraphicsDeviceManager graphics, ref int? playerLives)
    {
        background = Content.Load<Texture2D>("startBackground");
        arrow = Content.Load<Texture2D>("arrow");
        backgroundRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
        arrowRect = new Rectangle(450, 225, arrow.Width, arrow.Height);
        screen = Screen.Start;
        playerLives = 5;
    }
    #endregion

    private void LoadPlayContent(ContentManager Content)
    {
        screen = Screen.Play;
        switch (level)
        #region levels
        {
            case Level.One:
                background = Content.Load<Texture2D>("levelOneBackground");
                break;
            case Level.Two:
                background = Content.Load<Texture2D>("levelTwoBackground");
                break;
            case Level.Three:
                background = Content.Load<Texture2D>("levelThreeBackground");
                break;
            case Level.Four:
                background = Content.Load<Texture2D>("levelFourBackground");
                break;
            case Level.Five:
                background = Content.Load<Texture2D>("levelFiveBackground");
                break;
            case Level.Six:
                background = Content.Load<Texture2D>("levelSixBackground");
                break;
            case Level.Seven:
                background = Content.Load<Texture2D>("levelSevenBackground");
                break;
            case Level.Eight:
                background = Content.Load<Texture2D>("levelEightBackground");
                break;
            case Level.Nine:
                background = Content.Load<Texture2D>("levelNineBackground");
                break;
            case Level.Ten:
                background = Content.Load<Texture2D>("levelTenBackground");
                break;
            case Level.Eleven:
                background = Content.Load<Texture2D>("levelElevenBackground");
                break;
            case Level.Twelve:
                background = Content.Load<Texture2D>("levelTwelveBackground");
                break;
            case Level.Thirteen:
                background = Content.Load<Texture2D>("levelThirteenBackground");
                break;
            case Level.Fourteen:
                background = Content.Load<Texture2D>("levelFourteenBackground");
                break;
            case Level.Fifteen:
                background = Content.Load<Texture2D>("levelFifteenBackground");
                break;
        }
        #endregion
    }

    private void LoadOverContent(ContentManager Content)
    {
        background = Content.Load<Texture2D>("gameOverBackground");
        screen = Screen.Over;
    }

    private void LoadPauseContent(ContentManager Content)
    {
        SecondScreen = Content.Load<Texture2D>("Pause");
        screen = Screen.Pause;
    }

    private void LoadOptionsContent(ContentManager Content)
    {
        SecondScreen = Content.Load<Texture2D>("Options");
        screen = Screen.Options;
    }

    private void LoadCreditsContent(ContentManager Content)
    {
        background = Content.Load<Texture2D>("Credits");
        screen = Screen.Credits;
    }
    #endregion

    public void Draw(SpriteBatch spritebatch)
    {
        switch (screen)
        {
            case Screen.Start:
                spritebatch.Draw(background, backgroundRect, Color.White);
                spritebatch.Draw(arrow, arrowRect, Color.White);
                break;
            case Screen.Pause:
                spritebatch.Draw(background, backgroundRect, Color.White);
                spritebatch.Draw(SecondScreen, backgroundRect, Color.White);
                break;
            default:
                spritebatch.Draw(background, backgroundRect, Color.White);
                break;
        }
    }

Thanks, this was my way of doing it and the games done :D basically i just stored

Screen screen

in the Background class and updated it in the Update method in Game1 class to sync the Screen in that class.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .