简体   繁体   English

如何从另一个类的主类更改枚举值

[英]how to change enum value from main class in another class

I'm trying to make a start menu for my game and my code uses Enum's to moniter the screen state. 我正在尝试为游戏创建一个开始菜单,并且我的代码使用Enum监视屏幕状态。

Now i want to change the screenstate declared in the main class, in my Background class 现在我想更改主类在Background类中声明的screenstate

Screen screen = new Screen();

is declared in the Game1 class 在Game1类中声明

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? 出于某种原因,当我播放此文件并按下Enter键时,背景类的屏幕已更改,但主类的屏幕却不可以更改?

Maybe you're doing something wrong somewhere else, this code seems correct. 也许您在其他地方做错了什么,此代码似乎正确。

You can bypass this problem adding a GetState() method in your Background class, and call it in your Game1 Update method, without using ref screen . 您可以绕过此问题,而无需在ref screen Background类中添加GetState()方法,并在Game1 Update方法中调用它。

EDIT 编辑

If screen is an enum I think you can't pass it like a reference, because 如果screen是一个enum我认为您不能像引用一样传递它,因为

System.Enum is a reference type, but any specific enum type is a value type. System.Enum是引用类型,但是任何特定的枚举类型都是值类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM