2013-12-18 96 views
0

我是xna的新手,我正在製作一個RPG。當我試圖通過項目建設,我得到的錯誤:錯誤:「名稱」....「在當前上下文中不存在」

1) The name "component" does not exist in the current context:

if (component is DrawableGameComponent) 
    ((DrawableGameComponent)component).Visible = true; 

2) The name "content" does not exist in the current context:

LoadContent(content); 

「組件」誤差在GameScreen類和「內容」發現錯誤發現在StartScreen類。

這些錯誤爲什麼會發生,它們是什麼意思?謝謝。

Game1.cs

namespace myRPG 
{ 
    /// <summary> 
    /// Default Project Template 
    /// </summary> 
    public class Game1 : Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     SpriteFont font; 

     //hold current state of the keyboard 
     KeyboardState keyboardState; 
     //hold previous state of the keyboard 
     KeyboardState oldKeyboardState; 
     //active screen will be set to either startScreen or actionScreen 
     GameScreen activeScreen; 
     StartScreen startScreen; 
     ActionScreen actionScreen; 
     CharScreen charScreen; 
     ClassScreen classScreen; 
     GenderScreen genderScreen; 
     StatBar statBar; 


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

     /// <summary> 
     /// Overridden from the base Game.Initialize. Once the GraphicsDevice is setup, 
     /// we'll use the viewport to initialize some values. 
     /// </summary> 
     protected override void Initialize() 
     { 
      base.Initialize(); 
     } 

     /// <summary> 
     /// Load your graphics content. 
     /// </summary> 
     protected override void LoadContent() 
     { 

      spriteBatch = new SpriteBatch(GraphicsDevice); 

      charSprite = this.Content.Load<Texture2D>("charSprite"); 

      statBar = new StatBar(Content); 
      //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, 

       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, 
       font = Content.Load<SpriteFont>("menufont"), 
       charSprite = Content.Load<Texture2D>("charSprite")); 
      Components.Add(charScreen); 
      //charScreen.Hide(); 
      activeScreen.Hide(); 
      activeScreen = charScreen; 
      activeScreen.Show(); 

      classScreen = new ClassScreen(
       this, 
       spriteBatch, 
       font = Content.Load<SpriteFont>("menufont"), 
       charSprite = Content.Load<Texture2D>("charSprite")); 
      Components.Add(classScreen); 
      activeScreen.Hide(); 
      activeScreen = classScreen; 
      activeScreen.Show(); 


     } 





     /// <summary> 
     /// Allows the game to run logic such as updating the world, 
     /// checking for collisions, gathering input, and playing audio. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 

     protected override void Update(GameTime gameTime) 
     { 
      //get hte current state of the keyboard 
      keyboardState = Keyboard.GetState(); 

      UpdateSprite(gameTime); 
      statBar.Update(); 

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

     /// <summary> 
     /// This is called when the game should draw itself. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 

     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 startScreen: 
        DrawStartScreen(); 
        startScreen.Draw(spriteBatch); 
        //StartScreen(); 
        break; 
       case charScreen: 
        DrawCharScreen(); 
        charScreen.Draw(spriteBatch); 
        //CharScreen(); 
        break; 
       case actionScreen: 
        //draw map 
        statBar.Draw(spriteBatch); 
        break; 
       case classScreen: 
        DrawClassScreen(); 
        classScreen.Draw(); 
        //ClassScreen(); 
        statBar.Draw(spriteBatch); 
        break; 
       case genderScreen: 
        DrawGenderScreen(); 
        genderScreen.Draw(spriteBatch); 
        //GenderScreen(); 
        break; 
      } 

      base.Draw(gameTime); 
      spriteBatch.End(); 
     } 
    } 

GameScreen類

foreach (GameComponent component in components) 
    component.Enabled = true; 
if (component is DrawableGameComponent) 
    ((DrawableGameComponent)component).Visible = false; 

啓動畫面類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Content; 

namespace AnyRPG 
{ 
    class StartScreen : GameScreen 
    { 
     MenuComponent menuComponent; 
     //background image 
     Texture2D RPGLogo; 
     //used to make the image fill the game window 
     Rectangle rectangleScreen; 

     //gets the users menu choice 
     public int SelectedIndex 
     { 
      //gets what the user chooses which is set to SelectedIndex 
      get 
      { 
       return menuComponent.SelectedIndex; 
      } 
      set 
      { 
       menuComponent.SelectedIndex = value; 
      } 
     } 

     public StartScreen(Game game, SpriteBatch spriteBatch, SpriteFont spriteFont, Texture2D image) 
      : base(game, spriteBatch) 
     { 
      LoadContent(content); 
      //constructor creates menu with start and exit options 
      string[] menuOptions = { "Start Game", "Leave Game" }; 
      //the menu is added to the list of components 
      menuComponent = new MenuComponent(game, 
       spriteBatch, 
       spriteFont, 
       menuOptions); 
      Components.Add(menuComponent); 
      //sets the image to the image that is sent in 
      this.RPGLogo = RPGLogo; 
      // a rectangle is created which fill the game screen 
      rectangleScreen = new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height/2); 
     } 
     private void LoadContent(ContentManager content) 
     { 
      RPGLogo = content.Load<Texture2D>("RPGLogo"); 
     } 

     public override void Update(GameTime gameTime) 
     { 
      base.Update(gameTime); 
     } 

     public override void Draw(GameTime gameTime) 
     { 
      spriteBatch.Draw(RPGLogo, rectangleScreen, Color.AntiqueWhite); 
      base.Draw(gameTime); 
     } 
    } 

}

+0

下一次只發布相關的代碼,你不需要發佈你的整個項目。 – pinckerman

回答

5

添加大括號:

foreach (GameComponent component in components) 
{ component.Enabled = true; 
    if (component is DrawableGameComponent) 
     ((DrawableGameComponent)component).Visible = false; 
} 

如果你不這樣做,只有第一行(component.Enabled = true;)將是你的foreach內。下一行不會有關於component變量的任何線索。

+1

這是一個「範圍」問題。 +1 –

+0

我已經添加了大括號。謝謝!你知道爲什麼我得到LoadContent(內容)的錯誤; ? – user481710

+0

我沒有看到你的'content'參數被聲明在哪裏,它是來自基類的字段嗎?另外,這個http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.drawablegamecomponent.loadcontent.aspx顯示'LoadContent'沒有參數。也許你應該刪除它? –

0

在你StartScreen構造你正在做

LoadContent(content); 

但是編譯器不知道什麼content是。也許你想把它作爲參數添加到構造函數中。

或者更好,做

LoadContent(Game.Content); 

因爲StartScreen應該DrawableGameContent繼承。

相關問題