2013-12-17 133 views
0

我是Xna的新手,我正在嘗試製作一個RPG。我建立了自己的遊戲,並在主遊戲課程中收到錯誤消息。我從我的代碼的Draw方法中收到錯誤。我不習慣使用switch語句,但我不確定創建一個的正確方法是什麼。我可以採取哪些措施來解決我的錯誤?謝謝。XNA Switch語句錯誤

錯誤:

就行了: 「開關(活動屏幕)」

A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type

遊戲類:

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

下一次嘗試後只有相關的代碼,而不是你全班當前屏幕像activeScreen is activeScreen.startScreen什麼不同的方法。 – pinckerman

回答

0

沒有看到類型是很難說,但ActiveScreen絕對不是您可以用作開關的類型。此外,它看起來像你在每種情況下重複相同的方法。根據上面的代碼,你根本不需要switch語句。

利用判定中,如果塊..

+0

如果我將當前打開的屏幕設置爲activeScreen,並且想要繪製哪一個打開,那麼在switch語句中正確的格式是什麼? – user481710

+0

如果你已經設置了活動屏幕,爲什麼你不能'畫'活動屏幕?我有點困惑,對不起。 –