Frage

I'm new to xna and I'm making an rpg. When I try and build by project I get the errors:

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

The "component" error is found in the GameScreen class and the "content" error is found in the StartScreen class.

Why are these errors happening and what do they mean? Thanks.

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 Class

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

StartScreen Class

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

}

War es hilfreich?

Lösung

Add curly braces :

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

If you don't, only the first line (component.Enabled = true;) will be inside your foreach. The next line won't have any clue about a component variable.

Andere Tipps

In your StartScreen constructor you are doing

LoadContent(content);

but the compiler doesn't know what content is. Maybe you want to add it as a parameter in the constructor.

Or better, do

LoadContent(Game.Content);

because StartScreen should inherit from DrawableGameContent.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top