错误:"名称"...."在当前上下文中不存在"

 宝马z4笑嘻嘻 发布于 2023-02-09 17:58

我是xna的新手,我正在制作一个RPG.当我尝试按项目构建时,我得到错误:

1)当前上下文中不存在名称"component":

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

2)当前上下文中不存在名称"content":

LoadContent(content);

"组件"的错误在发现GameScreen类和"内容"的错误在发现启动画面类.

为什么会发生这些错误?它们是什么意思?谢谢.

Game1.cs

namespace myRPG
{
    /// 
    /// Default Project Template
    /// 
    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();
        }

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

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

            spriteBatch = new SpriteBatch(GraphicsDevice);

            charSprite = this.Content.Load("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("menufont"),
                //loads the image to the screen
                Content.Load("RPGLogo"));
            //adds the screen to components
            Components.Add(startScreen);
            //startScreen.Hide();

            //creates new instance the actionScreen
            actionScreen = new ActionScreen(
                this,
                spriteBatch,

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

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


        }





        /// 
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// 
        /// Provides a snapshot of timing values.

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

        /// 
        /// This is called when the game should draw itself. 
        /// 
        /// Provides a snapshot of timing values.

        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;

StartScreen类

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

}

1 个回答
  • 添加花括号:

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

    如果不这样做,只有第一行(component.Enabled = true;)将在你的foreach中.下一行不会对component变量有任何线索.

    2023-02-09 18:00 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有