XNA 交换机语句错误

本文关键字:错误 语句 交换机 XNA | 更新日期: 2024-10-26 03:03:23

我是Xna的新手,我正在尝试制作RPG。我构建了我的游戏,但在主游戏类中收到错误。我从代码的 Draw 方法收到错误。我不习惯使用 switch 语句,也不确定制作 switch 语句的正确方法是什么。我可以采取哪些步骤来解决我的错误?谢谢。

错误:

在行:"开关(活动屏幕)"

开关表达式或大小写标签必须是布尔值、字符、字符串、 积分、枚举或相应的可为空的类型

游戏类:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont font;
    KeyboardState keyboardState;
    KeyboardState oldKeyboardState;
    GameScreen activeScreen;
    StartScreen startScreen;
    ActionScreen actionScreen;
    CharScreen charScreen;
    ClassScreen classScreen;
    GenderScreen genderScreen;
    Vector2 charPosition = new Vector2(0, 0);
    Texture2D charSprite;
    int charHorizSpeed = 1;
    int charVertSpeed = 1;
    Texture2D logoTexture;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.IsFullScreen = false;
        oldKeyboardState = Keyboard.GetState();
    }
    protected override void Initialize()
    {
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        charSprite = this.Content.Load<Texture2D>("charSprite");
        //create new instance of the startScreen
        startScreen = new StartScreen(
            this,
            spriteBatch,
            //loads the font to the screen
            font = Content.Load<SpriteFont>("menufont"),
            //loads the image to the screen
            Content.Load<Texture2D>("RPGLogo"));
        //adds the screen to components
        Components.Add(startScreen);
        //startScreen.Hide();
        //creates new instance the actionScreen
        actionScreen = new ActionScreen(
            this,
            spriteBatch,
            Content.Load<Texture2D>("tileMap"),
            Content.Load<Texture2D>("character"),
            charSprite = Content.Load<Texture2D>("charSprite"));
        //adds the screen to components
        Components.Add(actionScreen);
        //actionScreen.Hide();
        activeScreen.Hide();
        activeScreen = startScreen;
        activeScreen.Show();
        charScreen = new CharScreen(
            this,
            spriteBatch,
            charSprite = Content.Load<Texture2D>("charSprite"),
            font = Content.Load<SpriteFont>("menufont"));
        Components.Add(charScreen);
        //charScreen.Hide ();
        activeScreen.Hide();
        activeScreen = charScreen;
        activeScreen.Show();
        classScreen = new ClassScreen(
            this,
            spriteBatch,
            charSprite = Content.Load<Texture2D>("charSprite"),
            font = Content.Load<SpriteFont>("menufont"));
        Components.Add(classScreen);
        activeScreen.Hide();
        activeScreen = classScreen;
        activeScreen.Show();
    }
    protected override void Update(GameTime gameTime)
    {
        //get hte current state of the keyboard
        keyboardState = Keyboard.GetState();
        UpdateSprite(gameTime);
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        //checks if instances are the same
        if (activeScreen == startScreen)
        {
            //checks if enter key was pressed
            if (CheckKey(Keys.Enter))
            {
                //if the selected index is on the first item (start game), the current active screen will hide adn it will be switched to the action screen
                if (startScreen.SelectedIndex == 0)
                {
                    activeScreen.Hide();
                    activeScreen = actionScreen;
                    activeScreen.Show();
                }
                //if the selected index is on the second item (exit) the game will exit
                if (startScreen.SelectedIndex == 1)
                {
                    this.Exit();
                }
            }
        }
        if (activeScreen == charScreen)
        {
            if (CheckKey(Keys.Enter))
            {
                if (charScreen.SelectedIndex == 0)
                {
                    activeScreen.Hide();
                    activeScreen = classScreen;
                    activeScreen.Show();
                    //create a drop down menu for character class options/pop up?
                }
            }
            if (CheckKey(Keys.Enter))
            {
                if (charScreen.SelectedIndex == 1)
                {
                    activeScreen.Hide();
                    activeScreen = genderScreen;
                    activeScreen.Show();
                }
            }
        }
        if (activeScreen == classScreen)
        {
            if (CheckKey(Keys.Enter))
            {
                if (classScreen.SelectedIndex == 0)
                {
                    //call warior class
                }
                if (classScreen.SelectedIndex == 1)
                {
                    //call mage class
                }
                if (classScreen.SelectedIndex == 2)
                {
                    //call ranger class
                }
            }
        }
        if (activeScreen == genderScreen)
        {
            if (CheckKey(Keys.Enter))
            {
                if (genderScreen.SelectedIndex == 0)
                {
                    //call gender class (male)
                }
                if (genderScreen.SelectedIndex == 1)
                {
                    //call gender class (female)
                }
            }
        }
        base.Update(gameTime);
        oldKeyboardState = keyboardState;
    }
    private bool CheckKey(Keys theKey)
    {
        //returns if the key was pressed in the last frame
        return keyboardState.IsKeyUp(theKey) &&
        oldKeyboardState.IsKeyDown(theKey);
    }
    private void DrawStartScreen()
    {
        spriteBatch.DrawString(font, "Vengence In Albion", new Vector2(20, 45), Color.White);
    }
    private void DrawCharScreen()
    {
        spriteBatch.DrawString(font, "Character Selection", new Vector2(20, 45), Color.White);
        spriteBatch.Draw(charSprite, charPosition, Color.White);
    }
    private void DrawClassScreen()
    {
        spriteBatch.DrawString(font, "Choose your Class", new Vector2(20, 45), Color.White);
    }
    private void DrawGenderScreen()
    {
        spriteBatch.DrawString(font, "Choose a gender", new Vector2(20, 45), Color.White);
    }
    private void UpdateSprite(GameTime gameTime)
    {
        //move the sprite by speed
        KeyboardState newState = Keyboard.GetState();
        int MaxX = Window.ClientBounds.Width - charSprite.Width;
        int MaxY = Window.ClientBounds.Height - charSprite.Height;
        int MinX = 0;
        int MinY = 0;
        if (newState.IsKeyDown(Keys.Left))
        {
            // Move left
            charHorizSpeed = -1;
        }
        if (newState.IsKeyDown(Keys.Right))
        {
            // Move left
            charHorizSpeed = 1;
        }
        if (newState.IsKeyDown(Keys.Up))
        {
            // Move left
            charVertSpeed = -1;
        }
        if (newState.IsKeyDown(Keys.Down))
        {
            // Move left
            charVertSpeed = 1;
        }
        if (charPosition.X > MaxX)
        {
            charHorizSpeed *= -1;
            charPosition.X = MaxX;
        }
        else if (charPosition.X < MinX)
        {
            charHorizSpeed *= -1;
            charPosition.X = MinX;
        }
        if (charPosition.Y > MaxY)
        {
            charVertSpeed *= -1;
            charPosition.Y = MaxY;
        }
        else if (charPosition.Y < MinY)
        {
            charVertSpeed *= -1;
            charPosition.Y = MinY;
        }
        oldKeyboardState = keyboardState;
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.DarkSlateBlue);
        spriteBatch.Begin();
             switch (activeScreen)
        {
            case activeScreen.startScreen:
                DrawStartScreen();
                StartScreen();
                break;
            case activeScreen.charScreen:
                DrawCharScreen();
                CharScreen();
                break;
            case activeScreen.actionScreen:
                //draw map
                break;
            case activeScreen.classScreen:
                DrawClassScreen();
                ClassScreen();
                break;
            case activeScreen.genderScreen:
                DrawGenderScreen();
                GenderScreen();
                break;
        }
         base.Draw(gameTime);
        spriteBatch.End();
    }
}

XNA 交换机语句错误

如果不看到类型,很难分辨,但 activeScreen 绝对不是可以用作开关的类型。 此外,看起来您在每种情况下都在重复相同的方法。 根据上面的代码,你根本不需要 switch 语句。

使用不同的方法来确定当前屏幕,例如activeScreen is activeScreen.startScreen或if块中的某些内容。