Changing enum in a different class for screen

Posted by user2434321 on Game Development See other posts from Game Development or by user2434321
Published on 2013-10-11T07:00:43Z Indexed on 2013/10/23 4:12 UTC
Read the original article Hit count: 532

Filed under:
|
|

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!

© Game Development or respective owner

Related posts about XNA

Related posts about c#