XNA Switch语句错误

时间:2013-12-17 00:08:18

标签: c# xna switch-statement

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

1 个答案:

答案 0 :(得分:0)

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

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