【问题标题】:XNA Switch Statement ErrorXNA Switch 语句错误
【发布时间】:2013-12-18 07:28:25
【问题描述】:

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

错误:

上线:“switch(activeScreen)”

switch 表达式或 case 标签必须是 bool、char、string、 整数、枚举或对应的可空类型

游戏类:

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();
    }
}

【问题讨论】:

  • 下次尝试只发布相关代码,而不是全班。

标签: c# xna switch-statement


【解决方案1】:

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

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

【讨论】:

  • 如果我将当前打开的屏幕设置为 activeScreen 并想绘制任何一个打开的屏幕,那么在 switch 语句中正确的格式是什么?
  • 如果您已经设置了活动屏幕,为什么不能直接“绘制”活动屏幕?我有点糊涂了,抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多